Repository navigation
fix(python-sdk): raise MemoriaResponseError for empty 2xx bodies - #502
loveRhythm1990 merged 2 commits into
Conversation
…rixorigin#499) A 204, zero-length 200 or JSON null success body reaches the resource parsers as None, and Model.from_dict(None) raised a raw AttributeError that bypassed except MemoriaError handlers. Validate at the parse boundary: models.from_dict now requires a JSON object and store_batch requires a JSON array, both raising MemoriaResponseError. observe() is left to matrixorigin#498.
|
This pull request is merged. |
loveRhythm1990
left a comment
There was a problem hiding this comment.
LGTM for the code at ae61a23.
I independently checked all four changed files, the HTTP transport and resource call sites. The reported empty-response failure is real: running the new tests against base gives 69 failures; this head passes all 1,078 SDK unit tests. The guards preserve supported array retrieval responses and intentionally empty endpoints. Eight additional sync/async wire-body checks also pass, including literal JSON null.
Non-blocking test correction: httpx.Response(200, json=None) actually creates an empty body, so the case named json-null duplicates empty-200. Use content=b"null" with application/json to exercise JSON decoding. The implementation handles literal null correctly; I verified that independently.
Integration note: #502 and #503 are individually conflict-free against current main, but merging their heads locally produces a conflict in sdk/python/CHANGELOG.md (models.py merges cleanly). Preserve both changelog entries when rebasing whichever lands second.
Coverage: 4/4 changed files reviewed, 100%, none skipped. Current CI is green. This is a code-review comment, not a merge action.
httpx.Response(200, json=None) produces an empty body, so the case duplicated empty-200. Use content=b'null' with a JSON content type to exercise JSON decoding. Also move the changelog entry to the end of the Fixed section so it does not collide with the neighbouring matrixorigin#502 entry.
httpx.Response(200, json=None) produces an empty body, so the case duplicated empty-200. Use content=b'null' with a JSON content type to exercise JSON decoding.
loveRhythm1990
left a comment
There was a problem hiding this comment.
LGTM at f803135.
The null-body fixture now sends literal JSON null, so it exercises decoding separately from an empty response. I reviewed all four current changed files and confirmed the production guards are unchanged from the validated fix. All 1,078 SDK unit tests pass on this head.
A local trial merge with #503's current head is conflict-free and preserves both changelog entries; all 1,125 SDK unit tests pass on the combined tree. No additional findings.
Coverage: 4/4 changed files reviewed, 100%, none skipped. Current test CI is green and the head has no conflict with main.
Fixes #498 ## Problem `observe()` and `observe_deduplicated()` (sync and async, 4 call sites in `client.py`) passed the 2xx body to `ObserveResult.from_dict(data or {})`: - `["mem_1"]`, `"text"`, `42` raised a raw `AttributeError` from `.get()`. - `[]`, `false`, `null`, 204 and an empty 200 were collapsed to `{}` by `data or {}`, so the SDK reported "no memories extracted". - `{"memories": null}` raised `TypeError: 'NoneType' object is not iterable`. ## Change - `client.py`: pass `data` straight to `ObserveResult.from_dict`, removing `data or {}`. - `models.py`: `ObserveResult.from_dict` requires a JSON object and raises `MemoriaResponseError` otherwise. An explicit `"memories": null` is read as `[]`. A `memories` value that is neither an array nor null (for example a string, which `list()` would split into characters) is rejected. - `tests/unit/test_observe_response_shape.py`: sync and async tests for both methods, covering every body shape in the issue plus the empty forms, the null `memories` case and valid bodies. - CHANGELOG entry. The server's `/v1/observe` handlers always return an object with a `memories` array, so an empty body is a protocol violation, not a valid "nothing extracted" answer. ## Relationship to #502 #502 (fix for #499) makes the other resource parsers raise `MemoriaResponseError` for empty bodies. This PR does not use the `_require_object` helper that #502 adds, so the code does not depend on it. The two PRs touch `sdk/python/CHANGELOG.md` and `models.py`. This PR's changelog entry sits at the end of the `Fixed` section, away from #502's entry, and a trial merge of both heads is conflict-free. If main changes before they land, rebase whichever lands second and keep both entries. A later cleanup could switch `ObserveResult.from_dict` to the shared helper. ## Verification - `pytest tests/unit`: 1055 passed. - The `json-null` test case now sends a literal `null` body (`httpx.Response(200, json=None)` sends an empty one). - With the `src` changes stashed, the new test file has 44 failures. The 3 valid-body cases pass either way. - `ruff check` and `mypy src` are clean.
Fixes #499
Problem
A 2xx response with an empty body (204, zero-length 200, or JSON
null) is folded intoNoneby the transport. The resource modules pass it toModel.from_dict(data), which calls.get()and raises a rawAttributeError: 'NoneType' object has no attribute 'get'. That escapes the documentedMemoriaErrorhierarchy and bypasses callers'except MemoriaErrorhandlers.Change
models.py: add_require_object()and call it at the top offrom_dictforMemory,RetrieveResult(dict form),PurgeResult,Snapshot,Branch,ApplyResult,ProfileandGovernanceResult. A non-object body raisesMemoriaResponseError, the same exceptionMemoryPagealready uses.resources/memories.py:store_batch(sync and async) requires a JSON array, because iteratingNoneraisedTypeError.tests/unit/test_empty_success_body.py, sync and async, for 204, empty 200 and JSONnull.Out of scope
ObserveResult/observe()is tracked in bug(python-sdk): observe() 2xx bodies with valid non-object JSON crash raw or fabricate an empty ObserveResult #498 and is not touched here.ping,branches.listandsnapshots.list(existing tests cover them), andbranches.create(already guarded).from_dictdoes not have them. Carrying them would need a change at the transport boundary. I can follow up if wanted.Verification
pytest tests/unit: 1078 passed.srcchanges stashed, and passes with them.ruff checkandmypy srcare clean.