Skip to content

Commit e360241

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: prevent crash when parsing default values for typing.Any parameters
PiperOrigin-RevId: 941154780
1 parent 17d5f38 commit e360241

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/google/adk/tools/_function_parameter_parse_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ def _is_default_value_compatible(
222222
default_value: Any, annotation: inspect.Parameter.annotation
223223
) -> bool:
224224
# None type is expected to be handled external to this function
225+
if annotation is Any:
226+
return True
225227
if _is_builtin_primitive_or_compound(annotation):
226228
return isinstance(default_value, annotation)
227229

tests/unittests/tools/test_from_function_with_options.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,19 @@ def generate_image(
519519
assert array_schema.items.items.any_of[0].type == types.Type.STRING
520520
assert array_schema.items.items.any_of[0].format is None
521521
assert array_schema.items.items.any_of[1].type == types.Type.STRING
522+
523+
524+
def test_from_function_with_options_any_type_with_default_value():
525+
"""Test that typing.Any with a default value works and doesn't crash."""
526+
527+
def my_tool(param: Any = 'default_string') -> str:
528+
return f'ok {param}'
529+
530+
declaration = _automatic_function_calling_util.from_function_with_options(
531+
my_tool, GoogleLLMVariant.GEMINI_API
532+
)
533+
534+
assert declaration.parameters is not None
535+
assert declaration.parameters.properties['param'].default == 'default_string'
536+
# Any type maps to None (no type) in schema
537+
assert declaration.parameters.properties['param'].type is None

0 commit comments

Comments
 (0)