From d419af28d3e9806ba3c27fb03606acabdbaa275f Mon Sep 17 00:00:00 2001 From: chelsealong Date: Sat, 12 Sep 2026 17:45:41 +0000 Subject: [PATCH] fix: raise clear ImportError when VertexAiSearchTool bypass needs google-adk[gcp] Setting bypass_multi_tools_limit=True on VertexAiSearchTool swaps it for DiscoveryEngineSearchTool, which requires the google-cloud-discoveryengine package. Previously the missing dependency surfaced as a raw ModuleNotFoundError from deep inside the package with no hint at what to install. Now the import failure is caught and re-raised with a message pointing to `pip install google-adk[gcp]`. Fixes #7100 (partially: the dependency-error portion of the report). --- src/google/adk/agents/llm_agent.py | 11 ++++++++-- .../unittests/agents/test_llm_agent_fields.py | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/google/adk/agents/llm_agent.py b/src/google/adk/agents/llm_agent.py index 51205a4d36f..ac2410d08ee 100644 --- a/src/google/adk/agents/llm_agent.py +++ b/src/google/adk/agents/llm_agent.py @@ -167,10 +167,17 @@ async def _convert_tool_union_to_tools( # other tools. # TODO: Remove once the workaround is no longer needed. if multiple_tools and isinstance(tool_union, VertexAiSearchTool): - from ..tools.discovery_engine_search_tool import DiscoveryEngineSearchTool - vais_tool = tool_union if vais_tool.bypass_multi_tools_limit: + try: + from ..tools.discovery_engine_search_tool import DiscoveryEngineSearchTool + except ImportError as e: + raise ImportError( + 'VertexAiSearchTool with bypass_multi_tools_limit=True requires' + ' the google-cloud-discoveryengine package. Install it with' + ' `pip install google-adk[gcp]`.' + ) from e + return [ DiscoveryEngineSearchTool( data_store_id=vais_tool.data_store_id, diff --git a/tests/unittests/agents/test_llm_agent_fields.py b/tests/unittests/agents/test_llm_agent_fields.py index 78eb92524f8..0878262dc17 100644 --- a/tests/unittests/agents/test_llm_agent_fields.py +++ b/tests/unittests/agents/test_llm_agent_fields.py @@ -595,6 +595,28 @@ async def test_handle_vais_with_other_tools(self): assert tools[1].name == 'discovery_engine_search' assert tools[1].__class__.__name__ == 'DiscoveryEngineSearchTool' + async def test_handle_vais_with_other_tools_missing_gcp_extra(self): + """Missing google-cloud-discoveryengine raises an actionable error.""" + agent = LlmAgent( + name='test_agent', + model='gemini-pro', + tools=[ + self._my_tool, + VertexAiSearchTool( + data_store_id='test_data_store_id', + bypass_multi_tools_limit=True, + ), + ], + ) + ctx = await _create_readonly_context(agent) + + with mock.patch.dict( + 'sys.modules', + {'google.adk.tools.discovery_engine_search_tool': None}, + ): + with pytest.raises(ImportError, match='google-adk\\[gcp\\]'): + await agent.canonical_tools(ctx) + async def test_handle_vais_with_other_tools_no_bypass(self): """Test that VertexAiSearchTool is not replaced.""" agent = LlmAgent(