Skip to content

Commit ed2c3eb

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Prevent stopping event processing on events with None content
PiperOrigin-RevId: 856706510
1 parent 50c4b8d commit ed2c3eb

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/google/adk/agents/remote_a2a_agent.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ def _create_a2a_request_for_user_function_response(
348348

349349
return a2a_message
350350

351+
def _is_remote_response(self, event: Event) -> bool:
352+
return (
353+
event.author == self.name
354+
and event.custom_metadata
355+
and event.custom_metadata.get(A2A_METADATA_PREFIX + "response", False)
356+
)
357+
351358
def _construct_message_parts_from_session(
352359
self, ctx: InvocationContext
353360
) -> tuple[list[A2APart], Optional[str]]:
@@ -365,7 +372,7 @@ def _construct_message_parts_from_session(
365372

366373
events_to_process = []
367374
for event in reversed(ctx.session.events):
368-
if event.author == self.name:
375+
if self._is_remote_response(event):
369376
# stop on content generated by current a2a agent given it should already
370377
# be in remote session
371378
if event.custom_metadata:
@@ -496,6 +503,8 @@ async def _handle_a2a_response(
496503
invocation_id=ctx.invocation_id,
497504
branch=ctx.branch,
498505
)
506+
event.custom_metadata = event.custom_metadata or {}
507+
event.custom_metadata[A2A_METADATA_PREFIX + "response"] = True
499508
return event
500509
except A2AClientError as e:
501510
logger.error("Failed to handle A2A response: %s", e)

tests/unittests/agents/test_remote_a2a_agent.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,16 @@ def test_construct_message_parts_from_session_stops_on_agent_reply(self):
683683
agent1 = Mock()
684684
agent1.content = content2
685685
agent1.author = self.agent.name
686-
agent1.custom_metadata = None
686+
agent1.custom_metadata = {
687+
A2A_METADATA_PREFIX + "response": True,
688+
}
689+
690+
agent2 = Mock()
691+
agent2.content = None
692+
agent2.author = self.agent.name
693+
# Just actions, no content. Not marked as a response.
694+
agent2.actions = Mock()
695+
agent2.custom_metadata = None
687696

688697
part3 = Mock()
689698
part3.text = "User 2"
@@ -694,7 +703,7 @@ def test_construct_message_parts_from_session_stops_on_agent_reply(self):
694703
user2.author = "user"
695704
user2.custom_metadata = None
696705

697-
self.mock_session.events = [user1, agent1, user2]
706+
self.mock_session.events = [user1, agent1, user2, agent2]
698707

699708
def mock_converter(part):
700709
mock_a2a_part = Mock()
@@ -785,7 +794,10 @@ def test_construct_message_parts_from_session_stateful_partial_history(self):
785794
agent1 = Mock()
786795
agent1.content = content2
787796
agent1.author = self.agent.name
788-
agent1.custom_metadata = {A2A_METADATA_PREFIX + "context_id": "ctx-1"}
797+
agent1.custom_metadata = {
798+
A2A_METADATA_PREFIX + "response": True,
799+
A2A_METADATA_PREFIX + "context_id": "ctx-1",
800+
}
789801

790802
part3 = Mock()
791803
part3.text = "User 2"

0 commit comments

Comments
 (0)