Skip to content

fix: filter thought parts from A2A client user-facing response#4686

Open
OiPunk wants to merge 2 commits intogoogle:mainfrom
OiPunk:codex/adk-python-4676-a2a-thought-filter
Open

fix: filter thought parts from A2A client user-facing response#4686
OiPunk wants to merge 2 commits intogoogle:mainfrom
OiPunk:codex/adk-python-4676-a2a-thought-filter

Conversation

@OiPunk
Copy link

@OiPunk OiPunk commented Mar 3, 2026

Summary

  • Filters out thought parts (metadata.adk_thought: true) from A2A client completed/final response events before yielding to consumers
  • Intermediate (submitted/working) events are preserved as-is since all their parts are already marked as thoughts for streaming progress display
  • Added 4 regression tests covering completed tasks, status updates, working state, and regular A2A messages

Root Cause

In RemoteA2aAgent._handle_a2a_response(), intermediate events correctly mark all parts as thought=True for working/submitted states. However, when a completed response arrives containing both thought parts and final answer parts, the thought parts are not filtered out. The convert_a2a_part_to_genai_part() correctly preserves thought=True from 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():

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

This safely handles all cases:

  • Completed tasks: Thought parts are removed, only final answer remains
  • Working/submitted events: All parts are thoughts → non_thought_parts is empty → no filtering (preserved as-is)
  • Events with no thought parts: No change

Test Plan

  • Added test_handle_a2a_response_filters_thought_parts_from_completed_task
  • Added test_handle_a2a_response_filters_thought_parts_from_status_update
  • Added test_handle_a2a_response_preserves_all_thought_parts_for_working
  • Added test_handle_a2a_response_filters_thought_from_a2a_message
  • All 24 existing + new tests in TestRemoteA2aAgentMessageHandling pass

Fixes #4676

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
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 _handle_a2a_response method to strip these thought parts from user-facing responses, ensuring that only the intended final answer is displayed. This improves the clarity and correctness of the client's output without affecting the display of intermediate progress.

Highlights

  • User-facing response filtering: Implemented filtering to remove internal "thought" parts (metadata.adk_thought: true) from A2A client completed/final response events before they are yielded to consumers.
  • Intermediate event preservation: Ensured that intermediate (submitted/working) events are preserved as-is, as all their parts are intentionally marked as thoughts for streaming progress display.
  • Regression tests: Added four new regression tests to cover completed tasks, status updates, working state preservation, and regular A2A messages, ensuring the filtering logic behaves as expected.

🧠 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
  • src/google/adk/agents/remote_a2a_agent.py
    • Added conditional logic within _handle_a2a_response to filter out parts marked as thought=True from the event content before returning it, specifically for completed responses.
  • tests/unittests/agents/test_remote_a2a_agent.py
    • Added test_handle_a2a_response_filters_thought_parts_from_completed_task to verify thought parts are removed from completed tasks.
    • Added test_handle_a2a_response_filters_thought_parts_from_status_update to confirm thought parts are filtered from completed status updates.
    • Added test_handle_a2a_response_preserves_all_thought_parts_for_working to ensure all thought parts are preserved for working state events.
    • Added test_handle_a2a_response_filters_thought_from_a2a_message to test thought filtering for regular A2A messages.
Activity
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@google-cla
Copy link

google-cla bot commented Mar 3, 2026

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.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Mar 3, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Mar 3, 2026

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!

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +518 to +526
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A2A client renders thought parts together with final user-facing response

2 participants