@@ -142,38 +142,39 @@ async def _convert_tool_union_to_tools(
142142 model : Union [str , BaseLlm ],
143143 multiple_tools : bool = False ,
144144) -> list [BaseTool ]:
145+ from ..tools .enterprise_search_tool import EnterpriseWebSearchTool
145146 from ..tools .google_search_tool import GoogleSearchTool
146147 from ..tools .vertex_ai_search_tool import VertexAiSearchTool
147148
148- # Wrap google_search tool with AgentTool if there are multiple tools because
149- # the built-in tools cannot be used together with other tools.
149+ # Handle built-in tool workarounds when multiple tools are present.
150+ # Built-in tools cannot be used together with other tools, so we wrap or
151+ # replace them with compatible alternatives.
150152 # TODO(b/448114567): Remove once the workaround is no longer needed.
151- if multiple_tools and isinstance (tool_union , GoogleSearchTool ):
152- from ..tools .google_search_agent_tool import create_google_search_agent
153- from ..tools .google_search_agent_tool import GoogleSearchAgentTool
154-
155- search_tool = cast (GoogleSearchTool , tool_union )
156- if search_tool .bypass_multi_tools_limit :
157- return [GoogleSearchAgentTool (create_google_search_agent (model ))]
158-
159- # Replace VertexAiSearchTool with DiscoveryEngineSearchTool if there are
160- # multiple tools because the built-in tools cannot be used together with
161- # other tools.
162- # TODO(b/448114567): Remove once the workaround is no longer needed.
163- if multiple_tools and isinstance (tool_union , VertexAiSearchTool ):
164- from ..tools .discovery_engine_search_tool import DiscoveryEngineSearchTool
165-
166- vais_tool = cast (VertexAiSearchTool , tool_union )
167- if vais_tool .bypass_multi_tools_limit :
168- return [
169- DiscoveryEngineSearchTool (
170- data_store_id = vais_tool .data_store_id ,
171- data_store_specs = vais_tool .data_store_specs ,
172- search_engine_id = vais_tool .search_engine_id ,
173- filter = vais_tool .filter ,
174- max_results = vais_tool .max_results ,
175- )
176- ]
153+ if multiple_tools :
154+ tool_workarounds = [
155+ # GoogleSearchTool: wrap with AgentTool
156+ {
157+ 'tool_class' : GoogleSearchTool ,
158+ 'handler' : lambda : _handle_google_search_tool (tool_union , model ),
159+ },
160+ # VertexAiSearchTool: replace with DiscoveryEngineSearchTool
161+ {
162+ 'tool_class' : VertexAiSearchTool ,
163+ 'handler' : lambda : _handle_vertex_ai_search_tool (tool_union ),
164+ },
165+ # EnterpriseWebSearchTool: wrap with AgentTool
166+ {
167+ 'tool_class' : EnterpriseWebSearchTool ,
168+ 'handler' : lambda : _handle_enterprise_search_tool (
169+ tool_union , model
170+ ),
171+ },
172+ ]
173+
174+ for workaround in tool_workarounds :
175+ if isinstance (tool_union , workaround ['tool_class' ]):
176+ if tool_union .bypass_multi_tools_limit :
177+ return workaround ['handler' ]()
177178
178179 if isinstance (tool_union , BaseTool ):
179180 return [tool_union ]
@@ -192,6 +193,43 @@ async def _convert_tool_union_to_tools(
192193 return []
193194
194195
196+ def _handle_google_search_tool (
197+ tool_union : ToolUnion , model : Union [str , BaseLlm ]
198+ ) -> list [BaseTool ]:
199+ """Handle GoogleSearchTool workaround by wrapping with AgentTool."""
200+ from ..tools .google_search_agent_tool import create_google_search_agent
201+ from ..tools .google_search_agent_tool import GoogleSearchAgentTool
202+
203+ return [GoogleSearchAgentTool (create_google_search_agent (model ))]
204+
205+
206+ def _handle_vertex_ai_search_tool (tool_union : ToolUnion ) -> list [BaseTool ]:
207+ """Handle VertexAiSearchTool workaround by replacing with DiscoveryEngineSearchTool."""
208+ from ..tools .discovery_engine_search_tool import DiscoveryEngineSearchTool
209+ from ..tools .vertex_ai_search_tool import VertexAiSearchTool
210+
211+ vais_tool = cast (VertexAiSearchTool , tool_union )
212+ return [
213+ DiscoveryEngineSearchTool (
214+ data_store_id = vais_tool .data_store_id ,
215+ data_store_specs = vais_tool .data_store_specs ,
216+ search_engine_id = vais_tool .search_engine_id ,
217+ filter = vais_tool .filter ,
218+ max_results = vais_tool .max_results ,
219+ )
220+ ]
221+
222+
223+ def _handle_enterprise_search_tool (
224+ tool_union : ToolUnion , model : Union [str , BaseLlm ]
225+ ) -> list [BaseTool ]:
226+ """Handle EnterpriseWebSearchTool workaround by wrapping with AgentTool."""
227+ from ..tools .enterprise_search_agent_tool import create_enterprise_search_agent
228+ from ..tools .enterprise_search_agent_tool import EnterpriseSearchAgentTool
229+
230+ return [EnterpriseSearchAgentTool (create_enterprise_search_agent (model ))]
231+
232+
195233class LlmAgent (BaseAgent ):
196234 """LLM-based Agent."""
197235
0 commit comments