Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions google/genai/_extra_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'
Expand All @@ -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(
Expand Down
45 changes: 45 additions & 0 deletions google/genai/tests/afc/test_convert_if_exist_pydantic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
10 changes: 10 additions & 0 deletions google/genai/tests/afc/test_invoke_function_from_dict_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down