Repository navigation
fix(python-sdk): validate observe response shape - #503
loveRhythm1990 merged 2 commits into
Conversation
observe() and observe_deduplicated() passed the 2xx body to
ObserveResult.from_dict(data or {}). Arrays, strings and numbers raised a raw
AttributeError, and falsy or empty bodies were turned into a fabricated empty
result.
ObserveResult.from_dict now requires a JSON object and raises
MemoriaResponseError otherwise. An explicit null memories field is read as an
empty list, and a non-array memories field is rejected.
|
This pull request is merged. |
loveRhythm1990
left a comment
There was a problem hiding this comment.
LGTM for the code at b47f54d.
I independently checked all four changed files, all four observe call sites, and the server's shared observe response construction. Removing data or {} prevents falsy malformed responses from masquerading as successful empty extraction. The object/array checks fix the reported exceptions while retaining the explicitly requested null-memories compatibility.
The new tests produce 44 failures on base; all 1,055 SDK unit tests pass on this head. Eight additional sync/async wire-body checks pass, including literal JSON null.
Non-blocking test correction: httpx.Response(200, json=None) produces an empty body, so json-null currently duplicates empty-200. Use content=b"null" with application/json. I checked the literal-null path separately and it correctly raises MemoriaResponseError.
Integration note: the source changes compose with #502, but the two heads conflict in sdk/python/CHANGELOG.md. Whichever lands second needs that conflict resolved while preserving both entries; the PR description's "either order" should not imply a conflict-free merge.
Coverage: 4/4 changed files reviewed, 100%, none skipped. Current CI is green and there is no conflict with current main.
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.
|
Addressed in the latest push: the |
loveRhythm1990
left a comment
There was a problem hiding this comment.
LGTM at 4e1db47.
The literal-null fixture and changelog conflict are addressed. I reviewed all four current changed files and confirmed the observe response validation remains correct for sync/async calls. All 1,055 SDK unit tests pass on this head.
A local trial merge with #502's current head is conflict-free, preserves both changelog entries, and passes all 1,125 SDK unit tests. No additional findings.
Coverage: 4/4 changed files reviewed, 100%, none skipped. The head has no conflict with current main. DB Tests are still running; this is a code LGTM, pending completion of CI.
Fixes #498
Problem
observe()andobserve_deduplicated()(sync and async, 4 call sites inclient.py) passed the 2xx body toObserveResult.from_dict(data or {}):["mem_1"],"text",42raised a rawAttributeErrorfrom.get().[],false,null, 204 and an empty 200 were collapsed to{}bydata or {}, so the SDK reported "no memories extracted".{"memories": null}raisedTypeError: 'NoneType' object is not iterable.Change
client.py: passdatastraight toObserveResult.from_dict, removingdata or {}.models.py:ObserveResult.from_dictrequires a JSON object and raisesMemoriaResponseErrorotherwise. An explicit"memories": nullis read as[]. Amemoriesvalue that is neither an array nor null (for example a string, whichlist()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 nullmemoriescase and valid bodies.The server's
/v1/observehandlers always return an object with amemoriesarray, 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
MemoriaResponseErrorfor empty bodies. This PR does not use the_require_objecthelper that #502 adds, so the code does not depend on it.The two PRs touch
sdk/python/CHANGELOG.mdandmodels.py. This PR's changelog entry sits at the end of theFixedsection, 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 switchObserveResult.from_dictto the shared helper.Verification
pytest tests/unit: 1055 passed.json-nulltest case now sends a literalnullbody (httpx.Response(200, json=None)sends an empty one).srcchanges stashed, the new test file has 44 failures. The 3 valid-body cases pass either way.ruff checkandmypy srcare clean.