-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(flows): dedupe identical tool calls within an invocation (#3940) #4189
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
…3940) Repeated tool execution can happen when the model emits the same function call multiple times (same tool name + same arguments), which is especially painful for slow or expensive tools. This change introduces an invocation-scoped single-flight cache: - Adds RunConfig.dedupe_tool_calls (opt-in) to reuse results for identical calls - Always dedupes BaseTool.is_long_running tools (safer default for long ops) - Ensures only the first execution applies tool side effects (state/artifacts) - Emits cached responses with per-call function_response.id and debug metadata Tests: - Added unit tests covering duplicate calls across steps and within one step.
|
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 @jayy-77, 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 the issue of redundant tool executions when a model repeatedly emits the same function call. By implementing an invocation-scoped caching mechanism, it significantly improves efficiency and reduces costs associated with slow or expensive tools. The changes ensure that identical tool calls are executed only once, while still providing the expected responses for all calls, and carefully manage side effects to prevent unintended multiple applications. 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. 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
|
|
Hello @jayy-77, thank you for your contribution! Before we can merge this PR, you'll need to sign our Contributor License Agreement (CLA). You can do so here: https://cla.developers.google.com/ Response from ADK Triaging Agent |
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 introduces a valuable feature for deduplicating identical tool calls within an invocation, which can significantly improve performance and reduce costs for slow or expensive tools. The implementation is robust, employing a single-flight pattern with asyncio.Task and proper locking to handle concurrent requests effectively. The exception handling, which clears the cache on failure, is a thoughtful addition that prevents caching failed states. The changes are well-contained, and the inclusion of unit tests covering both sequential and parallel duplicate calls is excellent. I have a couple of minor suggestions to reduce code duplication, but overall, this is a high-quality contribution.
| should_dedupe = bool( | ||
| invocation_context.run_config | ||
| and invocation_context.run_config.dedupe_tool_calls | ||
| ) or tool.is_long_running |
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.
The logic to determine if a tool call should be deduplicated is repeated here and in _execute_single_function_call_live (lines 593-596). To avoid duplication and improve maintainability, consider extracting this logic into a small helper function.
For example:
def _should_dedupe_tool_call(invocation_context: InvocationContext, tool: BaseTool) -> bool:
"""Determines if a tool call should be deduplicated."""
return (
invocation_context.run_config
and invocation_context.run_config.dedupe_tool_calls
) or tool.is_long_running| if cache_hit: | ||
| function_response_event.custom_metadata = ( | ||
| function_response_event.custom_metadata or {} | ||
| ) | ||
| function_response_event.custom_metadata['adk_tool_call_cache_hit'] = True |
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.
This logic for adding cache hit metadata to the event is duplicated in _execute_single_function_call_live (lines 619-623). Extracting this into a helper function would improve code reuse and maintainability.
For example:
def _add_cache_hit_metadata(event: Event) -> None:
"""Adds cache hit metadata to a function response event."""
event.custom_metadata = event.custom_metadata or {}
event.custom_metadata['adk_tool_call_cache_hit'] = TrueYou could then call it like if cache_hit: _add_cache_hit_metadata(function_response_event).
Repeated tool execution can happen when the model emits the same function call multiple times (same tool name + same arguments), which is especially painful for slow or expensive tools.
This change introduces an invocation-scoped single-flight cache:
Tests:
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):