Skip to content

fix(afc): accept int arguments for float members in Union annotations - #3461

Open
Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/afc-union-float-from-int
Open

Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/afc-union-float-from-int

Conversation

@Roxen-AS

@Roxen-AS Roxen-AS commented Dec 3, 2026

Copy link
Copy Markdown

Summary

Fixes #3453.

In convert_if_exist_pydantic_model (google/genai/_extra_utils.py), convert_number_values_for_function_call_args normalizes whole-number floats (like 21.0) to int, and the non-Union path already accepts int for loat annotations. However, the Union branch (Optional[float], loat | None, Union[float, str], etc.) only checked isinstance(value, arg), rejecting whole-number arguments with UnknownFunctionCallArgumentError.

Changes

  • Accept int (excluding �ool) for loat union members in convert_if_exist_pydantic_model.
  • Reject �ool in the plain loat conversion check as well so booleans are not coerced to loat.
  • Added unit tests in google/genai/tests/afc/test_convert_if_exist_pydantic_model.py and google/genai/tests/afc/test_invoke_function_from_dict_args.py.

Testing

  • pytest google/genai/tests/afc --ignore=...stream... -q: 107 passed
  • mypy google/genai/_extra_utils.py: Success: no issues found in 1 source file
  • Verified offline chats.create AFC repro with httpx2.MockTransport.

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>

@Sun-Wukhan Sun-Wukhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down. I confirmed the root cause against main: in the Union branch of convert_if_exist_pydantic_model an int value never satisfies isinstance(value, float), and the int for float exception further down is unreachable for Union annotations because the Union branch raises once the loop finishes without a match. The widening clause in the loop plus the bool exclusion in both places fixes that, and the new tests cover float | None, Optional[float], Union[float, str], and the Union[float, bool] ordering.

Two points worth a look:

  1. The second hunk changes behaviour outside the Union path, and no test pins it. Because bool is a subclass of int, the scalar exception previously also accepted True anywhere a float was expected, including through the container branches: on main, convert_if_exist_pydantic_model([True], list[float]) returns [True] and the same holds for a dict[str, float] value. With this PR both now raise UnknownFunctionCallArgumentError. That looks like the right outcome, but it is a behaviour change a caller could be relying on, so could you add regression tests for the list[float] and dict[str, float] cases so the change is deliberate and stays fixed?

  2. Ordering inside the Union loop: the widening clause runs before an exact type match on a later member is tried. For Union[float, int] an int value is now claimed by the float member before the loop reaches int. The result is identical today because the conversion returns the value unchanged, so this is not a problem in practice, but if the float path ever starts coercing values (for example returning float(value)), the member order would silently change results. Worth a short comment at the clause noting that widening is a fallback, or trying exact matches first.

The invoke level test with Union[float, None] is a good end to end pin for the reported case.

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@Roxen-AS

Roxen-AS commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks @Sun-Wukhan for the review! Addressed both points:

  1. Added regression tests in test_convert_if_exist_pydantic_model.py verifying that list[float] and dict[str, float] reject bool ([True], {'a': True}) with UnknownFunctionCallArgumentError.
  2. Moved the int (excluding bool) -> float fallback in the Union branch after the loop over exact union members so exact matches like Union[float, int] are always attempted first.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L Code changes between 40-100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatic function calling never runs a tool with an Optional[float] parameter when the model passes a whole number

3 participants