Skip to content

fix: prevent state_delta overwrite on function_response-only events#4642

Open
yuvrajangadsingh wants to merge 1 commit intogoogle:mainfrom
yuvrajangadsingh:fix/state-delta-overwrite-3178
Open

fix: prevent state_delta overwrite on function_response-only events#4642
yuvrajangadsingh wants to merge 1 commit intogoogle:mainfrom
yuvrajangadsingh:fix/state-delta-overwrite-3178

Conversation

@yuvrajangadsingh
Copy link

Fixes #3178

When skip_summarization=True and an event only has function_response parts (no text), __maybe_save_output_to_state joins zero text parts into "" and overwrites whatever the after_tool_callback already set in state_delta.

Root cause: is_final_response() returns True for skip_summarization events, so the method enters the save block. But there are no text parts to extract, so result = "". Without output_schema, the empty string gets written directly to state_delta[output_key], clobbering the callback's value.

Fix: skip writing to state_delta when result is empty and no output_schema is configured. This preserves callback-set values on function_response-only events while keeping existing behavior for text-based responses unchanged.

Testing: added a test that sets state_delta via callback on a function_response-only event with skip_summarization=True, then verifies the value isn't overwritten. All 406 agent tests pass.

@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 a bug where the state_delta of an LlmAgent could be inadvertently cleared or overwritten when processing events that only contain function_response parts and skip_summarization is enabled. The fix ensures that pre-existing state_delta values, particularly those set by after_tool_callback, are maintained under these specific conditions, thereby preserving the agent's state integrity.

Highlights

  • Prevent state_delta overwrite: Implemented a check in __maybe_save_output_to_state to prevent state_delta from being overwritten with an empty string when skip_summarization is true and an event only contains function_response parts without an output_schema.
  • New test case: Added a unit test to verify that state_delta values set by callbacks are correctly preserved in function_response-only events with skip_summarization=True.

🧠 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/llm_agent.py
    • Added a conditional return to __maybe_save_output_to_state to prevent overwriting state_delta with an empty string when no text parts are found and no output_schema is present.
  • tests/unittests/agents/test_llm_agent_output_save.py
    • Introduced test_maybe_save_output_to_state_skips_function_response_only_event to confirm that state_delta values are preserved for function_response-only events with skip_summarization.
Activity
  • No human activity (comments, reviews, etc.) has been recorded for this pull request yet.
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.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Feb 27, 2026
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 effectively addresses a bug where state_delta was being overwritten by an empty string in __maybe_save_output_to_state for function-response-only events with skip_summarization=True. The added elif not result: condition correctly prevents this unintended behavior. The new test case test_maybe_save_output_to_state_skips_function_response_only_event thoroughly validates the fix, ensuring the callback-set values are preserved. The changes are well-implemented and improve the robustness of the agent's state management.

@yuvrajangadsingh yuvrajangadsingh marked this pull request as ready for review February 27, 2026 12:28
@ryanaiagent ryanaiagent self-assigned this Feb 27, 2026
@ryanaiagent ryanaiagent added the needs review [Status] The PR/issue is awaiting review from the maintainer label Feb 27, 2026
@ryanaiagent
Copy link
Collaborator

Hi @yuvrajangadsingh , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@ryanaiagent
Copy link
Collaborator

Hi @xuanyang15 , can you please review this.

When skip_summarization is True and an event only contains
function_response parts (no text), __maybe_save_output_to_state
would join zero text parts into an empty string and overwrite
the state_delta value already set by after_tool_callback.

Skip writing to state_delta when result is empty and no
output_schema is configured.

Fixes google#3178
@yuvrajangadsingh yuvrajangadsingh force-pushed the fix/state-delta-overwrite-3178 branch from fe595fd to 6db186d Compare February 28, 2026 11:24
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 needs review [Status] The PR/issue is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overridden state_delta by empty string in after_tool_callback after skip_summarization

3 participants