Skip to content

fix: preserve Annotated metadata in function tool declarations and arg conversion - #6879

Open
MarlzRana wants to merge 1 commit into
google:mainfrom
MarlzRana:fix/annotated-function-tool-params
Open

fix: preserve Annotated metadata in function tool declarations and arg conversion#6879
MarlzRana wants to merge 1 commit into
google:mainfrom
MarlzRana:fix/annotated-function-tool-params

Conversation

@MarlzRana

@MarlzRana MarlzRana commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
When the JSON_SCHEMA_FOR_FUNC_DECL feature is enabled, typing.Annotated metadata was silently dropped in two places:

  1. Declaration builder. _function_tool_declarations._get_function_fields called get_type_hints() without include_extras=True, so Annotated[T, Field(description=..., ge=1)] was unwrapped to a bare T before Pydantic's create_model saw it. Every description and constraint an author attached via Annotated was missing from the schema shown to the model. The legacy builder never had this problem, so this was a regression between the two paths.

  2. Argument conversion. FunctionTool._preprocess_args resolves parameter types with get_type_hints(), which strips Annotated for free. But when get_type_hints() cannot resolve a signature (an unresolvable forward reference anywhere in it e.g. a TYPE_CHECKING-only import or a recursive type alias), it raises and the code falls back to the raw param.annotation, which is still Annotated-wrapped. None of the conversion branches recognised the wrapper, so JSON dicts from the model reached the function body unconverted (which later led to AttributeError's as described in the original issue on "member access" of the model, which in fact was passed as a raw dict).

Solution:

  • Pass include_extras=True to get_type_hints() in the JSON-schema declaration builder (parameter and forward-ref return paths), so Annotated metadata reaches the generated schema which matches the legacy builder as well
  • Unwrap Annotated in _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:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added 16 tests across three existing files:

  • test_function_tool_declarations.py: Annotated metadata (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 preserve Annotated metadata.
  • test_function_tool_pydantic.py: _preprocess_args conversion on both the resolved and the get_type_hints-fallback paths, plus an end-to-end run_async case.

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.py now 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

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Any dependent changes have been merged and published in downstream modules.

There are none here for context.

Additional context

None.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants