Skip to content

Commit c8ccea9

Browse files
vudigapranavclaudeIwaniukooo11
authored
fix(client): split SSE lines only on CR, LF and CRLF (a2aproject#1456)
# Description `parse_sse_stream` read the stream with `response.aiter_lines()`, which follows `str.splitlines()` and also ends a line at U+2028, U+2029 and U+0085. The server writes these characters raw inside one `data:` line, so the client cut the event short and the payload failed to parse. As suggested in a2aproject#1432, the fix is in `parse_sse_stream`. Lines now come from `aiter_text()` and end only at CRLF, LF or CR, as the [SSE specification](https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream) requires. A CR at the end of a chunk is held back, so a CRLF split across two chunks still ends one line. Tests: - `test_parse_sse_stream_ends_lines_only_at_cr_or_lf` feeds real `httpx.Response` byte chunks through the parser: - the three separators inside `data:` - a CRLF and a U+2028 byte sequence each split across chunks - an event with no closing blank line, which is still not dispatched - Transport tests that mocked `aiter_lines()` now mock `aiter_text()`. `./scripts/lint.sh` and `uv run pytest` pass. Coverage on `http_helpers.py` is 93%. - [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) Fixes a2aproject#1432 🦕 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent 1ff86d5 commit c8ccea9

4 files changed

Lines changed: 109 additions & 25 deletions

File tree

‎src/a2a/client/transports/http_helpers.py‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import re
23

34
from collections.abc import AsyncGenerator, Callable, Iterator
45
from contextlib import contextmanager
@@ -65,6 +66,39 @@ async def send_http_request(
6566
return response.json()
6667

6768

69+
_SSE_LINE_END = re.compile(r'\r\n|\r|\n')
70+
71+
72+
async def _aiter_sse_lines(
73+
response: httpx.Response,
74+
) -> AsyncGenerator[str, None]:
75+
"""Yields lines ended only by CRLF, LF or CR, unlike `aiter_lines()`.
76+
77+
The unfinished line is kept as a list of pieces and joined once, and
78+
only the new chunk is scanned, so a long `data:` line that spans many
79+
chunks costs linear rather than quadratic time.
80+
"""
81+
pieces: list[str] = []
82+
held_cr = False
83+
async for chunk in response.aiter_text():
84+
text = '\r' + chunk if held_cr else chunk
85+
# Hold a trailing CR back: the next chunk may start with its LF.
86+
held_cr = text.endswith('\r')
87+
*lines, rest = _SSE_LINE_END.split(text[:-1] if held_cr else text)
88+
if lines:
89+
lines[0] = ''.join(pieces) + lines[0]
90+
pieces = []
91+
for line in lines:
92+
yield line
93+
if rest:
94+
pieces.append(rest)
95+
tail = ''.join(pieces)
96+
# A held CR at end of stream still ends a line, even an empty one. That
97+
# blank line is what dispatches the last event, so yield it too.
98+
if held_cr or tail:
99+
yield tail
100+
101+
68102
async def parse_sse_stream(
69103
response: httpx.Response,
70104
) -> AsyncGenerator[tuple[str, str], None]:
@@ -75,9 +109,7 @@ async def parse_sse_stream(
75109
event_name = 'message'
76110
payload_chunks: list[str] = []
77111

78-
async for line in response.aiter_lines():
79-
raw_line = line.rstrip('\r\n')
80-
112+
async for raw_line in _aiter_sse_lines(response):
81113
# Empty line denotes the completion of the current event block
82114
if not raw_line:
83115
if payload_chunks:

‎tests/client/transports/test_http_helpers.py‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections.abc import AsyncIterator
2+
13
import httpx
24
import pytest
35

@@ -21,7 +23,7 @@ def test_default_sse_error_handler():
2123

2224
@pytest.mark.asyncio
2325
async def test_parse_sse_stream_edge_cases():
24-
async def mock_aiter_lines():
26+
async def mock_aiter_text():
2527
yield ': comment line (should be ignored)\n'
2628
yield 'event: custom_event\n'
2729
yield 'data: hello\n'
@@ -32,7 +34,7 @@ async def mock_aiter_lines():
3234
yield '\n'
3335

3436
response = httpx.Response(200)
35-
response.aiter_lines = mock_aiter_lines # type: ignore
37+
response.aiter_text = mock_aiter_text # type: ignore
3638

3739
events = [e async for e in parse_sse_stream(response)]
3840
assert events == [
@@ -41,6 +43,60 @@ async def mock_aiter_lines():
4143
]
4244

4345

46+
async def _byte_chunks(chunks: list[bytes]) -> AsyncIterator[bytes]:
47+
for chunk in chunks:
48+
yield chunk
49+
50+
51+
@pytest.mark.asyncio
52+
@pytest.mark.parametrize(
53+
('chunks', 'expected'),
54+
[
55+
pytest.param(
56+
['data: a
b
c\x85d\n\n'.encode()],
57+
[('message', 'a
b
c\x85d')],
58+
id='unicode-line-separators-stay-in-data',
59+
),
60+
pytest.param(
61+
[b'data: a\xe2\x80', b'\xa8b\r', b'\ndata: c\r\r'],
62+
[('message', 'a
b\nc')],
63+
id='crlf-and-character-split-across-chunks',
64+
),
65+
pytest.param(
66+
[b'data: a\r'],
67+
[],
68+
id='event-without-blank-line-is-not-dispatched',
69+
),
70+
],
71+
)
72+
async def test_parse_sse_stream_ends_lines_only_at_cr_or_lf(
73+
chunks: list[bytes], expected: list[tuple[str, str]]
74+
) -> None:
75+
"""Only CRLF, LF and CR end a line, as the SSE specification requires."""
76+
response = httpx.Response(200, content=_byte_chunks(chunks))
77+
78+
events = [e async for e in parse_sse_stream(response)]
79+
80+
assert events == expected
81+
82+
83+
@pytest.mark.asyncio
84+
@pytest.mark.timeout(10)
85+
async def test_parse_sse_stream_long_data_line_over_many_chunks() -> None:
86+
"""A multi-megabyte data: line read in small chunks stays linear."""
87+
payload = 'x' * (2 * 1024 * 1024)
88+
body = f'data: {payload}\r\n\r\n'.encode()
89+
chunk_size = 512
90+
chunks = [body[i : i + chunk_size] for i in range(0, len(body), chunk_size)]
91+
response = httpx.Response(200, content=_byte_chunks(chunks))
92+
93+
# Re-joining and re-scanning the whole line on every chunk took tens of
94+
# seconds here; the linear version takes well under a second.
95+
events = [e async for e in parse_sse_stream(response)]
96+
97+
assert events == [('message', payload)]
98+
99+
44100
@pytest.mark.asyncio
45101
async def test_send_http_stream_request_non_sse(mocker):
46102
client = httpx.AsyncClient()

‎tests/client/transports/test_jsonrpc_client.py‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,8 @@ async def test_send_message_streaming_sse_error(
451451
mock_response = AsyncMock(spec=httpx.Response)
452452
mock_response.raise_for_status = MagicMock()
453453
mock_response.headers = {'content-type': 'text/event-stream'}
454-
mock_response.aiter_lines.return_value = async_iterable_from_list(
455-
[
456-
'event: error',
457-
'data: Simulated SSE error',
458-
'',
459-
]
454+
mock_response.aiter_text.return_value = async_iterable_from_list(
455+
['event: error\ndata: Simulated SSE error\n\n']
460456
)
461457
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
462458

@@ -475,7 +471,7 @@ async def test_send_message_streaming_request_error(
475471
mock_response = AsyncMock(spec=httpx.Response)
476472
mock_response.raise_for_status = MagicMock()
477473
mock_response.headers = {'content-type': 'text/event-stream'}
478-
mock_response.aiter_lines.side_effect = httpx.RequestError(
474+
mock_response.aiter_text.side_effect = httpx.RequestError(
479475
'Simulated request error', request=MagicMock()
480476
)
481477
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
@@ -495,9 +491,7 @@ async def test_send_message_streaming_timeout(
495491
mock_response = AsyncMock(spec=httpx.Response)
496492
mock_response.raise_for_status = MagicMock()
497493
mock_response.headers = {'content-type': 'text/event-stream'}
498-
mock_response.aiter_lines.side_effect = httpx.TimeoutException(
499-
'Timeout'
500-
)
494+
mock_response.aiter_text.side_effect = httpx.TimeoutException('Timeout')
501495
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
502496

503497
with pytest.raises(A2AClientError, match='timed out'):
@@ -581,7 +575,7 @@ async def test_send_message_streaming_server_error_propagates(
581575
request=httpx.Request('POST', 'http://test.url'),
582576
response=mock_response,
583577
)
584-
mock_response.aiter_lines.return_value = async_iterable_from_list([])
578+
mock_response.aiter_text.return_value = async_iterable_from_list([])
585579
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
586580

587581
with pytest.raises(A2AClientError) as exc_info:
@@ -813,8 +807,8 @@ async def test_send_message_streaming_ignores_unrecognized_fields(
813807
mock_response = AsyncMock(spec=httpx.Response)
814808
mock_response.raise_for_status = MagicMock()
815809
mock_response.headers = {'content-type': 'text/event-stream'}
816-
mock_response.aiter_lines.return_value = async_iterable_from_list(
817-
[f'data: {json.dumps(payload)}', '']
810+
mock_response.aiter_text.return_value = async_iterable_from_list(
811+
[f'data: {json.dumps(payload)}\n\n']
818812
)
819813
mock_sse_source.return_value.__aenter__.return_value = mock_response
820814

@@ -969,7 +963,9 @@ async def test_streaming_malformed_event_raises_a2a_error(
969963
mock_response = AsyncMock(spec=httpx.Response)
970964
mock_response.raise_for_status = MagicMock()
971965
mock_response.headers = {'content-type': 'text/event-stream'}
972-
mock_response.aiter_lines.return_value = async_iterable_from_list(lines)
966+
mock_response.aiter_text.return_value = async_iterable_from_list(
967+
[f'{line}\n' for line in lines]
968+
)
973969
mock_sse_source.return_value.__aenter__.return_value = mock_response
974970

975971
with pytest.raises(expected_error):

‎tests/client/transports/test_rest_client.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async def test_send_message_streaming_timeout(
109109
mock_response = AsyncMock(spec=httpx.Response)
110110
mock_response.headers = {'content-type': 'text/event-stream'}
111111
mock_response.raise_for_status.return_value = None
112-
mock_response.aiter_lines.side_effect = httpx.TimeoutException(
112+
mock_response.aiter_text.side_effect = httpx.TimeoutException(
113113
'Read timed out'
114114
)
115115
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
@@ -348,7 +348,7 @@ async def test_send_message_streaming_with_new_extensions(
348348
mock_response = AsyncMock(spec=httpx.Response)
349349
mock_response.headers = {'content-type': 'text/event-stream'}
350350
mock_response.raise_for_status.return_value = None
351-
mock_response.aiter_lines.return_value = async_iterable_from_list([])
351+
mock_response.aiter_text.return_value = async_iterable_from_list([])
352352
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
353353

354354
context = ClientCallContext(
@@ -398,7 +398,7 @@ async def test_send_message_streaming_server_error_propagates(
398398
request=httpx.Request('POST', 'http://test.url'),
399399
response=mock_response,
400400
)
401-
mock_response.aiter_lines.return_value = async_iterable_from_list([])
401+
mock_response.aiter_text.return_value = async_iterable_from_list([])
402402
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
403403

404404
with pytest.raises(A2AClientError) as exc_info:
@@ -752,7 +752,7 @@ async def test_rest_streaming_methods_prepend_tenant( # noqa: PLR0913
752752
mock_response = AsyncMock(spec=httpx.Response)
753753
mock_response.headers = {'content-type': 'text/event-stream'}
754754
mock_response.raise_for_status.return_value = None
755-
mock_response.aiter_lines.return_value = async_iterable_from_list([])
755+
mock_response.aiter_text.return_value = async_iterable_from_list([])
756756
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
757757

758758
# 3. Call the method
@@ -905,8 +905,8 @@ async def test_send_message_streaming_ignores_unrecognized_fields(
905905
mock_response = AsyncMock(spec=httpx.Response)
906906
mock_response.raise_for_status = MagicMock()
907907
mock_response.headers = {'content-type': 'text/event-stream'}
908-
mock_response.aiter_lines.return_value = async_iterable_from_list(
909-
[f'data: {json.dumps(payload)}', '']
908+
mock_response.aiter_text.return_value = async_iterable_from_list(
909+
[f'data: {json.dumps(payload)}\n\n']
910910
)
911911
mock_sse_source.return_value.__aenter__.return_value = mock_response
912912

0 commit comments

Comments
 (0)