Skip to content

Commit 4f3b733

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: label response as thought if task is immediately returned as working
PiperOrigin-RevId: 847660113
1 parent 0b1cff2 commit 4f3b733

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/google/adk/agents/remote_a2a_agent.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,15 @@ async def _handle_a2a_response(
412412
)
413413
# for streaming task, we update the event with the task status.
414414
# We update the event as Thought updates.
415-
if task and task.status and task.status.state == TaskState.submitted:
415+
if (
416+
task
417+
and task.status
418+
and task.status.state
419+
in (
420+
TaskState.submitted,
421+
TaskState.working,
422+
)
423+
):
416424
event.content.parts[0].thought = True
417425
elif (
418426
isinstance(update, A2ATaskStatusUpdateEvent)
@@ -423,10 +431,10 @@ async def _handle_a2a_response(
423431
event = convert_a2a_message_to_event(
424432
update.status.message, self.name, ctx, self._a2a_part_converter
425433
)
426-
if event.content and update.status.state in [
434+
if event.content and update.status.state in (
427435
TaskState.submitted,
428436
TaskState.working,
429-
]:
437+
):
430438
for part in event.content.parts:
431439
part.thought = True
432440
elif isinstance(update, A2ATaskArtifactUpdateEvent) and (

tests/unittests/agents/test_remote_a2a_agent.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,48 @@ async def test_handle_a2a_response_with_task_submitted_and_no_update(self):
845845
assert A2A_METADATA_PREFIX + "task_id" in result.custom_metadata
846846
assert A2A_METADATA_PREFIX + "context_id" in result.custom_metadata
847847

848+
@pytest.mark.asyncio
849+
async def test_handle_a2a_response_with_task_working_and_no_update(self):
850+
"""Test successful A2A response handling with streaming task and no update."""
851+
mock_a2a_task = Mock(spec=A2ATask)
852+
mock_a2a_task.id = "task-123"
853+
mock_a2a_task.context_id = "context-123"
854+
mock_a2a_task.status = Mock(spec=A2ATaskStatus)
855+
mock_a2a_task.status.state = TaskState.working
856+
857+
# Create a proper Event mock that can handle custom_metadata
858+
mock_a2a_part = Mock(spec=TextPart)
859+
mock_event = Event(
860+
author=self.agent.name,
861+
invocation_id=self.mock_context.invocation_id,
862+
branch=self.mock_context.branch,
863+
content=genai_types.Content(role="model", parts=[mock_a2a_part]),
864+
)
865+
866+
with patch(
867+
"google.adk.agents.remote_a2a_agent.convert_a2a_task_to_event"
868+
) as mock_convert:
869+
mock_convert.return_value = mock_event
870+
871+
result = await self.agent._handle_a2a_response(
872+
(mock_a2a_task, None), self.mock_context
873+
)
874+
875+
assert result == mock_event
876+
mock_convert.assert_called_once_with(
877+
mock_a2a_task,
878+
self.agent.name,
879+
self.mock_context,
880+
self.mock_a2a_part_converter,
881+
)
882+
# Check the parts are updated as Thought
883+
assert result.content.parts[0].thought is True
884+
assert result.content.parts[0].thought_signature is None
885+
# Check that metadata was added
886+
assert result.custom_metadata is not None
887+
assert A2A_METADATA_PREFIX + "task_id" in result.custom_metadata
888+
assert A2A_METADATA_PREFIX + "context_id" in result.custom_metadata
889+
848890
@pytest.mark.asyncio
849891
async def test_handle_a2a_response_with_task_status_update_with_message(self):
850892
"""Test handling of a task status update with a message."""

0 commit comments

Comments
 (0)