From 223cee40d277f7897e8d06f6bea74831db339c52 Mon Sep 17 00:00:00 2001 From: Roxen-AS Date: Thu, 3 Dec 2026 05:37:31 +0530 Subject: [PATCH 1/2] fix(afc): accept int arguments for float members in Union annotations Signed-off-by: Roxen-AS --- google/genai/_extra_utils.py | 11 ++++++- .../test_convert_if_exist_pydantic_model.py | 31 +++++++++++++++++++ .../test_invoke_function_from_dict_args.py | 10 ++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index 9a1554f9fd..d40ac9e0c6 100644 --- a/google/genai/_extra_utils.py +++ b/google/genai/_extra_utils.py @@ -312,6 +312,11 @@ def convert_if_exist_pydantic_model( if ( (get_args(arg) and get_origin(arg) is list) or isinstance(value, arg) + or ( + arg is float + and isinstance(value, int) + and not isinstance(value, bool) + ) or (isinstance(value, dict) and is_annotation_pydantic_model(arg)) ): try: @@ -335,7 +340,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 ce73b73837..266bdf45d4 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,37 @@ 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 + ) + 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') 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 440c332610..522245ffb5 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.', From f9c804d1946572cb202470f90c47e131b3521859 Mon Sep 17 00:00:00 2001 From: Roxen-AS Date: Sun, 6 Dec 2026 03:56:34 +0530 Subject: [PATCH 2/2] fix(afc): try exact union matches before int-to-float widening Signed-off-by: Roxen-AS --- google/genai/_extra_utils.py | 16 ++++++++++------ .../afc/test_convert_if_exist_pydantic_model.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index d40ac9e0c6..38bd9de3c2 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( @@ -312,11 +313,6 @@ def convert_if_exist_pydantic_model( if ( (get_args(arg) and get_origin(arg) is list) or isinstance(value, arg) - or ( - arg is float - and isinstance(value, int) - and not isinstance(value, bool) - ) or (isinstance(value, dict) and is_annotation_pydantic_model(arg)) ): try: @@ -327,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' 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 266bdf45d4..540a0d4bdc 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 @@ -116,12 +116,26 @@ def test_union_types(): ) 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():