Repository navigation
fix(compat): raise A2A errors for malformed v0.3 JSON-RPC responses - #1435
ShivamSharma43 wants to merge 3 commits into
Conversation
CompatJsonRpcTransport parsed every unary response with the json-rpc package's JSONRPC20Response(**response_data). That package validates the envelope with its own exceptions, so an error object with an extra member or without a message, an error that is not an object, a body with neither result nor error, or a body that is not an object reached the caller as TypeError or ValueError instead of the mapped A2A error or A2AClientError. Check the response with a _get_jsonrpc_result helper instead, as JsonRpcTransport does since a2aproject#1375. Delete still accepts a body with neither result nor error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/compat/v0_3/jsonrpc_transport.py | 73.62% | 79.63% | 🟢 +6.01% |
| Total | 93.59% | 93.75% | 🟢 +0.16% |
Generated by coverage-comment.yml
| f'{error!r}' | ||
| ) | ||
| raise self._create_jsonrpc_error(error) | ||
| if result_required and 'result' not in response_data: |
There was a problem hiding this comment.
response_data can have result : None, please fix
There was a problem hiding this comment.
Fixed in 21f43e4. A missing or null result now raises A2AClientError instead of reaching model_validate. The one exception is tasks/pushNotificationConfig/delete, because its v0.3 success response has result: null (DeleteTaskPushNotificationConfigSuccessResponse, and jsonrpc_adapter.py sends exactly that). It opts in with allow_null_result=True. This also changes message/send with a null result: it used to return an empty SendMessageResponse and now raises.
| ) -> Any: | ||
| """Returns the result of a JSON-RPC success response, raising for anything else. | ||
|
|
||
| An error response raises the A2A error mapped from its code, even when |
There was a problem hiding this comment.
Please shorten the docstring to max 2 short informative sentences
| f'{error!r}' | ||
| ) | ||
| raise self._create_jsonrpc_error(error) | ||
| if result_required and 'result' not in response_data: |
There was a problem hiding this comment.
And please include such case in tests
There was a problem hiding this comment.
Added in 21f43e4. test_compat_jsonrpc_transport_null_result covers result: null for send_message, cancel_task, the create/get/list push config methods and get_extended_agent_card. The malformed-response cases for get_task include it too. test_compat_jsonrpc_transport_delete_push_config_null_result checks that delete accepts both a null and a missing result.
|
|
||
| return A2AClientError(message) | ||
|
|
||
| def _get_jsonrpc_result( |
There was a problem hiding this comment.
It's worth running it in once in _send_request instead of calling it few times in separate methods, DRY
There was a problem hiding this comment.
Done in 21f43e4. _send_request now runs _check_jsonrpc_response once and returns the checked response, so the methods just read response_data['result']. This matches how JsonRpcTransport._send_request does it.
…med-response # Conflicts: # src/a2a/compat/v0_3/jsonrpc_transport.py # tests/compat/v0_3/test_jsonrpc_transport.py
Check the response envelope once in _send_request instead of in every method. A missing or null result now raises A2AClientError instead of reaching model_validate, except for tasks/pushNotificationConfig/delete, whose v0.3 success response carries a null result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Description
CompatJsonRpcTransportturns every unary response into ajson-rpcJSONRPC20Response(**response_data). That package validates the envelope with its own exceptions, so some responses reach the caller as a rawTypeErrororValueErrorinstead of an A2A error. #1375 fixed this inJsonRpcTransport. The v0.3 transport still has it.Results of
get_taskagainst these response bodies:{"code": -32001, "message": "...", "reason": "x"}TypeError: JSONRPCError.__init__() got an unexpected keyword argument 'reason'TaskNotFoundErrormessage, e.g.{"code": -32001}ValueError: Error message should be stringTaskNotFoundError"error": "boom"TypeError: ... argument after ** must be a mapping, not strA2AClientErrorresultnorerrorValueError: Either result or error should be usedA2AClientErrorTypeError: ... argument after ** must be a mapping, not listA2AClientErrorIn the first two rows the server sent a valid A2A error code.
_create_jsonrpc_erroralready handles a missingmessage, but thejson-rpcvalidation failed before it ran.Fix
All in
src/a2a/compat/v0_3/jsonrpc_transport.py:_get_jsonrpc_result, with the same rules asJsonRpcTransport._check_jsonrpc_response. Anerrorobject raises the mapped A2A error through the existing_create_jsonrpc_error. A body that is not an object, anerrorthat is not an object, or a body with neitherresultnorerrorraisesA2AClientError. Otherwise it returnsresult.JSONRPC20Response. Well-formed success and error responses behave as before.delete_task_push_notification_configpassesresult_required=False, so it still accepts a body with neitherresultnorerror, as it did before.The streaming path does not use
JSONRPC20Responseand is not changed.Tests
test_compat_jsonrpc_transport_malformed_responserunsget_taskagainst each body in the table. All five cases fail on main with the exceptions listed above and pass with the fix.test_compat_jsonrpc_transport_delete_push_config_without_resultchecks that delete still accepts a body with neither member. It passes before and after.uv run pytest tests/compat/v0_3: 329 passedbash scripts/lint.sh: success. The 3 ruff diagnostics it prints are inclient_factory.pyandtransports/__init__.py, which this change does not touch.uv run pyteston Windows, Python 3.11, skippingtests/integration/cross_versionandtests/integration/push_notificationsbecause they needos.killpgand fork-based multiprocessing: 2554 passed, 207 skipped, 4 xfailed, 1 xpassed (withPYTHONUTF8=1, whichtest_samples_smoke.pyneeds on a cp1252 console)Follow the
CONTRIBUTINGGuide.Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
Ensure the tests and linter pass (Run
bash scripts/format.shfrom the repository root to format)Appropriate docs were updated (if necessary)
🤖 Generated with Claude Code