Skip to content

fix: encode dynamic URL path segments#5963

Merged
kartik-mem0 merged 7 commits into
mem0ai:mainfrom
VectorPeak:fix/encode-url-path-segments
Jul 7, 2026
Merged

fix: encode dynamic URL path segments#5963
kartik-mem0 merged 7 commits into
mem0ai:mainfrom
VectorPeak:fix/encode-url-path-segments

Conversation

@VectorPeak

@VectorPeak VectorPeak commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Linked Issue

Fixes #5893. Re-submitted after fork recovery; original PR #5818 was closed because the fork was deleted.

Description

What Problem This Solves

Several hosted SDK and CLI methods build API paths by directly interpolating caller-provided IDs into URL path segments:

/v1/memories/{memory_id}/
/v1/memories/{memory_id}/history/
/v2/entities/{entity_type}/{entity_id}/
/v1/event/{event_id}/

Those values are identifiers from the SDK or CLI caller's perspective, but /, ?, and # are structural URL characters. If an ID contains one of those characters, the request no longer targets the intended single path segment.

For example, deleting a user/entity named:

org/team?active#frag

can construct a path like:

/v2/entities/user/org/team?active#frag/

That can be parsed as an extra path segment plus query/fragment syntax instead of one literal entity identifier. The intended request path is:

/v2/entities/user/org%2Fteam%3Factive%23frag/

This keeps the identifier as a single URL path segment and lets the server receive the literal ID value after decoding.

Change

This PR adds local path-segment encoding helpers and uses them where dynamic IDs are inserted into hosted API paths.

  • Python SDK and Python CLI use urllib.parse.quote(str(value), safe="") so / is encoded instead of being preserved by quote()'s default behavior.
  • TypeScript SDK and Node CLI use encodeURIComponent(String(value)) for the same single-segment encoding behavior.
  • Query-string parameters are unchanged; this PR only touches dynamic path segments.
  • The public API surface is unchanged. Callers can continue passing the same IDs, including IDs that contain URL syntax characters.

Affected hosted API paths include:

/v1/memories/{memory_id}/
/v1/memories/{memory_id}/history/
/v2/entities/{entity_type}/{entity_id}/
/v1/event/{event_id}/

Conflict Resolution

This branch has been updated on top of the current main branch. The conflicts came from related CLI fixes that landed after the original PR:

The resolution keeps both fixes: entity delete results remain keyed by entity type, and entity_type / entity_id are still encoded before constructing /v2/entities/.../.../ paths. The Node CLI test file also keeps both the result-preservation tests from main and the path-encoding regression tests from this PR.

Evidence

Representative encoded request examples:

/v1/memories/mem%2Fa%3Fb%23c/
/v1/memories/mem%2Fa%3Fb%23c/history/
/v2/entities/user/org%2Fteam%3Factive%23frag/
/v1/event/evt%2Fa%3Fb%23c/

Validation after resolving conflicts:

git diff --check
uv run --with pytest pytest tests/test_client.py -q
cd cli/python && PYTHONPATH=src uv run --with pytest pytest tests/test_platform_backend.py tests/test_platform_backend_paths.py -q

Results:

git diff --check: PASS
tests/test_client.py: 29 passed
cli/python tests: 5 passed

Node/TypeScript tests were not re-run in this conflict-resolution pass because this checkout does not have node_modules installed, but the affected Node CLI conflict was resolved by keeping both existing test groups.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactor (no functional changes)
  • Documentation update

Breaking Changes

N/A

Test Coverage

  • I added/updated unit tests
  • I added/updated integration tests
  • I tested manually (describe below)
  • No tests needed (explain why)

Regression coverage is included for:

  • Python SDK: tests/test_client.py
  • Python CLI: cli/python/tests/test_platform_backend_paths.py
  • TypeScript SDK: mem0-ts/src/client/tests/memoryClient.crud.test.ts and mem0-ts/src/client/tests/memoryClient.users.test.ts
  • Node CLI: cli/node/tests/platform-backend.test.ts

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing targeted tests pass locally where the environment was available
  • I have updated documentation if needed

@CLAassistant

CLAassistant commented Jun 30, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@VectorPeak
VectorPeak force-pushed the fix/encode-url-path-segments branch from 84403b3 to c92cea4 Compare June 30, 2026 03:05
@VectorPeak

Copy link
Copy Markdown
Contributor Author

Hi @kartik-mem0, sorry to bother you again.

The previous PR for this fix had already received an LGTM, but I accidentally deleted the fork branch before it could be merged, so GitHub could no longer complete the merge from that branch.

I opened this new PR to address the same issue with the same intended fix. When you have a chance, could you please help review this one as well?

Thank you very much for your time and help.

Widen the Python CLI helper's parameter hint from str to Any to match
the SDK's _encode_path_segment. Both bodies coerce via quote(str(value),
safe=""), so the str hint was misleading — the helper intentionally
accepts non-str ids (int, UUID). No behavior change.
Fixes the TypeScript SDK CI build failure: `prettier --check .` rejected
the new test file's line wrapping (build_ts_sdk lint step). No logic change.
@kartik-mem0
kartik-mem0 merged commit cc52f0e into mem0ai:main Jul 7, 2026
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encode dynamic URL path segments in SDK and CLI requests

3 participants