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.
|
Hi @gautamvarmadatla , 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. |
|
Hi @Jacksunwei , can you please review this. LGTM. |
|
Hi @Jacksunwei @rohityan, just a gentle follow-up in case this got buried. Whenever you have a chance, I’d really appreciate a review on the PR. Over past two weeks, I’ve been updating the branch regularly to keep it in sync with main, so I’d be very grateful if you could take a look when possible. |
…circuits LlmAgent Merge #4838 - Closes: #4837 **Problem:** When `LlmAgent` uses both `output_key` and `before_agent_callback`, and the before-agent callback returns `types.Content`, execution is correctly short-circuited and the response is returned to the user, but `session.state[output_key]` is not updated because the normal `LlmAgent` output-saving path is bypassed. **Solution:** Override `_handle_before_agent_callback` in `LlmAgent` to call `__maybe_save_output_to_state()` on any returned event. This keeps the fix scoped to `LlmAgent`, which owns `output_key`, avoids introducing `BaseAgent` changes for subclass-specific behavior, and fixes both `run_async` and `run_live` through the shared callback path. ### Testing Plan Added a regression test that runs `LlmAgent` with `output_key` and a short-circuiting `before_agent_callback`, then checks that `session.state["result"]` contains the returned text. **Unit Tests:** - [X] I have added or updated unit tests for my change. - [X] All unit tests pass locally. **Manual End-to-End (E2E) Tests:** Run repro code in #4837 In that example, `session.state["result"]` should equal "cached answer" ### Checklist - [X] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [X] I have performed a self-review of my own code. - [X] I have commented my code, particularly in hard-to-understand areas. - [X] I have added tests that prove my fix is effective or that my feature works. - [X] New and existing unit tests pass locally with my changes. - [X] I have manually tested my changes end-to-end. - [X] Any dependent changes have been merged and published in downstream modules. Co-authored-by: Liang Wu <wuliang@google.com> COPYBARA_INTEGRATE_REVIEW=#4838 from gautamvarmadatla:fix/output-key-before-agent-callback 7f084e0 PiperOrigin-RevId: 934022327
|
Thank you @gautamvarmadatla for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit 0e263f1. Closing this PR as the changes are now in the main branch. |
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