Skip to content

Commit 945de8a

Browse files
Linux2010claude
andcommitted
fix: add SSE parameter and content alias for v0.3 compat streaming
Fixes #1089 - Response Stream Array-Wrapping Crash 1. Add ?alt=sse parameter to streaming endpoints in rest_transport.py: - /v1/message:stream?alt=sse - /v1/tasks/{id}:subscribe?alt=sse This forces Google A2A API to return SSE format instead of JSON array-wrapped streams that cause protobuf Parse errors. 2. Add AliasChoices for 'content' field in Message class (types.py): - Accepts both 'parts' (v1.0) and 'content' (v0.3) field names - Fixes ValidationError when parsing v0.3 wire format responses Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent d2f2e18 commit 945de8a

4 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎src/a2a/compat/v0_3/rest_transport.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async def send_message_streaming(
139139

140140
async for event in self._send_stream_request(
141141
'POST',
142-
'/v1/message:stream',
142+
'/v1/message:stream?alt=sse',
143143
context=context,
144144
json=MessageToDict(req_proto, preserving_proto_field_name=True),
145145
):
@@ -291,7 +291,7 @@ async def subscribe(
291291
try:
292292
async for event in self._send_stream_request(
293293
subscribe_method,
294-
f'/v1/tasks/{request.id}:subscribe',
294+
f'/v1/tasks/{request.id}:subscribe?alt=sse',
295295
context=context,
296296
):
297297
yield event

‎src/a2a/compat/v0_3/types.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from enum import Enum
77
from typing import Any, Literal
88

9-
from pydantic import Field, RootModel
9+
from pydantic import AliasChoices, Field, RootModel
1010

1111
from a2a._base import A2ABaseModel
1212

@@ -1458,7 +1458,7 @@ class Message(A2ABaseModel):
14581458
"""
14591459
Optional metadata for extensions. The key is an extension-specific identifier.
14601460
"""
1461-
parts: list[Part]
1461+
parts: list[Part] = Field(validation_alias=AliasChoices('parts', 'content'))
14621462
"""
14631463
An array of content parts that form the message body. A message can be
14641464
composed of multiple parts of different types (e.g., text and files).

‎tests/compat/v0_3/test_conversions.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ def test_message_conversion():
203203
assert v03_restored == v03_msg
204204

205205

206+
def test_message_content_alias():
207+
"""Test that Message accepts 'content' as alias for 'parts' (v0.3 compatibility)."""
208+
# Test with 'content' key (legacy v0.3 wire format)
209+
legacy_json = {
210+
'messageId': 'msg-123',
211+
'role': 'user',
212+
'content': [{'kind': 'text', 'text': 'hello'}],
213+
}
214+
msg = types_v03.Message.model_validate(legacy_json)
215+
assert msg.parts == [types_v03.Part(root=types_v03.TextPart(text='hello'))]
216+
217+
# Test with 'parts' key (modern format)
218+
modern_json = {
219+
'messageId': 'msg-456',
220+
'role': 'agent',
221+
'parts': [{'kind': 'text', 'text': 'response'}],
222+
}
223+
msg2 = types_v03.Message.model_validate(modern_json)
224+
assert msg2.parts == [types_v03.Part(root=types_v03.TextPart(text='response'))]
225+
226+
206227
def test_message_conversion_minimal():
207228
v03_msg = types_v03.Message(
208229
message_id='m1',

‎tests/compat/v0_3/test_rest_transport.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,35 @@ async def mock_send_stream_request(*args, **kwargs):
233233
assert events[1] == StreamResponse(message=Message(message_id='msg-123'))
234234

235235

236+
@pytest.mark.asyncio
237+
async def test_compat_rest_transport_stream_url_has_alt_sse_param(transport):
238+
"""Test that streaming endpoints include ?alt=sse parameter."""
239+
captured_paths = []
240+
241+
async def mock_send_stream_request(method, path, context=None, json=None):
242+
captured_paths.append(path)
243+
task = Task(id='task-123')
244+
task.status.message.role = Role.ROLE_AGENT
245+
yield StreamResponse(task=task)
246+
247+
transport._send_stream_request = mock_send_stream_request
248+
249+
# Test send_message_streaming
250+
req = SendMessageRequest(
251+
message=Message(message_id='msg-1', role=Role.ROLE_USER)
252+
)
253+
_ = [event async for event in transport.send_message_streaming(req)]
254+
255+
assert captured_paths[-1] == '/v1/message:stream?alt=sse'
256+
257+
# Test subscribe
258+
captured_paths.clear()
259+
sub_req = SubscribeToTaskRequest(id='task-123')
260+
_ = [event async for event in transport.subscribe(sub_req)]
261+
262+
assert captured_paths[-1] == '/v1/tasks/task-123:subscribe?alt=sse'
263+
264+
236265
def create_405_error():
237266
mock_response = MagicMock(spec=httpx.Response)
238267
mock_response.status_code = 405

0 commit comments

Comments
 (0)