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(