Skip to content

Commit 2294f1f

Browse files
author
8packcoder
committed
fix(agents): tolerate empty A2A message parts in RemoteA2aAgent
Guard against empty event.content.parts when handling initial TASK_REQUIRED responses. Added unit test covering the empty-parts scenario.
1 parent d518b23 commit 2294f1f

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/google/adk/agents/remote_a2a_agent.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,14 @@ async def _handle_a2a_response(
411411
)
412412
# for streaming task, we update the event with the task status.
413413
# We update the event as Thought updates.
414-
if task and task.status and task.status.state == TaskState.submitted:
415-
event.content.parts[0].thought = True
414+
if (
415+
task
416+
and task.status
417+
and task.status.state == TaskState.submitted
418+
and event.content.parts
419+
):
420+
for part in event.content.parts:
421+
part.thought = True
416422
elif (
417423
isinstance(update, A2ATaskStatusUpdateEvent)
418424
and update.status

tests/unittests/agents/test_remote_a2a_agent.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,45 @@ async def test_handle_a2a_response_with_partial_artifact_update(self):
10091009

10101010
assert result is None
10111011

1012+
@pytest.mark.asyncio
1013+
async def test_handle_a2a_response_with_task_submitted_and_empty_parts(self):
1014+
"""Test handling of a task submitted response with empty parts."""
1015+
mock_a2a_task = Mock(spec=A2ATask)
1016+
mock_a2a_task.id = "task-123"
1017+
mock_a2a_task.context_id = "context-123"
1018+
mock_a2a_task.status = Mock(spec=A2ATaskStatus)
1019+
mock_a2a_task.status.state = TaskState.submitted
1020+
1021+
# Create a proper Event mock that can handle custom_metadata
1022+
# Content with empty parts
1023+
mock_event = Event(
1024+
author=self.agent.name,
1025+
invocation_id=self.mock_context.invocation_id,
1026+
branch=self.mock_context.branch,
1027+
content=genai_types.Content(role="model", parts=[]),
1028+
)
1029+
1030+
with patch(
1031+
"google.adk.agents.remote_a2a_agent.convert_a2a_task_to_event"
1032+
) as mock_convert:
1033+
mock_convert.return_value = mock_event
1034+
1035+
result = await self.agent._handle_a2a_response(
1036+
(mock_a2a_task, None), self.mock_context
1037+
)
1038+
1039+
assert result == mock_event
1040+
mock_convert.assert_called_once_with(
1041+
mock_a2a_task,
1042+
self.agent.name,
1043+
self.mock_context,
1044+
self.mock_a2a_part_converter,
1045+
)
1046+
# Check that metadata was added
1047+
assert result.custom_metadata is not None
1048+
assert A2A_METADATA_PREFIX + "task_id" in result.custom_metadata
1049+
assert A2A_METADATA_PREFIX + "context_id" in result.custom_metadata
1050+
10121051

10131052
class TestRemoteA2aAgentMessageHandlingFromFactory:
10141053
"""Test message handling functionality."""

0 commit comments

Comments
 (0)