- Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(callbacks): make after_agent_callback replace instead of append #3517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When after_agent_callback returns a Content object, it now replaces the agent's original output instead of appending as an additional event. The implementation marks the original final response event as partial when a callback exists, then yields the callback's content as the true final response. This ensures only one final response event is emitted while preserving session history for sequential agents and tool calling.
Summary of ChangesHello @dylan-apex, 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 critical bug in the Highlights
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.
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 correctly addresses the issue of after_agent_callback appending instead of replacing the agent's final response. The approach of marking the original response as partial and yielding a new final response from the callback is sound. However, I've identified a critical bug in the implementation in base_agent.py where the agent's final response can be dropped if a callback modifies the session state but doesn't return any new content. I have left detailed comments with suggested fixes for both run_async and run_live methods to address this data loss scenario. The updates to the unit tests look good and align with the intended new behavior.
…nly event When after_agent_callback returns an event with only state changes (no content), the original final response was being lost. This fix ensures that: - If callback returns content, it replaces the original response (as documented) - If callback only modifies state, the original final response is preserved - State-only events are marked as partial so they're not considered final responses Added test case to verify correct behavior when callback modifies state but returns no content.
| /gemini review |
There was a problem hiding this 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 correctly adjusts the after_agent_callback to replace the original agent's output, aligning its behavior with the documentation. The implementation is sound and covers various callback scenarios, and the accompanying test updates validate the new logic. My review focuses on improving maintainability by addressing significant code duplication. I've suggested refactoring a large, repeated block of logic in base_agent.py into a helper method and consolidating a recurring filtering pattern in the test suite.
| /gemini summary |
Summary of ChangesThis pull request addresses a critical bug in the Highlights
Changelog
Activity
|
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
(after_agent_callback)[https://google.github.io/adk-docs/callbacks/types-of-callbacks/#after-agent-callback] clearly states that when you use this, it will replace the original event with the new event, however, in practice it simply appends the event and both events are marked as
is_final_response. In multi-agent workflows, this means that not only can you get multiple final responses from the individual agents, but you can get more than 1 final response per agent, therefore making it more difficult to tell which responses matter.Solution:
When after_agent_callback returns a Content object, it now replaces the agent's original output instead of appending as an additional event.
The implementation marks the original final response event as partial when a callback exists, then yields the callback's content as the true final response. This ensures only one final response event is emitted while preserving session history for sequential agents and tool calling.
It also updates unit tests that relied on the old behavior of sending 2 events when
after_agent_callbackis used to only send and expect 1event.is_final_responseTesting Plan
Unit Tests:
====================== 3135 passed, 2396 warnings in 47.72s =======================
Manual End-to-End (E2E) Tests:
Working E2E Test
Output
Checklist
Additional context
Add any other context or screenshots about the feature request here.