Repository navigation
fix(client): split SSE lines only on CR, LF and CRLF - #1456
Conversation
parse_sse_stream read the stream with httpx's aiter_lines(), which follows str.splitlines() and also ends a line at U+2028, U+2029 and U+0085. The server writes those characters raw inside a data: line, so the client cut the event short and the JSON-RPC payload failed to parse. Lines now come from aiter_text() and end only at CRLF, LF or CR, as the SSE specification requires. A CR at the end of a chunk is held back so a CRLF split across chunks still ends a single line. Transport tests that mocked aiter_lines() now mock aiter_text(). Fixes a2aproject#1432 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/client/transports/http_helpers.py | 92.13% | 93.51% | 🟢 +1.38% |
| Total | 93.60% | 93.62% | 🟢 +0.02% |
Generated by coverage-comment.yml
1AbdullahButt
left a comment
There was a problem hiding this comment.
The splitting is correct. One cost issue, though, on long data: lines.
What I checked in _aiter_sse_lines (http_helpers.py:72-87).
- A CRLF split across chunks (
"x\r"+"\ny") gives one linex. - A lone CR followed by non-LF text still ends the line.
"a\r\r"+"\n"givesaand then an empty line.- A stream ending in
"...\n\r"yields a final"", so the last event is dispatched. That's right, since the CR terminates the empty line. - An unterminated last line is yielded but never dispatched, because
parse_sse_streamonly dispatches on an empty line. aiter_text()uses the same decoder asaiter_lines()did, so charset handling doesn't change.
Long lines are re-copied and re-scanned on every chunk. text = pending + chunk followed by _SSE_LINE_END.split(text) means a line that spans N chunks costs about N²/2 chunk-lengths of copying and regex scanning. A2A messages put whole file parts (base64 bytes) into a single data: line. So a 20 MB artifact read in 64 KB chunks is around 300 chunks, and about 3 GB of copying for that one line. httpx's own LineDecoder, which aiter_lines() used, avoids this by buffering the pieces in a list and joining them once (httpx _decoders.py, LineDecoder.decode). The same shape works here, and it only scans the new chunk:
async def _aiter_sse_lines(response: httpx.Response) -> AsyncGenerator[str, None]:
buffer: list[str] = []
held_cr = False
async for chunk in response.aiter_text():
if held_cr:
chunk, held_cr = '\r' + chunk, False
if chunk.endswith('\r'):
chunk, held_cr = chunk[:-1], True
*lines, rest = _SSE_LINE_END.split(chunk)
if lines:
lines[0] = ''.join(buffer) + lines[0]
buffer = []
for line in lines:
yield line
if rest:
buffer.append(rest)
tail = ''.join(buffer)
if held_cr or tail:
yield tailI checked this version with a standalone script of my own (not repo code). It ran 20,000 random strings over a b \r \n U+2028 : space, cut at random chunk boundaries. Its output matched re.split(r'\r\n|\r|\n') on the joined text (terminated lines, plus a non-empty remainder) every time. A test with one multi-megabyte data: line over many small chunks would guard against the slowdown coming back.
Other notes:
- #1259 (leading BOM) also edits the
aiter_lines()loop inparse_sse_stream, andgit merge-treeshows the two PRs conflict inhttp_helpers.py. Whichever lands second will need to move theremoveprefix('')onto the first line from_aiter_sse_lines. git merge-treeagainstmainis clean.
I didn't run the repo's tests.
Reviewed with help from Claude Code.
_aiter_sse_lines prepended the unfinished line to every new chunk and re-ran the line-ending regex over the whole thing. A data: line that spans N chunks therefore cost about N^2/2 chunk-lengths of copying and scanning. A2A puts whole file parts into one data: line, so a large artifact read in small chunks got very slow. The unfinished line is now kept as a list of pieces and joined once when its line ending arrives, and only the new chunk is scanned, as httpx's own LineDecoder does. A trailing CR is still held back so a CRLF split across chunks ends a single line. Added a test that reads a 2 MB data: line in 512-byte chunks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, good catch on the long-line cost. Done in afb63ba:
Verified with Noted on #1259: whichever lands second will need to move the BOM strip onto the first line from Fix and reply prepared with Claude Code, on Pranav's behalf. |
The long data: line test timed itself with perf_counter and asserted under 5 seconds. It now uses @pytest.mark.timeout(10), as the reviewer asked, which the repo already depends on via pytest-timeout. Also added a comment on why _aiter_sse_lines still yields an empty tail when the stream ends on a CR: that CR ends a blank line, and the blank line is what dispatches the last event. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @Iwaniukooo11, both done in 16be92b:
Verified with Fix and reply prepared with Claude Code, on Pranav's behalf. |
Resolve the conflict in test_rest_client.py and switch the new stream error tests to mock aiter_text, since a2aproject#1456 changed the SSE reader from aiter_lines to aiter_text.
🤖 I have created a release *beep* *boop* --- ## [1.3.1](v1.3.0...v1.3.1) (2026-11-16) ### Features * add httpx2 support ([#1439](#1439)) ([a59bb68](a59bb68)) * **cluster:** Introduce ServerCallContext to DatabaseTaskEventStream ([#1399](#1399)) ([6168d59](6168d59)) ### Bug Fixes * **client:** handle case-insensitive SSE media types ([#1464](#1464)) ([3932982](3932982)) * **client:** keep tenant defaults local to each call ([#1463](#1463)) ([569b94f](569b94f)) * **client:** percent-encode ids in REST transport paths ([#1436](#1436)) ([1bbe69c](1bbe69c)) * **client:** split SSE lines only on CR, LF and CRLF ([#1456](#1456)) ([c8ccea9](c8ccea9)) * **compat:** get a v0.3 push config by task id alone ([#1380](#1380)) ([aa8e966](aa8e966)) * **compat:** list push notification configs in the v0.3 REST client ([#1424](#1424)) ([d830036](d830036)), closes [#1423](#1423) * **compat:** map v0.3 gRPC errors from their ErrorInfo reason ([#1466](#1466)) ([caab6a2](caab6a2)) * **compat:** parse the v0.3 REST extended agent card as JSON ([#1420](#1420)) ([076e0ec](076e0ec)) * **compat:** percent-encode ids in v0.3 REST transport paths ([#1458](#1458)) ([1ff86d5](1ff86d5)) * **compat:** return -32602 for invalid v0.3 JSON-RPC params ([#1419](#1419)) ([05aaff6](05aaff6)) * **compat:** return 400 for a non-integer v0.3 REST historyLength ([#1382](#1382)) ([9c90c48](9c90c48)), closes [#1381](#1381) * **compat:** return HTTP errors from v0.3 REST streams that fail early ([#1403](#1403)) ([11f1116](11f1116)) * **compat:** send no v0.3 configuration when the request has none ([#1429](#1429)) ([01483cb](01483cb)) * **compat:** send v0.3 REST request bodies with json_name keys ([#1455](#1455)) ([2399993](2399993)) * **compat:** serve DELETE for v0.3 REST push notification configs ([#1442](#1442)) ([14e799c](14e799c)) * reject decoded values that are not objects in parse_lenient ([#1431](#1431)) ([257626b](257626b)) * reject invalid REST boolean query values ([#1383](#1383)) ([babc3f5](babc3f5)) * **server:** close the event queues when ActiveTask.start() fails ([#1422](#1422)) ([5e0d415](5e0d415)) * **server:** create the cluster tables lazily in VersionedDatabaseTaskStore get and delete ([#1357](#1357)) ([4e133b7](4e133b7)) * **server:** fail fast when an executor produces no events ([#1345](#1345)) ([ab7e36f](ab7e36f)) * **server:** fail streaming sends when an executor produces no events ([#1460](#1460)) ([c16cab1](c16cab1)) * **server:** forward CancelTaskRequest.metadata to AgentExecutor.cancel ([#1416](#1416)) ([f666ba3](f666ba3)), closes [#1415](#1415) * **server:** infer contextId from task when only taskId is provided ([#1453](#1453)) ([2ba8e7b](2ba8e7b)), closes [#1411](#1411) * **server:** match input modes on type and subtype, ignoring case ([#1425](#1425)) ([28f8a62](28f8a62)) * **server:** prevent failed cancel from overwriting terminal task state ([#1405](#1405)) ([ac48ce1](ac48ce1)) * **server:** record FAILED when a task's terminal save fails ([#1400](#1400)) ([4e916d8](4e916d8)) * **server:** reject out-of-range ListTasks cursor timestamps ([#1451](#1451)) ([f451ed7](f451ed7)) * **server:** run after_handler_run when a handler call is cancelled ([#1427](#1427)) ([2cc1dd5](2cc1dd5)) * **server:** treat request bodies that are not valid UTF-8 as parse errors ([#1401](#1401)) ([56ec855](56ec855)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Description
parse_sse_streamread the stream withresponse.aiter_lines(), which followsstr.splitlines()and also ends a line at U+2028, U+2029 and U+0085. The server writes these characters raw inside onedata:line, so the client cut the event short and the payload failed to parse.As suggested in #1432, the fix is in
parse_sse_stream. Lines now come fromaiter_text()and end only at CRLF, LF or CR, as the SSE specification 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_lffeeds realhttpx.Responsebyte chunks through the parser:data:aiter_lines()now mockaiter_text()../scripts/lint.shanduv run pytestpass. Coverage onhttp_helpers.pyis 93%.CONTRIBUTINGGuide.bash scripts/format.shfrom the repository root to format)Fixes #1432 🦕