From 9f0745436120a26f7da15ff8443e941d984b0912 Mon Sep 17 00:00:00 2001 From: "Navid Khan (Ali)" <60997541+Sun-Wukhan@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:43:31 -0500 Subject: [PATCH] fix(afc): accept whole numbers for Optional[float] tool parameters --- google/genai/_extra_utils.py | 9 ++++ .../test_convert_if_exist_pydantic_model.py | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index 9a1554f9f..6acca1580 100644 --- a/google/genai/_extra_utils.py +++ b/google/genai/_extra_utils.py @@ -313,6 +313,15 @@ def convert_if_exist_pydantic_model( (get_args(arg) and get_origin(arg) is list) or isinstance(value, arg) or (isinstance(value, dict) and is_annotation_pydantic_model(arg)) + # int values are also accepted for a float member of the union, + # mirroring the int to float exception in the non-union path + # below. bool is excluded because it is an int subclass and a + # bool member may appear later in the union. + or ( + isinstance(value, int) + and not isinstance(value, bool) + and arg is float + ) ): try: return convert_if_exist_pydantic_model( 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..9653cca14 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 @@ -59,6 +59,51 @@ def test_value_int_annotation_float(): ) +def test_value_int_annotation_optional_float(): + # Whole numbers arrive as int after + # convert_number_values_for_function_call_args, and must still be + # accepted when float appears inside a union annotation. + assert ( + convert_if_exist_pydantic_model( + 21, Optional[float], 'param_name', 'func_name' + ) + == 21 + ) + assert ( + convert_if_exist_pydantic_model( + 21, Union[float, None], 'param_name', 'func_name' + ) + == 21 + ) + # The int member is written first, so it wins for an int value. + assert ( + convert_if_exist_pydantic_model( + 21, Union[int, float], 'param_name', 'func_name' + ) + == 21 + ) + assert ( + convert_if_exist_pydantic_model( + None, Optional[float], 'param_name', 'func_name' + ) + is None + ) + assert ( + convert_if_exist_pydantic_model( + 21.5, Optional[float], 'param_name', 'func_name' + ) + == 21.5 + ) + # | in 3.10+ + if sys.version_info >= (3, 10): + assert ( + convert_if_exist_pydantic_model( + 21, float | None, 'param_name', 'func_name' + ) + == 21 + ) + + def test_union_types(): assert ( convert_if_exist_pydantic_model(