Skip to content

fix: clean up transfer_to_agent docstring to reduce token waste#4628

Open
stakeswky wants to merge 2 commits intogoogle:mainfrom
stakeswky:fix/clean-transfer-tool-docstring
Open

fix: clean up transfer_to_agent docstring to reduce token waste#4628
stakeswky wants to merge 2 commits intogoogle:mainfrom
stakeswky:fix/clean-transfer-tool-docstring

Conversation

@stakeswky
Copy link

Move the developer-facing Note from transfer_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

…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
@google-cla
Copy link

google-cla bot commented Feb 25, 2026

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • Token Optimization: Moved a developer-facing note from the transfer_to_agent function's docstring to a code comment to reduce LLM token usage and noise, aiming to decrease hallucination risk.
  • Function Call ID Consistency: Implemented logic in _finalize_model_response_event to ensure consistent function call IDs are maintained across partial and final Server-Sent Events (SSE) during streaming, addressing a regression for issue populate_client_function_call_id generates different UUIDs for the same function call across partial and final SSE streaming events #4609.
  • New Unit Tests: Added new unit tests to validate the correct preservation and consistency of function call IDs in _finalize_model_response_event for streaming scenarios.

🧠 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/flows/llm_flows/base_llm_flow.py
    • Modified _finalize_model_response_event to collect and restore function call IDs, ensuring consistency between partial and final SSE events.
    • Updated the docstring for the model_response_event parameter to clarify its role in SSE streaming.
  • src/google/adk/tools/transfer_to_agent_tool.py
    • Relocated a developer-specific note from the transfer_to_agent function's docstring to a code comment above the function definition.
  • tests/unittests/flows/llm_flows/test_base_llm_flow.py
    • Introduced new test cases to verify that _finalize_model_response_event maintains consistent function call IDs across partial and final SSE events and correctly preserves LLM-assigned IDs.
Activity
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 25, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Feb 25, 2026

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 cla/google check at the bottom of the pull request.

Also, could you please add a testing plan section to your PR description to describe how you tested your changes? This will help reviewers to review your PR more efficiently.

Thanks!

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 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.

Comment on lines +124 to +126
for idx, fc in enumerate(function_calls):
if idx < len(prior_fc_ids) and prior_fc_ids[idx]:
fc.id = prior_fc_ids[idx]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

@stakeswky stakeswky force-pushed the fix/clean-transfer-tool-docstring branch from e9c4798 to 8bc5237 Compare February 26, 2026 06:37
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change the docstring for the transfer_to_agent tool to remove unneeded information sent to the model.

2 participants