fix: preserve Annotated metadata in function tool declarations and arg conversion - #6845
Closed
MarlzRana wants to merge 1 commit into
Closed
fix: preserve Annotated metadata in function tool declarations and arg conversion#6845MarlzRana wants to merge 1 commit into
MarlzRana wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
MarlzRana
force-pushed
the
fix/annotated-function-tool-params
branch
from
August 21, 2026 16:02
92e304d to
83e0c11
Compare
…g Conversion - Pass include_extras=True to get_type_hints in the JSON-schema declaration builder so Annotated[T, Field(...)] descriptions and constraints reach the generated schema, matching the legacy builder - Unwrap Annotated in FunctionTool._preprocess_args (top-level and per union member) so dict-to-model conversion works when get_type_hints falls back to the raw annotation - Add tests covering both builders and the resolved and fallback arg paths
MarlzRana
force-pushed
the
fix/annotated-function-tool-params
branch
from
August 21, 2026 16:03
83e0c11 to
caa6fac
Compare
Contributor
Author
|
I will re-open this later after a bit more polish. |
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.
Link to Issue or Description of Change
2. Or, if no issue exists, describe the change:
Problem:
When the
JSON_SCHEMA_FOR_FUNC_DECLfeature is enabled,typing.Annotatedmetadata was silently dropped in two places:Declaration builder.
_function_tool_declarations._get_function_fieldscalledget_type_hints()withoutinclude_extras=True, soAnnotated[T, Field(description=..., ge=1)]was unwrapped to a bareTbefore Pydantic'screate_modelsaw it. Every description and constraint an author attached viaAnnotatedwas missing from the schema shown to the model. The legacy builder never had this problem, so this was a regression between the two paths.Argument conversion.
FunctionTool._preprocess_argsresolves parameter types withget_type_hints(), which stripsAnnotatedfor free. But whenget_type_hints()cannot resolve a signature (an unresolvable forward reference anywhere in it — e.g. aTYPE_CHECKING-only import or a recursive type alias), it raises and the code falls back to the rawparam.annotation, which is stillAnnotated-wrapped. None of the conversion branches recognised the wrapper, so JSON dicts from the model reached the function body unconverted (laterAttributeError).Solution:
include_extras=Truetoget_type_hints()in the JSON-schema declaration builder (parameter and forward-ref return paths), soAnnotatedmetadata reaches the generated schema — matching the legacy builder.Annotatedin_preprocess_args, both at the top level and per union member (Optional[Annotated[T, ...]]hides the wrapper inside the union), so dict-to-model conversion works on the fallback path too.Known limitations left out of scope:
list[Annotated[Model, ...]]on the fallback path (degrades gracefully — passes the dict through, no crash), and a top-levelField(alias=...)now renaming a schema property. Both are documented for follow-up.Testing Plan
Unit Tests:
Added 16 tests across three existing files:
test_function_tool_declarations.py—Annotatedmetadata (descriptions, constraints, defaults, optional models, nested lists, return type) reaches the JSON schema.test_build_function_declaration.py— parity pair asserting the legacy and JSON-schema builders both preserveAnnotatedmetadata.test_function_tool_pydantic.py—_preprocess_argsconversion on both the resolved and theget_type_hints-fallback paths, plus an end-to-endrun_asynccase.Each fix was verified by reverting it and confirming the corresponding tests fail (the e2e test fails with
AttributeError: 'dict' object has no attribute 'name', the real symptom).Manual End-to-End (E2E) Tests:
Not applicable — the behaviour is fully exercised by the unit tests, including an end-to-end
run_asyncinvocation that asserts the wrapped function receives a real Pydantic model instance rather than a dict.Checklist
🤖 Generated with Claude Code