fix(tools): handle Union[ModelA, ModelB] in FunctionTool._preprocess_args#5813
Open
devteamaegis wants to merge 2 commits into
Open
fix(tools): handle Union[ModelA, ModelB] in FunctionTool._preprocess_args#5813devteamaegis wants to merge 2 commits into
devteamaegis wants to merge 2 commits into
Conversation
…args When a tool parameter is typed as Union[PydanticA, PydanticB] the old code only handled Optional[T] (exactly one non-None union member). With two or more concrete types, target_type was left as the raw Union, which is not a class, so the issubclass(target_type, pydantic.BaseModel) guard was never entered and the dict was passed through unconverted — causing downstream isinstance() failures. The fix adds an elif branch for len(non_none_types) > 1 that iterates the Pydantic members in declaration order and calls model_validate() on the first one that accepts the dict, mirroring the Optional[T] path. Fixes google#5799
…ss_args Covers the fix for issue google#5799 where dicts passed as Union[PydanticA, PydanticB] arguments were not converted to model instances. Added five new test cases: - dict matching first Union branch -> first model - dict matching second Union branch -> second model - already-correct instance passes through unchanged - Optional[T] backward-compatibility still holds - run_async end-to-end with both Union branches
Collaborator
|
Response from ADK Triaging Agent Hello @devteamaegis, thank you for creating this PR! This PR is a great fix for the issue with
Thank you for your valuable contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
FunctionTool._preprocess_argssilently skips dict-to-Pydantic conversion when aparameter is typed as
Union[ModelA, ModelB](two or more non-Nonemembers).The existing code only handled
Optional[T]— i.e. exactly one non-Nonetype in the union:
Reported in #5799. Real-world failure:
Fix
Add an
elif len(non_none_types) > 1branch that, when the value is adict,iterates the Pydantic members of the union in declaration order and calls
model_validate()on the first one that accepts the payload — identical strategyto the existing
Optional[T]path.Tests
Five new cases added to
tests/unittests/tools/test_function_tool_pydantic.py:test_preprocess_args_union_first_branch_convertedtest_preprocess_args_union_second_branch_convertedtest_preprocess_args_union_already_correct_type_unchangedtest_preprocess_args_union_optional_backward_compatOptional[T]still workstest_run_async_union_end_to_endrun_asyncFixes #5799