Skip to content

Commit 064f0d2

Browse files
wukathcopybara-github
authored andcommitted
fix: Disable tool caching for skill toolset
Skill toolset's tools can change dynamically within a single invocation (when a skill is loaded) Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 893666326
1 parent 16a1a18 commit 064f0d2

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/google/adk/tools/base_toolset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __init__(
8282
self.tool_name_prefix = tool_name_prefix
8383
self._cached_invocation_id: Optional[str] = None
8484
self._cached_prefixed_tools: Optional[list[BaseTool]] = None
85+
self._use_invocation_cache = True
8586

8687
@abstractmethod
8788
async def get_tools(
@@ -117,7 +118,8 @@ async def get_tools_with_prefix(
117118
invocation_id = readonly_context.invocation_id if readonly_context else None
118119

119120
if (
120-
self._cached_prefixed_tools is not None
121+
self._use_invocation_cache
122+
and self._cached_prefixed_tools is not None
121123
and self._cached_invocation_id == invocation_id
122124
):
123125
return self._cached_prefixed_tools

src/google/adk/tools/skill_toolset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ def __init__(
700700
self._skills = {skill.name: skill for skill in skills}
701701
self._code_executor = code_executor
702702
self._script_timeout = script_timeout
703+
self._use_invocation_cache = False
703704

704705
self._provided_tools_by_name = {}
705706
self._provided_toolsets = []

tests/unittests/tools/test_base_toolset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,13 @@ async def test_get_tools_with_prefix_caching():
438438
assert len(tools3) == 1
439439
assert tools3 is not tools1 # Should be a new list instance
440440
assert tools3[0].name == 'test_tool1'
441+
442+
# Test disabling caching
443+
toolset._use_invocation_cache = False
444+
tools4 = await toolset.get_tools_with_prefix(
445+
readonly_context=readonly_context2
446+
)
447+
tools5 = await toolset.get_tools_with_prefix(
448+
readonly_context=readonly_context2
449+
)
450+
assert tools4 is not tools5

tests/unittests/tools/test_skill_toolset.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,16 +1340,17 @@ def my_func():
13401340

13411341
ctx = _make_tool_context_with_agent()
13421342
# Initial tools (only core)
1343-
tools = await toolset.get_tools(readonly_context=ctx)
1344-
assert len(tools) == 4
1343+
tools1 = await toolset.get_tools_with_prefix(readonly_context=ctx)
1344+
assert len(tools1) == 4
13451345

13461346
# Activate skills
13471347
load_tool = skill_toolset.LoadSkillTool(toolset)
13481348
await load_tool.run_async(args={"name": "skill1"}, tool_context=ctx)
13491349
await load_tool.run_async(args={"name": "skill2"}, tool_context=ctx)
13501350

13511351
# Dynamic tools should now be resolved
1352-
tools = await toolset.get_tools(readonly_context=ctx)
1352+
tools = await toolset.get_tools_with_prefix(readonly_context=ctx)
1353+
assert tools is not tools1
13531354
tool_names = {t.name for t in tools}
13541355

13551356
# Core tools

0 commit comments

Comments
 (0)