fix: clean up transfer_to_agent docstring to reduce token waste#4628
fix: clean up transfer_to_agent docstring to reduce token waste#4628stakeswky wants to merge 2 commits intogoogle:mainfrom
Conversation
…g events
When SSE streaming is active, _finalize_model_response_event is called
once per LlmResponse chunk (partial=True for streaming chunks, partial=False
for the final). Each call creates a fresh Event via model_validate, which
means the function calls in the final event have empty IDs. When
populate_client_function_call_id runs on the final event it generates a
brand-new adk-{uuid}, different from the one assigned to the partial event.
This breaks Human-in-the-Loop (HITL) workflows using LongRunningFunctionTool:
1. Partial event yields function call with ID-A → consumer captures ID-A
2. Final event yields same function call with ID-B → ADK persists ID-B
3. Consumer submits FunctionResponse with ID-A → ADK can't find it → error
Fix: after populate_client_function_call_id assigns IDs to a partial event,
write the content (including IDs) back to model_response_event. On the next
call (final event), extract those IDs before the merge overwrites content,
then restore them by position before populate_client_function_call_id runs.
This ensures partial and final events for the same function call share the
same adk-* ID.
Fixes google#4609
Move developer-facing note from the docstring to a code comment above the function. The docstring is sent to the model on every tool call, so keeping it lean reduces token usage and lowers hallucination risk. Fixes google#4615
|
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. |
Summary of ChangesHello @stakeswky, 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 optimizes LLM interactions by reducing unnecessary token consumption and improving the reliability of function call handling during streaming. It refactors a docstring to be more LLM-friendly and fixes a bug where function call IDs were not consistently maintained across partial and final streaming events, which is crucial for human-in-the-loop workflows. This change directly addresses issue #4615. 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
|
|
Response from ADK Triaging Agent Hello @stakeswky, thank you for creating this PR! It looks like the CLA check has failed. Before we can review your contribution, you'll need to sign the Contributor License Agreement. You can find more information in the "Details" link of the Also, could you please add a Thanks! |
There was a problem hiding this comment.
Code Review
This pull request primarily addresses a bug with function call ID consistency during SSE streaming by ensuring IDs are preserved between partial and final events. The changes in src/google/adk/flows/llm_flows/base_llm_flow.py and the accompanying new tests are well-implemented and cover the fix effectively. The PR also includes a good optimization by moving a developer-facing note from a docstring to a code comment in src/google/adk/tools/transfer_to_agent_tool.py to save tokens.
While the changes are good, the pull request title and description are a bit misleading as they only mention the docstring cleanup. I recommend updating them to reflect the more significant functional change of fixing the function call ID consistency, which will provide better context for future code archeology.
| for idx, fc in enumerate(function_calls): | ||
| if idx < len(prior_fc_ids) and prior_fc_ids[idx]: | ||
| fc.id = prior_fc_ids[idx] |
There was a problem hiding this comment.
This loop can be made more concise and Pythonic by using zip. It simplifies iterating over function_calls and prior_fc_ids together and automatically stops when the shorter list is exhausted, matching the current logic.
| for idx, fc in enumerate(function_calls): | |
| if idx < len(prior_fc_ids) and prior_fc_ids[idx]: | |
| fc.id = prior_fc_ids[idx] | |
| for fc, prior_id in zip(function_calls, prior_fc_ids): | |
| if prior_id: | |
| fc.id = prior_id |
e9c4798 to
8bc5237
Compare
Move the developer-facing
Notefromtransfer_to_agent's docstring to a code comment above the function definition.The docstring is sent to the model on every tool invocation. The removed note was aimed at developers, not the model, so it wasted tokens and added noise that could increase hallucination risk.
No functional changes.
Fixes #4615