fix: preserve Annotated metadata in function tool declarations and arg conversion - #6879
Open
MarlzRana wants to merge 1 commit into
Open
fix: preserve Annotated metadata in function tool declarations and arg conversion#6879MarlzRana wants to merge 1 commit into
MarlzRana wants to merge 1 commit into
Conversation
…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
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
1. Link to an existing issue (if applicable):
_preprocess_argswhen using recursive type aliases or any unresolvable forward reference #6878Problem:
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 (which later led toAttributeError's as described in the original issue on "member access" of the model, which in fact was passed as a raw dict).Solution:
include_extras=Truetoget_type_hints()in the JSON-schema declaration builder (parameter and forward-ref return paths), soAnnotatedmetadata reaches the generated schema which matches the legacy builder as wellAnnotatedin_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.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 stashing it and confirming the corresponding tests that check for correct behavior fail before, and un-stashing fixes.
Manual End-to-End (E2E) Tests:
r.e issue #6877: I went in with the debugger and checked that the FunctionDeclarations in
basic_llm_flow.pynow contain parameter/outputss descriptions and constraints where now there.r.e. issue #6878: I observed the absence of the exception presented in the issue, when using a tool with a recursive type, and access a member of a Pydantic model.
Checklist
There are none here for context.
Additional context
None.