2424T = TypeVar ("T" , bound = pydantic .BaseModel )
2525
2626
27+ def _validate_gemini_tools (gemini_tools : list [genai .types .Tool ]) -> None :
28+ """Validate that gemini_tools does not contain FunctionDeclarations.
29+
30+ Gemini-specific tools should only include tools that cannot be represented
31+ as FunctionDeclarations (e.g., GoogleSearch, CodeExecution, ComputerUse).
32+ Standard function calling tools should use the tools interface instead.
33+
34+ Args:
35+ gemini_tools: List of Gemini tools to validate
36+
37+ Raises:
38+ ValueError: If any tool contains function_declarations
39+ """
40+ for tool in gemini_tools :
41+ # Check if the tool has function_declarations attribute and it's not empty
42+ if hasattr (tool , "function_declarations" ) and tool .function_declarations :
43+ raise ValueError (
44+ "gemini_tools should not contain FunctionDeclarations. "
45+ "Use the standard tools interface for function calling tools. "
46+ "gemini_tools is reserved for Gemini-specific tools like "
47+ "GoogleSearch, CodeExecution, ComputerUse, UrlContext, and FileSearch."
48+ )
49+
50+
2751class GeminiModel (Model ):
2852 """Google Gemini model provider implementation.
2953
@@ -40,10 +64,16 @@ class GeminiConfig(TypedDict, total=False):
4064 params: Additional model parameters (e.g., temperature).
4165 For a complete list of supported parameters, see
4266 https://ai.google.dev/api/generate-content#generationconfig.
67+ gemini_tools: Gemini-specific tools that are not FunctionDeclarations
68+ (e.g., GoogleSearch, CodeExecution, ComputerUse, UrlContext, FileSearch).
69+ Use the standard tools interface for function calling tools.
70+ For a complete list of supported tools, see
71+ https://ai.google.dev/api/caching#Tool
4372 """
4473
4574 model_id : Required [str ]
4675 params : dict [str , Any ]
76+ gemini_tools : list [genai .types .Tool ]
4777
4878 def __init__ (
4979 self ,
@@ -61,6 +91,10 @@ def __init__(
6191 validate_config_keys (model_config , GeminiModel .GeminiConfig )
6292 self .config = GeminiModel .GeminiConfig (** model_config )
6393
94+ # Validate gemini_tools if provided
95+ if "gemini_tools" in self .config :
96+ _validate_gemini_tools (self .config ["gemini_tools" ])
97+
6498 logger .debug ("config=<%s> | initializing" , self .config )
6599
66100 self .client_args = client_args or {}
@@ -72,6 +106,10 @@ def update_config(self, **model_config: Unpack[GeminiConfig]) -> None: # type:
72106 Args:
73107 **model_config: Configuration overrides.
74108 """
109+ # Validate gemini_tools if provided
110+ if "gemini_tools" in model_config :
111+ _validate_gemini_tools (model_config ["gemini_tools" ])
112+
75113 self .config .update (model_config )
76114
77115 @override
@@ -181,7 +219,7 @@ def _format_request_tools(self, tool_specs: Optional[list[ToolSpec]]) -> list[ge
181219 Return:
182220 Gemini tool list.
183221 """
184- return [
222+ tools = [
185223 genai .types .Tool (
186224 function_declarations = [
187225 genai .types .FunctionDeclaration (
@@ -193,6 +231,9 @@ def _format_request_tools(self, tool_specs: Optional[list[ToolSpec]]) -> list[ge
193231 ],
194232 ),
195233 ]
234+ if self .config .get ("gemini_tools" ):
235+ tools .extend (self .config .get ("gemini_tools" , []))
236+ return tools
196237
197238 def _format_request_config (
198239 self ,
0 commit comments