Skip to content

Commit 806c08e

Browse files
committed
fix: cache canonical tools to avoid multiple calls when streaming
1 parent e0e5384 commit 806c08e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/google/adk/agents/invocation_context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from ..plugins.plugin_manager import PluginManager
3333
from ..sessions.base_session_service import BaseSessionService
3434
from ..sessions.session import Session
35+
from ..tools.base_tool import BaseTool
3536
from .active_streaming_tool import ActiveStreamingTool
3637
from .base_agent import BaseAgent
3738
from .base_agent import BaseAgentState
@@ -202,6 +203,9 @@ class InvocationContext(BaseModel):
202203
plugin_manager: PluginManager = Field(default_factory=PluginManager)
203204
"""The manager for keeping track of plugins in this invocation."""
204205

206+
canonical_tools_cache: Optional[list[BaseTool]] = None
207+
"""The cache of canonical tools for this invocation."""
208+
205209
_invocation_cost_manager: _InvocationCostManager = PrivateAttr(
206210
default_factory=_InvocationCostManager
207211
)

src/google/adk/flows/llm_flows/base_llm_flow.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,10 @@ async def _maybe_add_grounding_metadata(
842842
response: Optional[LlmResponse] = None,
843843
) -> Optional[LlmResponse]:
844844
readonly_context = ReadonlyContext(invocation_context)
845-
tools = await agent.canonical_tools(readonly_context)
845+
if (tools := invocation_context.canonical_tools_cache) is None:
846+
tools = await agent.canonical_tools(readonly_context)
847+
invocation_context.canonical_tools_cache = tools
848+
846849
if not any(tool.name == 'google_search_agent' for tool in tools):
847850
return response
848851
ground_metadata = invocation_context.session.state.get(

0 commit comments

Comments
 (0)