Skip to content

Commit 3932982

Browse files
Elon-Jiangcodexmykytanetipa
authored
fix(client): handle case-insensitive SSE media types (a2aproject#1464)
# Description Streaming clients fail to parse a valid SSE response when the server sends `Content-Type: Text/Event-Stream` (or another capitalization). The shared helper treats it as a non-stream response and attempts to decode the SSE envelope as JSON. HTTP media types and subtypes are case-insensitive ([RFC 9110 §8.3.1](https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3.1)). Normalize the header before the existing SSE check. Both REST and JSON-RPC transports share this helper. Add ten public-transport regression/control cases covering lowercase, title case, uppercase, mixed case with a charset parameter, and ordinary JSON responses. Validation on upstream `14e799cb245c79e9f46bfb07763d7840b43e4cff`: - Original implementation: six regression failures; four controls pass. A local HTTP server also reproduces the parsing failure through the real REST transport; both capitalization variants return the expected message after the fix. - `./scripts/lint.sh`: passes (three existing unused-suppression warnings in untouched files). - `uv run pytest`: 2596 passed, 207 skipped, 4 xfailed, 1 xpassed. - `uv run pytest --cov=src --cov-report=term-missing --cov-fail-under=88`: same test results, 94% total coverage. The transport tests supply deterministic responses with `httpx.MockTransport`; the separate HTTP reproduction uses a real loopback server. These replace the external Agent endpoint only. No real model/provider call was tested. Existing optional/database skips and expected-failure markers remain unchanged. - [x] Follow the CONTRIBUTING guide. - [x] Use a conventional commit title. - [x] Run tests and linters. - [x] Documentation considered: no public API or configuration change. AI assistance: OpenAI Codex helped investigate, implement, and validate this change. Co-authored-by: Elon-Jiang <191222665+Elon-Jiang@users.noreply.github.com> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent 569b94f commit 3932982

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ async def send_http_stream_request(
174174
raise e
175175

176176
# If the response is not a stream, read it standardly (e.g., upfront JSON-RPC error payload)
177-
if 'text/event-stream' not in response.headers.get(
178-
'content-type', ''
177+
if (
178+
'text/event-stream'
179+
not in response.headers.get('content-type', '').lower()
179180
):
180181
content = await response.aread()
181182
yield content.decode('utf-8')
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import json
2+
3+
from typing import Any
4+
5+
import httpx
6+
import pytest
7+
8+
from a2a.client.transports.jsonrpc import JsonRpcTransport
9+
from a2a.client.transports.rest import RestTransport
10+
from a2a.types.a2a_pb2 import (
11+
AgentCard,
12+
Message,
13+
Part,
14+
Role,
15+
SendMessageRequest,
16+
StreamResponse,
17+
)
18+
from google.protobuf.json_format import MessageToDict
19+
20+
21+
@pytest.mark.asyncio
22+
@pytest.mark.parametrize('transport_class', [RestTransport, JsonRpcTransport])
23+
@pytest.mark.parametrize(
24+
('content_type', 'is_sse'),
25+
[
26+
('text/event-stream', True),
27+
('Text/Event-Stream', True),
28+
('TEXT/EVENT-STREAM', True),
29+
('TeXt/EvEnT-StReAm; charset=utf-8', True),
30+
('application/json', False),
31+
],
32+
)
33+
async def test_streaming_transport_accepts_media_type_case(
34+
transport_class: type[RestTransport] | type[JsonRpcTransport],
35+
content_type: str,
36+
is_sse: bool,
37+
) -> None:
38+
"""Decode equivalent media types through both public streaming transports."""
39+
expected = StreamResponse(
40+
message=Message(
41+
message_id='reply',
42+
role=Role.ROLE_AGENT,
43+
parts=[Part(text='hello')],
44+
)
45+
)
46+
47+
def respond(request: httpx.Request) -> httpx.Response:
48+
payload: dict[str, Any] = MessageToDict(expected)
49+
if transport_class is JsonRpcTransport:
50+
payload = {
51+
'jsonrpc': '2.0',
52+
'id': json.loads(request.content)['id'],
53+
'result': payload,
54+
}
55+
serialized = json.dumps(payload)
56+
body = f'data: {serialized}\n\n' if is_sse else serialized
57+
return httpx.Response(
58+
200, headers={'Content-Type': content_type}, text=body
59+
)
60+
61+
async with httpx.AsyncClient(
62+
transport=httpx.MockTransport(respond)
63+
) as http_client:
64+
transport = transport_class(
65+
httpx_client=http_client,
66+
agent_card=AgentCard(),
67+
url='https://agent.example',
68+
is_shared_httpx=True,
69+
)
70+
request = SendMessageRequest(
71+
message=Message(
72+
message_id='request',
73+
role=Role.ROLE_USER,
74+
parts=[Part(text='hi')],
75+
)
76+
)
77+
78+
events = [
79+
event async for event in transport.send_message_streaming(request)
80+
]
81+
82+
assert events == [expected]

0 commit comments

Comments
 (0)