5959}
6060
6161
62- def _get_fields_dict (func : Callable ) -> Dict :
62+ def _get_fields_dict (func : Callable [..., Any ] ) -> Dict [ str , Any ] :
6363 param_signature = dict (inspect .signature (func ).parameters )
6464 fields_dict = {
6565 name : (
@@ -94,7 +94,7 @@ def _get_fields_dict(func: Callable) -> Dict:
9494 return fields_dict
9595
9696
97- def _annotate_nullable_fields (schema : Dict ) :
97+ def _annotate_nullable_fields (schema : Dict [ str , Any ]) -> None :
9898 for _ , property_schema in schema .get ('properties' , {}).items ():
9999 # for Optional[T], the pydantic schema is:
100100 # {
@@ -117,7 +117,7 @@ def _annotate_nullable_fields(schema: Dict):
117117 break
118118
119119
120- def _annotate_required_fields (schema : Dict ) :
120+ def _annotate_required_fields (schema : Dict [ str , Any ]) -> None :
121121 required = [
122122 field_name
123123 for field_name , field_schema in schema .get ('properties' , {}).items ()
@@ -126,7 +126,7 @@ def _annotate_required_fields(schema: Dict):
126126 schema ['required' ] = required
127127
128128
129- def _remove_any_of (schema : Dict ) :
129+ def _remove_any_of (schema : Dict [ str , Any ]) -> None :
130130 for _ , property_schema in schema .get ('properties' , {}).items ():
131131 union_types = property_schema .pop ('anyOf' , None )
132132 # Take the first non-null type.
@@ -136,17 +136,17 @@ def _remove_any_of(schema: Dict):
136136 property_schema .update (type_ )
137137
138138
139- def _remove_default (schema : Dict ) :
139+ def _remove_default (schema : Dict [ str , Any ]) -> None :
140140 for _ , property_schema in schema .get ('properties' , {}).items ():
141141 property_schema .pop ('default' , None )
142142
143143
144- def _remove_nullable (schema : Dict ) :
144+ def _remove_nullable (schema : Dict [ str , Any ]) -> None :
145145 for _ , property_schema in schema .get ('properties' , {}).items ():
146146 property_schema .pop ('nullable' , None )
147147
148148
149- def _remove_title (schema : Dict ) :
149+ def _remove_title (schema : Dict [ str , Any ]) -> None :
150150 for _ , property_schema in schema .get ('properties' , {}).items ():
151151 property_schema .pop ('title' , None )
152152
@@ -162,7 +162,9 @@ def _get_pydantic_schema(func: Callable) -> Dict:
162162 return pydantic .create_model (func .__name__ , ** fields_dict ).model_json_schema ()
163163
164164
165- def _process_pydantic_schema (vertexai : bool , schema : Dict ) -> Dict :
165+ def _process_pydantic_schema (
166+ vertexai : bool , schema : Dict [str , Any ]
167+ ) -> Dict [str , Any ]:
166168 _annotate_nullable_fields (schema )
167169 _annotate_required_fields (schema )
168170 if not vertexai :
@@ -173,7 +175,9 @@ def _process_pydantic_schema(vertexai: bool, schema: Dict) -> Dict:
173175 return schema
174176
175177
176- def _map_pydantic_type_to_property_schema (property_schema : Dict ):
178+ def _map_pydantic_type_to_property_schema (
179+ property_schema : Dict [str , Any ],
180+ ) -> None :
177181 if 'type' in property_schema :
178182 property_schema ['type' ] = _py_type_2_schema_type .get (
179183 property_schema ['type' ], 'TYPE_UNSPECIFIED'
@@ -190,20 +194,20 @@ def _map_pydantic_type_to_property_schema(property_schema: Dict):
190194 property_schema ['type' ] = type_ ['type' ]
191195
192196
193- def _map_pydantic_type_to_schema_type (schema : Dict ) :
197+ def _map_pydantic_type_to_schema_type (schema : Dict [ str , Any ]) -> None :
194198 for _ , property_schema in schema .get ('properties' , {}).items ():
195199 _map_pydantic_type_to_property_schema (property_schema )
196200
197201
198- def _get_return_type (func : Callable ) -> Any :
202+ def _get_return_type (func : Callable [..., Any ] ) -> Any :
199203 return _py_type_2_schema_type .get (
200204 inspect .signature (func ).return_annotation .__name__ ,
201205 inspect .signature (func ).return_annotation .__name__ ,
202206 )
203207
204208
205209def build_function_declaration (
206- func : Union [Callable , BaseModel ],
210+ func : Union [Callable [..., Any ] , BaseModel ],
207211 ignore_params : Optional [list [str ]] = None ,
208212 variant : GoogleLLMVariant = GoogleLLMVariant .GEMINI_API ,
209213) -> types .FunctionDeclaration :
@@ -222,45 +226,41 @@ def build_function_declaration(
222226
223227 # ========== ADK defined function tool declaration (old behavior) ==========
224228 signature = inspect .signature (func )
225- should_update_signature = False
226- new_func = None
227229 if not ignore_params :
228230 ignore_params = []
229- for name , _ in signature .parameters .items ():
230- if name in ignore_params :
231- should_update_signature = True
232- break
233- if should_update_signature :
234- new_params = [
235- param
231+ should_update_signature = any (
232+ name in ignore_params for name in signature .parameters
233+ )
234+ if not should_update_signature :
235+ return from_function_with_options (func , variant )
236+
237+ if isinstance (func , type ):
238+ fields = {
239+ name : (param .annotation , param .default )
236240 for name , param in signature .parameters .items ()
237241 if name not in ignore_params
238- ]
239- if isinstance (func , type ):
240- fields = {
241- name : (param .annotation , param .default )
242- for name , param in signature .parameters .items ()
243- if name not in ignore_params
244- }
245- new_func = create_model (func .__name__ , ** fields )
246- else :
247- new_sig = signature .replace (parameters = new_params )
248- new_func = FunctionType (
249- func .__code__ ,
250- func .__globals__ ,
251- func .__name__ ,
252- func .__defaults__ ,
253- func .__closure__ ,
254- )
255- new_func .__signature__ = new_sig
256- new_func .__doc__ = func .__doc__
257- new_func .__annotations__ = func .__annotations__
258-
259- return (
260- from_function_with_options (func , variant )
261- if not should_update_signature
262- else from_function_with_options (new_func , variant )
242+ }
243+ return from_function_with_options (
244+ create_model (func .__name__ , ** fields ), variant
245+ )
246+
247+ new_params = [
248+ param
249+ for name , param in signature .parameters .items ()
250+ if name not in ignore_params
251+ ]
252+ new_sig = signature .replace (parameters = new_params )
253+ new_func = FunctionType (
254+ func .__code__ ,
255+ func .__globals__ ,
256+ func .__name__ ,
257+ func .__defaults__ ,
258+ func .__closure__ ,
263259 )
260+ setattr (new_func , '__signature__' , new_sig )
261+ new_func .__doc__ = func .__doc__
262+ new_func .__annotations__ = func .__annotations__
263+ return from_function_with_options (new_func , variant )
264264
265265
266266def build_function_declaration_for_langchain (
@@ -316,7 +316,7 @@ def build_function_declaration_util(
316316
317317
318318def from_function_with_options (
319- func : Callable ,
319+ func : Callable [..., Any ] ,
320320 variant : GoogleLLMVariant = GoogleLLMVariant .GEMINI_API ,
321321) -> 'types.FunctionDeclaration' :
322322
0 commit comments