Skip to content

Python: Keep Field constraints after an Annotated tool description - #8645

Closed
Saquib Jawed (saquibjawedbit) wants to merge 2 commits into
microsoft:mainfrom
saquibjawedbit:fix-annotated-field-metadata
Closed

Saquib Jawed (saquibjawedbit) wants to merge 2 commits into
microsoft:mainfrom
saquibjawedbit:fix-annotated-field-metadata

Conversation

@saquibjawedbit

Copy link
Copy Markdown
Contributor

Motivation & Context

A tool parameter annotated as Annotated[int, "How many items", Field(ge=1, le=10)] loses everything after the string description. _parse_annotation wrapped the trailing metadata in a single tuple, which Pydantic treats as one opaque metadata object and ignores. The constraints were missing from the JSON schema sent to the model, and out-of-range arguments were accepted when the tool ran. The same Field works when it is the only metadata, and the function's docstring already says the remaining annotations are returned as-is.

Description & Review Guide

  • What are the major changes? _parse_annotation unpacks the metadata after the description into the new Annotated instead of wrapping it in a tuple. It uses the tuple-subscript form so it stays valid on Python 3.10. Two regression tests cover the parsed Annotated metadata and the end-to-end @tool schema and invocation validation.
  • What is the impact of these changes? Field(...) constraints and other Pydantic metadata placed after a string description now appear in parameters() and are enforced on invoke. Parameters with only a description are unchanged.
  • What do you want reviewers to focus on? The one-line change in _parse_annotation. If the trailing Field sets its own description, it now takes precedence over the leading string, following Pydantic's normal metadata merge order. Previously that Field was dropped entirely.

Related Issue

Fixes #8643

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

_parse_annotation wrapped the metadata that follows a string description
in a single tuple, so Pydantic ignored it. Constraints such as
Field(ge=1, le=10) were missing from the tool schema and not enforced
when the tool was invoked. Unpack the remaining metadata instead.

Fixes microsoft#8643

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Fixes loss of Pydantic Field constraints when a tool parameter uses Annotated[...] with a leading string description by preserving metadata items after the description.

Changes:

  • Update _parse_annotation to expand trailing Annotated metadata items instead of wrapping them in a single tuple (so Pydantic can interpret them).
  • Add a unit test verifying _parse_annotation preserves extra metadata after a description string.
  • Add an end-to-end tool test ensuring JSON schema includes min/max constraints and invoke enforces validation.
File Description
python/​packages/​core/​agent_framework/​_tools.py Fixes Annotated reconstruction so metadata after a string description remains individually visible to Pydantic.
python/​packages/​core/​tests/​core/​test_tools.py Adds regression tests for preserved metadata and for schema/validation behavior via @tool.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 22, 2026
@saquibjawedbit

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@eavanvalkenburg

Copy link
Copy Markdown
Member

The issue is not a issue, closing this PR.

This branch was previously deployed

1 inactive deployment
github-app-auth — 6b2a4b39 Deployed Sep 22, 2026 by saquibjawedbit via add_label #23572
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Field constraints after a string description in an Annotated tool parameter are silently dropped

3 participants