Repository navigation
Commit 8c48ad3
fix(client): raise stream errors sent as plain SSE data events (a2aproject#1444)
# Description
`RestTransport` only treats an SSE event named `error` as an error. A
stream error sent as an ordinary `data:` event, with the
`google.rpc.Status` body from section 11.6, goes to `parse_lenient`.
That drops the unknown `error` key, so the caller gets an empty
`StreamResponse` and no exception. Before a2aproject#1354 the strict `Parse` at
least raised a `ParseError`.
Section 11.7 only says the `data` field carries a `StreamResponse` and
does not name an event type for errors, so SDK servers differ:
- a2a-js and this SDK send `event: error`.
- a2a-go sends a plain `data:` event, in
[`handleStreamingRequest`](https://github.com/a2aproject/a2a-go/blob/ea739de3859f0574175bc9876eda39b7120da8e3/a2asrv/rest.go#L332-L347)
through `sseWriter.WriteData`. It does this for errors in the middle of
a stream, and for early ones too when the request's `Accept` header
lists no JSON type. The Python client always sends `Accept:
text/event-stream`. a2a-go's own client reads the error from the `error`
key of the data object
([`ParseStreamResponse`](https://github.com/a2aproject/a2a-go/blob/ea739de3859f0574175bc9876eda39b7120da8e3/internal/rest/rest.go#L375-L400)).
So a Python client streaming from an a2a-go agent reads a
`TaskNotFoundError`, or any other mid-stream failure, as an empty event
and keeps going.
When a data event parses to no payload, `RestTransport` now checks it
for an `error` key and raises through the same `_handle_sse_error` that
handles `event: error`. `JsonRpcTransport` already raises for in-band
errors the same way. Normal events are not decoded a second time. An
event with no known payload and no `error` key is still yielded as
before, so the forward compatibility from a2aproject#1354 stays.
The repro at the end of this description (a mock server sending one
plain `data:` error event), on main (2cc1dd5):
```
event: None 0
stream ended without an error
```
On this branch:
```
raised TaskNotFoundError: task not found: t1
```
```
$ pytest tests/client/transports/test_rest_client.py # main with the new tests
FAILED tests/client/transports/test_rest_client.py::TestRestStreamErrors::test_stream_error_raises_mapped_error[data_event]
1 failed, 43 passed
$ pytest --ignore=tests/integration/cross_version # this branch, Python 3.10, locked deps
2558 passed, 207 skipped, 4 xfailed, 1 xpassed
$ pytest --cov=src
src/a2a/client/transports/rest.py 87% (the new lines are covered)
TOTAL 94%
```
The `error_event` case and the unknown-event case pass on main as well.
They pin the existing `event: error` path, which had no REST client
test, and the lenient handling of unknown events. The cross-version
tests are left out because their servers don't start within the 3 s
timeout on my machine; the same happens on main. `ruff check`, `ruff
format` and `ty check` pass, with only the three existing unused `type:
ignore` warnings from `ty`.
- [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)
No issue exists, since the new test reproduces it.
<details><summary>Repro script</summary>
```python
import asyncio
import json
import httpx
from a2a.client.transports.rest import RestTransport
from a2a.types.a2a_pb2 import AgentCard, SubscribeToTaskRequest
# The google.rpc.Status body a2a-go writes as a plain `data:` event.
ERROR = {
'error': {
'code': 404,
'status': 'NOT_FOUND',
'message': 'task not found: t1',
'details': [
{
'@type': 'type.googleapis.com/google.rpc.ErrorInfo',
'reason': 'TASK_NOT_FOUND',
'domain': 'a2a-protocol.org',
}
],
}
}
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={'content-type': 'text/event-stream'},
content=f'id: 1\ndata: {json.dumps(ERROR)}\n\n'.encode(),
)
async def main() -> None:
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http:
transport = RestTransport(http, AgentCard(), 'http://agent.example')
try:
async for event in transport.subscribe(SubscribeToTaskRequest(id='t1')):
print('event:', event.WhichOneof('payload'), event.ByteSize())
print('stream ended without an error')
except Exception as e:
print(f'raised {type(e).__name__}: {e}')
asyncio.run(main())
```
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Mykyta Netipa <mykytanetipa@google.com>1 parent c8ccea9 commit 8c48ad3
2 files changed
Lines changed: 97 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
407 | 407 | | |
408 | 408 | | |
409 | 409 | | |
| 410 | + | |
| 411 | + | |
410 | 412 | | |
411 | 413 | | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
412 | 426 | | |
413 | 427 | | |
414 | 428 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
945 | 945 | | |
946 | 946 | | |
947 | 947 | | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
0 commit comments