fix(tools): handle optional and union pydantic models with string annotations in FunctionTool#6471
Open
hsusul wants to merge 1 commit into
Open
Conversation
…otations in FunctionTool In FunctionTool._preprocess_args, parameter type resolution was inspecting param.annotation directly instead of the target_type hint resolved by get_type_hints. When from __future__ import annotations (or string annotations) is used, param.annotation is a string, causing get_origin(param.annotation) to return None and skip converting dict arguments to Pydantic BaseModel instances for Optional[BaseModel] or BaseModel | None parameters. This fixes the issue by: - Inspecting target_type (from get_type_hints) for Union and UnionType origins - Correctly unwrapping single non-None types so Pydantic BaseModel and list[BaseModel] parameters are preprocessed into model instances regardless of string annotations or PEP 604 union syntax.
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
In
FunctionTool._preprocess_args, parameter type inspection checkedparam.annotation(frominspect.signature) directly rather thantarget_type(resolved viaget_type_hints). Whenfrom __future__ import annotations(or string annotations) is used,param.annotationis astr, causingget_origin(param.annotation)to evaluate toNone. Consequently,FunctionToolfailed to convert dictionary arguments intopydantic.BaseModelinstances for functions withOptional[BaseModel],BaseModel | None, orOptional[list[BaseModel]]parameter annotations, leaving parameters as rawdictobjects and causing runtimeAttributeErrors inside the tool functions.Solution
FunctionTool._preprocess_argsto inspecttarget_type(fromget_type_hints) fortyping.Unionandtypes.UnionTypeorigins.BaseModelandlist[BaseModel]parameters are properly identified and converted into Pydantic model instances regardless of string annotations or PEP 604 union syntax (|).Testing Plan
Added unit tests in
tests/unittests/tools/test_function_tool_with_import_annotations.py:test_preprocess_args_with_optional_pydantic_model_and_annotationstest_preprocess_args_with_pipe_union_pydantic_model_and_annotationstest_preprocess_args_with_optional_list_of_pydantic_models_and_annotationsAll tests pass cleanly:
uv run pytest tests/unittests/tools/test_function_tool*.py uv run pre-commit run --files src/google/adk/tools/function_tool.py tests/unittests/tools/test_function_tool_with_import_annotations.py