fix: filter thought parts from A2A client user-facing response#4686
fix: filter thought parts from A2A client user-facing response#4686OiPunk wants to merge 2 commits intogoogle:mainfrom
Conversation
When an A2A server returns a completed response containing both thought parts (metadata.adk_thought=true) and final answer parts, the client now filters out thought parts before yielding the event to consumers. Intermediate (submitted/working) events are preserved as-is since all their parts are already marked as thoughts for streaming progress. Fixes google#4676
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where internal reasoning, or "thought parts," from A2A agent responses were inadvertently exposed to end-users in final completed events. The changes introduce a filtering mechanism within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @OiPunk, thank you for creating this PR! Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). You can visit https://cla.developers.google.com/ to see your current agreements or to sign a new one. Thanks! |
There was a problem hiding this comment.
Code Review
This pull request introduces a fix to filter 'thought' parts from the user-facing A2A client responses, which is a good improvement for the user experience. The implementation is clean and adds several regression tests to validate the behavior for different event types.
My main feedback is on a potential edge case in the filtering logic. While the current approach works for the tested scenarios, it might not correctly handle final/completed events that consist solely of thought parts. I've left a specific comment with more details.
Overall, the change is in the right direction, and the tests are a great addition.
| if ( | ||
| event.content is not None | ||
| and event.content.parts | ||
| ): | ||
| non_thought_parts = [ | ||
| p for p in event.content.parts if not p.thought | ||
| ] | ||
| if non_thought_parts: | ||
| event.content.parts = non_thought_parts |
There was a problem hiding this comment.
There's a potential edge case here. If a completed event contains only thought parts, this logic will not filter them, and they will be sent to the user. The current implementation assumes that if all parts are thoughts, it must be an intermediate (working/submitted) event that should be preserved.
However, if it's possible for a final/completed response to consist solely of thoughts, this assumption is incorrect. In that scenario, the list of parts should become empty, but here it would be preserved.
To make this more robust, you might need to explicitly check the task's state (e.g., completed, working) within this filtering logic, rather than inferring the event type from its content.
There was a problem hiding this comment.
Good observation! This is intentional — the if non_thought_parts: guard is a defensive choice following the same pattern used in contents.py:595-604 (_filter_thought_from_model_response).
If a completed event contains only thought parts, non_thought_parts will be empty and we preserve the original parts. The alternative — replacing with an empty list — would result in an empty response visible to the user, which is a worse outcome. In practice, a well-formed completed A2A response should always include at least one non-thought part (the final answer); an all-thought completed event would indicate a protocol-level anomaly that should be investigated rather than silently hidden.
That said, if the maintainers prefer explicit state-based filtering (checking the task state), I'm happy to adjust the approach.
Summary
metadata.adk_thought: true) from A2A client completed/final response events before yielding to consumersRoot Cause
In
RemoteA2aAgent._handle_a2a_response(), intermediate events correctly mark all parts asthought=Truefor working/submitted states. However, when a completed response arrives containing both thought parts and final answer parts, the thought parts are not filtered out. Theconvert_a2a_part_to_genai_part()correctly preservesthought=Truefrom A2A metadata, but the event is yielded with both thought and non-thought parts, causing consumers to render internal reasoning alongside the final answer.Fix
Added filtering logic before returning the event in
_handle_a2a_response():This safely handles all cases:
non_thought_partsis empty → no filtering (preserved as-is)Test Plan
test_handle_a2a_response_filters_thought_parts_from_completed_tasktest_handle_a2a_response_filters_thought_parts_from_status_updatetest_handle_a2a_response_preserves_all_thought_parts_for_workingtest_handle_a2a_response_filters_thought_from_a2a_messageTestRemoteA2aAgentMessageHandlingpassFixes #4676