fix(agents): persist output_key when before_agent_callback short-circuits LlmAgent#4838
Conversation
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 a bug in 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug where output_key was not being persisted when a before_agent_callback short-circuits an LlmAgent. The fix involves overriding _handle_before_agent_callback in LlmAgent to ensure __maybe_save_output_to_state is called on the event returned from the callback. This is a clean and well-scoped solution. The addition of a specific regression test confirms the fix is effective. The changes are correct and well-implemented.
output_keyis not written to session state whenbefore_agent_callbackshort-circuits execution #4837Problem:
When
LlmAgentuses bothoutput_keyandbefore_agent_callback, and the before-agent callback returnstypes.Content, execution is correctly short-circuited and the response is returned to the user, butsession.state[output_key]is not updated because the normalLlmAgentoutput-saving path is bypassed.Solution:
Override
_handle_before_agent_callbackinLlmAgentto call__maybe_save_output_to_state()on any returned event. This keeps the fix scoped toLlmAgent, which ownsoutput_key, avoids introducingBaseAgentchanges for subclass-specific behavior, and fixes bothrun_asyncandrun_livethrough the shared callback path.Testing Plan
Added a regression test that runs
LlmAgentwithoutput_keyand a short-circuitingbefore_agent_callback, then checks thatsession.state["result"]contains the returned text.Unit Tests:
Manual End-to-End (E2E) Tests:
Run repro code in #4837
In that example,
session.state["result"]should equal "cached answer"Checklist