fix: Concatenate all text Parts from an AgentTool agent's response.

PiperOrigin-RevId: 758424011
This commit is contained in:
Liang Wu 2025-05-13 16:38:06 -07:00 committed by Copybara-Service
parent d6cf660506
commit 933f01b89f

View File

@ -162,17 +162,17 @@ class AgentTool(BaseTool):
filename=artifact_name, artifact=artifact filename=artifact_name, artifact=artifact
) )
if ( if not last_event or not last_event.content or not last_event.content.parts:
not last_event
or not last_event.content
or not last_event.content.parts
or not last_event.content.parts[0].text
):
return '' return ''
if isinstance(self.agent, LlmAgent) and self.agent.output_schema: if isinstance(self.agent, LlmAgent) and self.agent.output_schema:
merged_text = '\n'.join(
[p.text for p in last_event.content.parts if p.text]
)
tool_result = self.agent.output_schema.model_validate_json( tool_result = self.agent.output_schema.model_validate_json(
last_event.content.parts[0].text merged_text
).model_dump(exclude_none=True) ).model_dump(exclude_none=True)
else: else:
tool_result = last_event.content.parts[0].text tool_result = '\n'.join(
[p.text for p in last_event.content.parts if p.text]
)
return tool_result return tool_result