Skip to content

Commit 3734cea

Browse files
xuanyang15copybara-github
authored andcommitted
fix: Use the agent's model when creating Google search agent tool
PiperOrigin-RevId: 819980797
1 parent 86097af commit 3734cea

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

contributing/samples/built_in_multi_tools/agent.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from dotenv import load_dotenv
1919
from google.adk import Agent
20-
from google.adk.tools.google_search_tool import google_search
20+
from google.adk.tools.google_search_tool import GoogleSearchTool
2121
from google.adk.tools.tool_context import ToolContext
2222
from google.adk.tools.vertex_ai_search_tool import VertexAiSearchTool
2323

@@ -57,7 +57,9 @@ def roll_die(sides: int, tool_context: ToolContext) -> int:
5757
""",
5858
tools=[
5959
roll_die,
60-
VertexAiSearchTool(data_store_id=VERTEXAI_DATASTORE_ID),
61-
google_search,
60+
VertexAiSearchTool(
61+
data_store_id=VERTEXAI_DATASTORE_ID, bypass_multi_tools_limit=True
62+
),
63+
GoogleSearchTool(bypass_multi_tools_limit=True),
6264
],
6365
)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ async def _preprocess_async(
438438
from ...agents.llm_agent import LlmAgent
439439

440440
agent = invocation_context.agent
441+
if not isinstance(agent, LlmAgent):
442+
raise TypeError(
443+
f'Expected agent to be an LlmAgent, but got {type(agent)}'
444+
)
441445

442446
# Runs processors.
443447
for processor in self.request_processors:
@@ -468,7 +472,7 @@ async def _preprocess_async(
468472
tools = await _convert_tool_union_to_tools(
469473
tool_union,
470474
ReadonlyContext(invocation_context),
471-
llm_request.model,
475+
agent.model,
472476
multiple_tools,
473477
)
474478
for tool in tools:

tests/unittests/flows/llm_flows/test_base_llm_flow.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
"""Unit tests for BaseLlmFlow toolset integration."""
1616

17-
from typing import Optional
17+
from unittest import mock
1818
from unittest.mock import AsyncMock
1919

20-
from google.adk.agents.callback_context import CallbackContext
2120
from google.adk.agents.llm_agent import Agent
2221
from google.adk.events.event import Event
2322
from google.adk.flows.llm_flows.base_llm_flow import BaseLlmFlow
23+
from google.adk.models.google_llm import Gemini
2424
from google.adk.models.llm_request import LlmRequest
2525
from google.adk.models.llm_response import LlmResponse
2626
from google.adk.plugins.base_plugin import BasePlugin
@@ -95,7 +95,6 @@ async def close(self):
9595
async def test_preprocess_handles_mixed_tools_and_toolsets():
9696
"""Test that _preprocess_async properly handles both tools and toolsets."""
9797
from google.adk.tools.base_tool import BaseTool
98-
from google.adk.tools.function_tool import FunctionTool
9998

10099
# Create a mock tool
101100
class _MockTool(BaseTool):
@@ -200,6 +199,46 @@ def _my_tool(sides: int) -> int:
200199
assert {d.name for d in declarations} == {'_my_tool', 'google_search_agent'}
201200

202201

202+
@pytest.mark.asyncio
203+
async def test_preprocess_calls_convert_tool_union_to_tools():
204+
"""Test that _preprocess_async calls _convert_tool_union_to_tools."""
205+
206+
class _MockTool:
207+
process_llm_request = AsyncMock()
208+
209+
mock_tool_instance = _MockTool()
210+
211+
def _my_tool(sides: int) -> int:
212+
"""A simple tool."""
213+
return sides
214+
215+
with mock.patch(
216+
'google.adk.agents.llm_agent._convert_tool_union_to_tools',
217+
new_callable=AsyncMock,
218+
) as mock_convert:
219+
mock_convert.return_value = [mock_tool_instance]
220+
221+
model = Gemini(model='gemini-2')
222+
agent = Agent(
223+
name='test_agent', model=model, tools=[_my_tool, google_search]
224+
)
225+
invocation_context = await testing_utils.create_invocation_context(
226+
agent=agent, user_content='test message'
227+
)
228+
flow = BaseLlmFlowForTesting()
229+
llm_request = LlmRequest(model='gemini-2')
230+
231+
async for _ in flow._preprocess_async(invocation_context, llm_request):
232+
pass
233+
234+
mock_convert.assert_called_with(
235+
google_search,
236+
mock.ANY, # ReadonlyContext(invocation_context)
237+
model,
238+
True, # multiple_tools
239+
)
240+
241+
203242
# TODO(b/448114567): Remove the following
204243
# test_handle_after_model_callback_grounding tests once the workaround
205244
# is no longer needed.

0 commit comments

Comments
 (0)