diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index 9a1554f9f..38bd9de3c 100644 --- a/google/genai/_extra_utils.py +++ b/google/genai/_extra_utils.py @@ -300,7 +300,8 @@ def convert_if_exist_pydantic_model( # example 1: typing.Union[int, float] # example 2: int | float equivalent to UnionType[int, float] if get_origin(annotation) in (Union, UnionType): - for arg in get_args(annotation): + args = get_args(annotation) + for arg in args: if isinstance(value, str) and arg in _STRING_ENCODED_TYPES: try: return _convert_string_encoded_value( @@ -322,6 +323,14 @@ def convert_if_exist_pydantic_model( # in the union type. except pydantic.ValidationError: continue + # Fall back to widening int (excluding bool) to float only after exact + # union members have been tried (e.g., Union[float, int]). + if ( + isinstance(value, int) + and not isinstance(value, bool) + and float in args + ): + return value # if none of the union type is matched, raise error raise errors.UnknownFunctionCallArgumentError( f'Failed to parse parameter {param_name} for function' @@ -335,7 +344,11 @@ def convert_if_exist_pydantic_model( ) # the only exception for value and annotation type to be different is int and # float. see convert_number_values_for_function_call_args function for context - if isinstance(value, int) and annotation is float: + if ( + isinstance(value, int) + and not isinstance(value, bool) + and annotation is float + ): return value if not isinstance(value, annotation): raise errors.UnknownFunctionCallArgumentError( diff --git a/google/genai/tests/afc/test_convert_if_exist_pydantic_model.py b/google/genai/tests/afc/test_convert_if_exist_pydantic_model.py index ce73b7383..540a0d4bd 100644 --- a/google/genai/tests/afc/test_convert_if_exist_pydantic_model.py +++ b/google/genai/tests/afc/test_convert_if_exist_pydantic_model.py @@ -91,6 +91,51 @@ def test_union_types(): convert_if_exist_pydantic_model(1, int | float, 'param_name', 'func_name') == 1 ) + assert ( + convert_if_exist_pydantic_model( + 21, float | None, 'param_name', 'func_name' + ) + == 21 + ) + + assert ( + convert_if_exist_pydantic_model( + 21, Optional[float], 'param_name', 'func_name' + ) + == 21 + ) + assert ( + convert_if_exist_pydantic_model( + 21, Union[float, str], 'param_name', 'func_name' + ) + == 21 + ) + assert ( + convert_if_exist_pydantic_model( + True, Union[float, bool], 'param_name', 'func_name' + ) + is True + ) + assert ( + convert_if_exist_pydantic_model( + 1, Union[float, int], 'param_name', 'func_name' + ) + == 1 + ) + with pytest.raises(errors.UnknownFunctionCallArgumentError): + convert_if_exist_pydantic_model( + True, Optional[float], 'param_name', 'func_name' + ) + with pytest.raises(errors.UnknownFunctionCallArgumentError): + convert_if_exist_pydantic_model(True, float, 'param_name', 'func_name') + with pytest.raises(errors.UnknownFunctionCallArgumentError): + convert_if_exist_pydantic_model( + [True], list[float], 'param_name', 'func_name' + ) + with pytest.raises(errors.UnknownFunctionCallArgumentError): + convert_if_exist_pydantic_model( + {'a': True}, dict[str, float], 'param_name', 'func_name' + ) def test_nested_pydantic_model(): diff --git a/google/genai/tests/afc/test_invoke_function_from_dict_args.py b/google/genai/tests/afc/test_invoke_function_from_dict_args.py index 440c33261..522245ffb 100644 --- a/google/genai/tests/afc/test_invoke_function_from_dict_args.py +++ b/google/genai/tests/afc/test_invoke_function_from_dict_args.py @@ -53,6 +53,16 @@ def func_under_test(x: int, y: float, z: str, w: bool): assert actual_response == expected_response +def test_optional_float_accepts_whole_number(): + def set_thermostat(celsius: Union[float, None] = None) -> str: + return f'set to {celsius}' + + assert ( + invoke_function_from_dict_args({'celsius': 21}, set_thermostat) + == 'set to 21' + ) + + @pytest.mark.skipif( sys.version_info < (3, 10), reason='| is only supported in Python 3.9 and above.',