Skip to content

Commit c16cab1

Browse files
1AbdullahButtclaudeIwaniukooo11
authored
fix(server): fail streaming sends when an executor produces no events (#1460)
# Description Since #1345, `on_message_send` raises `InvalidAgentResponseError` ("Agent execution produced no events") when the `AgentExecutor` returns without producing any event. `on_message_send_stream` did not have the same guard: its subscription ended with nothing yielded, so `SendStreamingMessage` answered `200 text/event-stream` with zero events. Spec §3.1.2 requires the stream to begin with a `Task` or contain exactly one `Message`. This PR applies the same check at the end of `on_message_send_stream`: when the subscription yielded no event and no task was created, it raises `InvalidAgentResponseError`. The JSON-RPC and REST dispatchers already read the first event eagerly, so the client now gets the same error as the non-streaming call (JSON-RPC `-32006`, REST `500 INVALID_AGENT_RESPONSE`) instead of an empty stream. Executors that produce events are unaffected. Tests: `test_on_message_send_stream_raises_when_agent_produces_no_events` reuses the `SilentExecutor` from the #1345 test. ``` $ pytest tests/server/request_handlers/test_default_request_handler_v2.py -k no_events # handler from main 1 failed, 1 passed $ pytest tests/server/request_handlers/test_default_request_handler_v2.py -k no_events # this branch 2 passed $ pytest tests/server tests/integration 1460 passed; the same 4 failures and 8 errors as on unmodified main (sample smoke tests and database tests that need services I don't have locally) ``` I also checked it over HTTP with a silent executor behind `create_jsonrpc_routes` and `create_rest_routes`: before, both streaming endpoints returned an empty `200` stream; after, JSON-RPC returns the `-32006` error and REST returns `500` with reason `INVALID_AGENT_RESPONSE`. `ruff check`, `ruff format` and `ty check` pass on the changed files. This change was written with Claude Code. - [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 #1459 🦕 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent caab6a2 commit c16cab1

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎src/a2a/server/request_handlers/default_request_handler_v2.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,12 @@ async def on_message_send_stream( # noqa: D102
555555

556556
task_id = cast('str', request_context.task_id)
557557

558+
produced_event = False
558559
async for event in active_task.subscribe(
559560
request=request_context,
560561
include_initial_task=False,
561562
):
563+
produced_event = True
562564
# Do NOT break here as we rely on AgentExecutor to yield control.
563565
# ActiveTask consumer (see active_task.py) validates the event
564566
# stream and raises InvalidAgentResponseError on misbehaving agents:
@@ -571,6 +573,13 @@ async def on_message_send_stream( # noqa: D102
571573
else:
572574
yield event
573575

576+
if not produced_event and not active_task.task_created:
577+
# A silent executor would otherwise end the stream without the
578+
# Task or Message it must start with.
579+
raise InvalidAgentResponseError(
580+
message='Agent execution produced no events'
581+
)
582+
574583
@server_plugin_hooks('create_task_push_notification_config')
575584
@validate_request_params
576585
@validate(

‎tests/server/request_handlers/test_default_request_handler_v2.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,3 +3689,29 @@ async def test_on_message_send_raises_when_agent_produces_no_events():
36893689
await request_handler.on_message_send(
36903690
params, create_server_call_context()
36913691
)
3692+
3693+
3694+
@pytest.mark.asyncio
3695+
async def test_on_message_send_stream_raises_when_agent_produces_no_events():
3696+
"""A silent executor must not end the stream without any event."""
3697+
request_handler = DefaultRequestHandlerV2(
3698+
agent_executor=SilentExecutor(),
3699+
task_store=InMemoryTaskStore(),
3700+
agent_card=create_default_agent_card(),
3701+
)
3702+
params = SendMessageRequest(
3703+
message=Message(
3704+
role=Role.ROLE_USER,
3705+
message_id='msg_silent_stream',
3706+
parts=[Part(text='Hi')],
3707+
),
3708+
configuration=SendMessageConfiguration(
3709+
accepted_output_modes=['text/plain']
3710+
),
3711+
)
3712+
3713+
with pytest.raises(InvalidAgentResponseError, match='no events'):
3714+
async for _ in request_handler.on_message_send_stream(
3715+
params, create_server_call_context()
3716+
):
3717+
pass

0 commit comments

Comments
 (0)