Skip to content

Commit 7478bda

Browse files
wukathcopybara-github
authored andcommitted
fix: Parallelize tool resolution in LlmAgent.canonical_tools()
Previously we resolved tools sequentially by awaiting _convert_tool_union_to_tools() in a loop -- reduce the latency by resolving tools concurrently. Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 872979105
1 parent bef3f11 commit 7478bda

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/google/adk/agents/llm_agent.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import asyncio
1718
import importlib
1819
import inspect
1920
import logging
@@ -589,24 +590,27 @@ async def canonical_global_instruction(
589590
return global_instruction, True
590591

591592
async def canonical_tools(
592-
self, ctx: ReadonlyContext = None
593+
self, ctx: Optional[ReadonlyContext] = None
593594
) -> list[BaseTool]:
594595
"""The resolved self.tools field as a list of BaseTool based on the context.
595596
596597
This method is only for use by Agent Development Kit.
597598
"""
598-
resolved_tools = []
599599
# We may need to wrap some built-in tools if there are other tools
600600
# because the built-in tools cannot be used together with other tools.
601601
# TODO(b/448114567): Remove once the workaround is no longer needed.
602602
multiple_tools = len(self.tools) > 1
603603
model = self.canonical_model
604-
for tool_union in self.tools:
605-
resolved_tools.extend(
606-
await _convert_tool_union_to_tools(
607-
tool_union, ctx, model, multiple_tools
608-
)
609-
)
604+
605+
results = await asyncio.gather(*(
606+
_convert_tool_union_to_tools(tool_union, ctx, model, multiple_tools)
607+
for tool_union in self.tools
608+
))
609+
610+
resolved_tools = []
611+
for tools in results:
612+
resolved_tools.extend(tools)
613+
610614
return resolved_tools
611615

612616
@property

tests/unittests/agents/test_llm_agent_fields.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,27 @@ async def test_handle_vais_only(self):
451451
assert tools[0].name == 'vertex_ai_search'
452452
assert tools[0].__class__.__name__ == 'VertexAiSearchTool'
453453

454+
async def test_multiple_tools_resolution(self):
455+
"""Test that multiple tools are resolved correctly."""
456+
457+
def _tool_1():
458+
pass
459+
460+
def _tool_2():
461+
pass
462+
463+
agent = LlmAgent(
464+
name='test_agent',
465+
model='gemini-pro',
466+
tools=[_tool_1, _tool_2],
467+
)
468+
ctx = await _create_readonly_context(agent)
469+
tools = await agent.canonical_tools(ctx)
470+
471+
assert len(tools) == 2
472+
assert tools[0].name == '_tool_1'
473+
assert tools[1].name == '_tool_2'
474+
454475

455476
# Tests for multi-provider model support via string model names
456477
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)