Skip to content

fix(client): convert malformed body decode errors to A2AClientError - #1523

Closed
feiiiiii5 wants to merge 2 commits into
a2aproject:mainfrom
feiiiiii5:feiiiiii5/D263
Closed

feiiiiii5 wants to merge 2 commits into
a2aproject:mainfrom
feiiiiii5:feiiiiii5/D263

Conversation

@feiiiiii5

@feiiiiii5 feiiiiii5 commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

Unary client transports leaked builtin UnicodeDecodeError / RecursionError when a remote response body was invalid UTF-8 or deeply nested JSON. handle_http_exceptions() caught only json.JSONDecodeError, which is a different ValueError subclass, so those failures escaped instead of becoming the documented A2AClientError. This affected every unary JsonRpcTransport and RestTransport call.

Fix: catch (json.JSONDecodeError, UnicodeDecodeError, RecursionError) explicitly and raise A2AClientError("Malformed JSON response: ...") from e, preserving __cause__. The block still lets other ValueErrors from the status error handlers propagate, so the catch was not broadened to bare ValueError.

Verification

  • New MockTransport regression test parametrized over invalid UTF-8 and 10k-deep nested bodies. Base-red: 2 failed with the leaked builtin exceptions; with fix the module is 18/18, all transports 205/205, all client tests 331/331.
  • ruff check, ruff format --check and ty check on the changed file all pass. Full-repo ty has 11 pre-existing diagnostics in unrelated files.

No live server or network used; streaming path and httpx2 alternate client not exercised.

Related to #1478 (same failure class on a different surface; no separate issue was filed for this helper). 🦕

@feiiiiii5
feiiiiii5 requested a review from a team as a code owner November 25, 2026 13:25
@github-actions

Copy link
Copy Markdown

🧪 Code Coverage (vs main)

⬇️ Download Full Report

No coverage changes.

Generated by coverage-comment.yml

@Iwaniukooo11 Iwaniukooo11 self-assigned this Nov 26, 2026
@Iwaniukooo11

Copy link
Copy Markdown
Member

Thanks for the PR, but we are closing this without merging:

  • The PR description says it tests 10k-deep nested JSON (RecursionError), but that test was removed in the second commit after CI failed on Python 3.14, so RecursionError is not tested.

  • Changing JSON Decode Error to Malformed JSON response unnecessarily changes the existing error message. Catching UnicodeDecodeError in handle_http_exceptions also mislabels non-JSON stream decoding in send_http_stream_request as a JSON error.

Iwaniukooo11 added a commit that referenced this pull request Nov 30, 2026
…ot decode (#1540)

# Description

`get_agent_card()` now raises `AgentCardResolutionError` for a card body
that `response.json()` rejects with `UnicodeDecodeError` (not UTF-8) or
`RecursionError` (deep nesting), with the same message as a
`JSONDecodeError`.

The Latin-1 test decodes a real body. The recursion test raises
`RecursionError` from `response.json()` directly. On Python 3.14 the
stack size sets the recursion limit, so no fixed depth reaches it on
every runner. #1523 lost its `RecursionError` test to the same problem.

```console
$ uv run pytest tests/client/test_card_resolver.py   # 145015f, card_resolver.py from 211d13a, new tests kept
FAILED tests/client/test_card_resolver.py::TestGetAgentCard::test_get_agent_card_undecodable_body
FAILED tests/client/test_card_resolver.py::TestGetAgentCard::test_get_agent_card_recursion_error
2 failed, 47 passed, 1 warning
$ uv run pytest tests/client/test_card_resolver.py   # 145015f, Python 3.10.21 and 3.14.7
49 passed, 1 warning
$ uv run pytest -n 4 --cov=a2a --cov-report term --cov-fail-under=88   # 145015f, Python 3.10.21
Required test coverage of 88% reached. Total coverage: 94.12%
2810 passed, 215 skipped, 4 xfailed, 1 xpassed, 219 warnings
$ uv run ruff check && uv run ruff format --check && uv run ty check   # ruff 0.15.16
All checks passed!
296 files already formatted
Found 3 diagnostics   # the 3 unused type: ignore warnings already on main, in files this does not touch
# not run: the Postgres and MySQL suites (no DSN set here)
```

@rohityan this follows your note on #1478: the three exceptions are
caught around `response.json()` only, so a `JSONDecodeError` from a
`signature_verifier` now propagates where it used to read "Failed to
parse JSON for agent card".

- [x] Follow the [`CONTRIBUTING`
Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md).
- [x] Make your Pull Request title in the
<https://www.conventionalcommits.org/> specification.
- [x] Ensure the tests and linter pass (Run `bash scripts/format.sh`
from the repository root to format)
- [x] Appropriate docs were updated (if necessary)
  - None needed: the docstring already promises this error.

Fixes #1478

---------

Signed-off-by: Christopher Krah <[email protected]>
Co-authored-by: Mateusz Iwaniuk <[email protected]>
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.

2 participants