Skip to content

fix(python-sdk): raise MemoriaResponseError for empty 2xx bodies - #502

Merged
loveRhythm1990 merged 2 commits into
matrixorigin:mainfrom
loveRhythm1990:fix/python-sdk-empty-2xx-body
Nov 17, 2026
Merged

loveRhythm1990 merged 2 commits into
matrixorigin:mainfrom
loveRhythm1990:fix/python-sdk-empty-2xx-body

Conversation

@loveRhythm1990

Copy link
Copy Markdown
Collaborator

Fixes #499

Problem

A 2xx response with an empty body (204, zero-length 200, or JSON null) is folded into None by the transport. The resource modules pass it to Model.from_dict(data), which calls .get() and raises a raw AttributeError: 'NoneType' object has no attribute 'get'. That escapes the documented MemoriaError hierarchy and bypasses callers' except MemoriaError handlers.

Change

  • models.py: add _require_object() and call it at the top of from_dict for Memory, RetrieveResult (dict form), PurgeResult, Snapshot, Branch, ApplyResult, Profile and GovernanceResult. A non-object body raises MemoriaResponseError, the same exception MemoryPage already uses.
  • resources/memories.py: store_batch (sync and async) requires a JSON array, because iterating None raised TypeError.
  • Regression tests in tests/unit/test_empty_success_body.py, sync and async, for 204, empty 200 and JSON null.
  • CHANGELOG entry.

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.
  • Endpoints that deliberately accept an empty body keep their behavior: ping, branches.list and snapshots.list (existing tests cover them), and branches.create (already guarded).
  • The error message names the endpoint family and the received type, but does not include the HTTP status or URL. from_dict does 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.
  • The new test file has 69 failures with the src changes stashed, and passes with them.
  • ruff check and mypy src are clean.

…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.
@mergify

mergify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@loveRhythm1990 loveRhythm1990 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

loveRhythm1990 added a commit to loveRhythm1990/Memoria that referenced this pull request Nov 17, 2026
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

Copy link
Copy Markdown
Collaborator Author

Addressed in the latest push: the json-null case now sends a literal null body with a JSON content type, so it no longer duplicates empty-200. A trial merge of this head with #503 is conflict-free (the #503 changelog entry moved to the end of the Fixed section).

@loveRhythm1990 loveRhythm1990 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

loveRhythm1990 added a commit that referenced this pull request Nov 17, 2026
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.
@loveRhythm1990
loveRhythm1990 merged commit 3a58488 into matrixorigin:main Nov 17, 2026
6 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.

bug(python-sdk): empty 2xx bodies (204 / empty 200 / JSON null) raise raw AttributeError across resource parsers

2 participants