@@ -296,20 +296,55 @@ def from_function_with_options(
296296) -> 'types.FunctionDeclaration' :
297297
298298 parameters_properties = {}
299- for name , param in inspect .signature (func ).parameters .items ():
300- if param .kind in (
301- inspect .Parameter .POSITIONAL_OR_KEYWORD ,
302- inspect .Parameter .KEYWORD_ONLY ,
303- inspect .Parameter .POSITIONAL_ONLY ,
304- ):
305- # This snippet catches the case when type hints are stored as strings
306- if isinstance (param .annotation , str ):
307- param = param .replace (annotation = typing .get_type_hints (func )[name ])
308-
309- schema = _function_parameter_parse_util ._parse_schema_from_parameter (
310- variant , param , func .__name__
311- )
312- parameters_properties [name ] = schema
299+ parameters_json_schema = {}
300+ annotation_under_future = typing .get_type_hints (func )
301+ try :
302+ for name , param in inspect .signature (func ).parameters .items ():
303+ if param .kind in (
304+ inspect .Parameter .POSITIONAL_OR_KEYWORD ,
305+ inspect .Parameter .KEYWORD_ONLY ,
306+ inspect .Parameter .POSITIONAL_ONLY ,
307+ ):
308+ param = _function_parameter_parse_util ._handle_params_as_deferred_annotations (
309+ param , annotation_under_future , name
310+ )
311+
312+ schema = _function_parameter_parse_util ._parse_schema_from_parameter (
313+ variant , param , func .__name__
314+ )
315+ parameters_properties [name ] = schema
316+ except ValueError :
317+ # If the function has complex parameter types that fail in _parse_schema_from_parameter,
318+ # we try to generate a json schema for the parameter using pydantic.TypeAdapter.
319+ parameters_properties = {}
320+ for name , param in inspect .signature (func ).parameters .items ():
321+ if param .kind in (
322+ inspect .Parameter .POSITIONAL_OR_KEYWORD ,
323+ inspect .Parameter .KEYWORD_ONLY ,
324+ inspect .Parameter .POSITIONAL_ONLY ,
325+ ):
326+ try :
327+ if param .annotation == inspect .Parameter .empty :
328+ param = param .replace (annotation = Any )
329+
330+ param = _function_parameter_parse_util ._handle_params_as_deferred_annotations (
331+ param , annotation_under_future , name
332+ )
333+
334+ _function_parameter_parse_util ._raise_for_invalid_enum_value (param )
335+
336+ json_schema_dict = _function_parameter_parse_util ._generate_json_schema_for_parameter (
337+ param
338+ )
339+
340+ parameters_json_schema [name ] = types .Schema .model_validate (
341+ json_schema_dict
342+ )
343+ except Exception as e :
344+ _function_parameter_parse_util ._raise_for_unsupported_param (
345+ param , func .__name__ , e
346+ )
347+
313348 declaration = types .FunctionDeclaration (
314349 name = func .__name__ ,
315350 description = func .__doc__ ,
@@ -324,6 +359,12 @@ def from_function_with_options(
324359 declaration .parameters
325360 )
326361 )
362+ elif parameters_json_schema :
363+ declaration .parameters = types .Schema (
364+ type = 'OBJECT' ,
365+ properties = parameters_json_schema ,
366+ )
367+
327368 if variant == GoogleLLMVariant .GEMINI_API :
328369 return declaration
329370
@@ -372,17 +413,35 @@ def from_function_with_options(
372413 inspect .Parameter .POSITIONAL_OR_KEYWORD ,
373414 annotation = return_annotation ,
374415 )
375- # This snippet catches the case when type hints are stored as strings
376416 if isinstance (return_value .annotation , str ):
377417 return_value = return_value .replace (
378418 annotation = typing .get_type_hints (func )['return' ]
379419 )
380420
381- declaration .response = (
382- _function_parameter_parse_util ._parse_schema_from_parameter (
383- variant ,
384- return_value ,
385- func .__name__ ,
421+ response_schema : Optional [types .Schema ] = None
422+ response_json_schema : Optional [Union [Dict [str , Any ], types .Schema ]] = None
423+ try :
424+ response_schema = (
425+ _function_parameter_parse_util ._parse_schema_from_parameter (
426+ variant ,
427+ return_value ,
428+ func .__name__ ,
429+ )
430+ )
431+ except ValueError :
432+ try :
433+ response_json_schema = (
434+ _function_parameter_parse_util ._generate_json_schema_for_parameter (
435+ return_value
436+ )
386437 )
387- )
438+ response_json_schema = types .Schema .model_validate (response_json_schema )
439+ except Exception as e :
440+ _function_parameter_parse_util ._raise_for_unsupported_param (
441+ return_value , func .__name__ , e
442+ )
443+ if response_schema :
444+ declaration .response = response_schema
445+ elif response_json_schema :
446+ declaration .response = response_json_schema
388447 return declaration
0 commit comments