Skip to content

Commit 4facb2c

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

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/google/adk/agents/invocation_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from pydantic import Field
2525
from pydantic import PrivateAttr
2626

27+
from google.adk.tools.base_tool import BaseTool
28+
2729
from ..apps.app import ResumabilityConfig
2830
from ..artifacts.base_artifact_service import BaseArtifactService
2931
from ..auth.credential_service.base_credential_service import BaseCredentialService
@@ -202,6 +204,9 @@ class InvocationContext(BaseModel):
202204
plugin_manager: PluginManager = Field(default_factory=PluginManager)
203205
"""The manager for keeping track of plugins in this invocation."""
204206

207+
canonical_tools_cache: Optional[list[BaseTool]] = None
208+
"""The cache of canonical tools for this invocation."""
209+
205210
_invocation_cost_manager: _InvocationCostManager = PrivateAttr(
206211
default_factory=_InvocationCostManager
207212
)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,12 @@ 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 invocation_context.canonical_tools_cache is None:
846+
tools = await agent.canonical_tools(readonly_context)
847+
invocation_context.canonical_tools_cache = tools
848+
else:
849+
tools = invocation_context.canonical_tools_cache
850+
846851
if not any(tool.name == 'google_search_agent' for tool in tools):
847852
return response
848853
ground_metadata = invocation_context.session.state.get(

0 commit comments

Comments
 (0)