Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/a2a/client/transports/http_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re

from collections.abc import AsyncGenerator, Callable, Iterator
from contextlib import contextmanager
Expand Down Expand Up @@ -65,6 +66,39 @@ async def send_http_request(
return response.json()


_SSE_LINE_END = re.compile(r'\r\n|\r|\n')


async def _aiter_sse_lines(
response: httpx.Response,
) -> AsyncGenerator[str, None]:
"""Yields lines ended only by CRLF, LF or CR, unlike `aiter_lines()`.

The unfinished line is kept as a list of pieces and joined once, and
only the new chunk is scanned, so a long `data:` line that spans many
chunks costs linear rather than quadratic time.
"""
pieces: list[str] = []
held_cr = False
async for chunk in response.aiter_text():
text = '\r' + chunk if held_cr else chunk
# Hold a trailing CR back: the next chunk may start with its LF.
held_cr = text.endswith('\r')
*lines, rest = _SSE_LINE_END.split(text[:-1] if held_cr else text)
if lines:
lines[0] = ''.join(pieces) + lines[0]
pieces = []
for line in lines:
yield line
if rest:
pieces.append(rest)
tail = ''.join(pieces)
# A held CR at end of stream still ends a line, even an empty one. That
# blank line is what dispatches the last event, so yield it too.
if held_cr or tail:
Comment thread
Iwaniukooo11 marked this conversation as resolved.
Resolved
yield tail


async def parse_sse_stream(
response: httpx.Response,
) -> AsyncGenerator[tuple[str, str], None]:
Expand All @@ -75,9 +109,7 @@ async def parse_sse_stream(
event_name = 'message'
payload_chunks: list[str] = []

async for line in response.aiter_lines():
raw_line = line.rstrip('\r\n')

async for raw_line in _aiter_sse_lines(response):
# Empty line denotes the completion of the current event block
if not raw_line:
if payload_chunks:
Expand Down
60 changes: 58 additions & 2 deletions tests/client/transports/test_http_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import AsyncIterator

import httpx
import pytest

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

@pytest.mark.asyncio
async def test_parse_sse_stream_edge_cases():
async def mock_aiter_lines():
async def mock_aiter_text():
yield ': comment line (should be ignored)\n'
yield 'event: custom_event\n'
yield 'data: hello\n'
Expand All @@ -32,7 +34,7 @@ async def mock_aiter_lines():
yield '\n'

response = httpx.Response(200)
response.aiter_lines = mock_aiter_lines # type: ignore
response.aiter_text = mock_aiter_text # type: ignore

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


async def _byte_chunks(chunks: list[bytes]) -> AsyncIterator[bytes]:
for chunk in chunks:
yield chunk


@pytest.mark.asyncio
@pytest.mark.parametrize(
('chunks', 'expected'),
[
pytest.param(
['data: a
b
c\x85d\n\n'.encode()],
[('message', 'a
b
c\x85d')],
id='unicode-line-separators-stay-in-data',
),
pytest.param(
[b'data: a\xe2\x80', b'\xa8b\r', b'\ndata: c\r\r'],
[('message', 'a
b\nc')],
id='crlf-and-character-split-across-chunks',
),
pytest.param(
[b'data: a\r'],
[],
id='event-without-blank-line-is-not-dispatched',
),
],
)
async def test_parse_sse_stream_ends_lines_only_at_cr_or_lf(
chunks: list[bytes], expected: list[tuple[str, str]]
) -> None:
"""Only CRLF, LF and CR end a line, as the SSE specification requires."""
response = httpx.Response(200, content=_byte_chunks(chunks))

events = [e async for e in parse_sse_stream(response)]

assert events == expected


@pytest.mark.asyncio
@pytest.mark.timeout(10)
async def test_parse_sse_stream_long_data_line_over_many_chunks() -> None:
"""A multi-megabyte data: line read in small chunks stays linear."""
payload = 'x' * (2 * 1024 * 1024)
body = f'data: {payload}\r\n\r\n'.encode()
chunk_size = 512
chunks = [body[i : i + chunk_size] for i in range(0, len(body), chunk_size)]
response = httpx.Response(200, content=_byte_chunks(chunks))

# Re-joining and re-scanning the whole line on every chunk took tens of
# seconds here; the linear version takes well under a second.
events = [e async for e in parse_sse_stream(response)]

assert events == [('message', payload)]


@pytest.mark.asyncio
async def test_send_http_stream_request_non_sse(mocker):
client = httpx.AsyncClient()
Expand Down
24 changes: 10 additions & 14 deletions tests/client/transports/test_jsonrpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,8 @@ async def test_send_message_streaming_sse_error(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.raise_for_status = MagicMock()
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.aiter_lines.return_value = async_iterable_from_list(
[
'event: error',
'data: Simulated SSE error',
'',
]
mock_response.aiter_text.return_value = async_iterable_from_list(
['event: error\ndata: Simulated SSE error\n\n']
)
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response

Expand All @@ -475,7 +471,7 @@ async def test_send_message_streaming_request_error(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.raise_for_status = MagicMock()
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.aiter_lines.side_effect = httpx.RequestError(
mock_response.aiter_text.side_effect = httpx.RequestError(
'Simulated request error', request=MagicMock()
)
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
Expand All @@ -495,9 +491,7 @@ async def test_send_message_streaming_timeout(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.raise_for_status = MagicMock()
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.aiter_lines.side_effect = httpx.TimeoutException(
'Timeout'
)
mock_response.aiter_text.side_effect = httpx.TimeoutException('Timeout')
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response

with pytest.raises(A2AClientError, match='timed out'):
Expand Down Expand Up @@ -581,7 +575,7 @@ async def test_send_message_streaming_server_error_propagates(
request=httpx.Request('POST', 'http://test.url'),
response=mock_response,
)
mock_response.aiter_lines.return_value = async_iterable_from_list([])
mock_response.aiter_text.return_value = async_iterable_from_list([])
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response

with pytest.raises(A2AClientError) as exc_info:
Expand Down Expand Up @@ -813,8 +807,8 @@ async def test_send_message_streaming_ignores_unrecognized_fields(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.raise_for_status = MagicMock()
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.aiter_lines.return_value = async_iterable_from_list(
[f'data: {json.dumps(payload)}', '']
mock_response.aiter_text.return_value = async_iterable_from_list(
[f'data: {json.dumps(payload)}\n\n']
)
mock_sse_source.return_value.__aenter__.return_value = mock_response

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

with pytest.raises(expected_error):
Expand Down
12 changes: 6 additions & 6 deletions tests/client/transports/test_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def test_send_message_streaming_timeout(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.raise_for_status.return_value = None
mock_response.aiter_lines.side_effect = httpx.TimeoutException(
mock_response.aiter_text.side_effect = httpx.TimeoutException(
'Read timed out'
)
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response
Expand Down Expand Up @@ -348,7 +348,7 @@ async def test_send_message_streaming_with_new_extensions(
mock_response = AsyncMock(spec=httpx.Response)
mock_response.headers = {'content-type': 'text/event-stream'}
mock_response.raise_for_status.return_value = None
mock_response.aiter_lines.return_value = async_iterable_from_list([])
mock_response.aiter_text.return_value = async_iterable_from_list([])
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response

context = ClientCallContext(
Expand Down Expand Up @@ -398,7 +398,7 @@ async def test_send_message_streaming_server_error_propagates(
request=httpx.Request('POST', 'http://test.url'),
response=mock_response,
)
mock_response.aiter_lines.return_value = async_iterable_from_list([])
mock_response.aiter_text.return_value = async_iterable_from_list([])
mock_aconnect_sse.return_value.__aenter__.return_value = mock_response

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

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

Expand Down
Loading