Repository navigation
Python: Keep Field constraints after an Annotated tool description - #8645
Closed
Saquib Jawed (saquibjawedbit) wants to merge 2 commits into
Closed
Saquib Jawed (saquibjawedbit) wants to merge 2 commits into
Saquib Jawed (saquibjawedbit) wants to merge 2 commits into
Conversation
_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
Saquib Jawed (saquibjawedbit)
temporarily deployed
to
github-app-auth
September 22, 2026 13:52 — with
GitHub Actions
Inactive
Saquib Jawed (saquibjawedbit)
temporarily deployed
to
github-app-auth
September 22, 2026 13:52 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
Review effort: Lite
Findings: 1
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_annotationto expand trailingAnnotatedmetadata items instead of wrapping them in a single tuple (so Pydantic can interpret them). - Add a unit test verifying
_parse_annotationpreserves 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.
Saquib Jawed (saquibjawedbit)
temporarily deployed
to
github-app-auth
September 22, 2026 13:57 — with
GitHub Actions
Inactive
…in the Annotated constraint test
Saquib Jawed (saquibjawedbit)
temporarily deployed
to
github-app-auth
September 22, 2026 17:00 — with
GitHub Actions
Inactive
Contributor
Author
|
@microsoft-github-policy-service agree |
Member
|
The issue is not a issue, closing this PR. |
This branch was previously deployed
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.


Motivation & Context
A tool parameter annotated as
Annotated[int, "How many items", Field(ge=1, le=10)]loses everything after the string description._parse_annotationwrapped the trailing metadata in a singletuple, 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 sameFieldworks when it is the only metadata, and the function's docstring already says the remaining annotations are returned as-is.Description & Review Guide
_parse_annotationunpacks the metadata after the description into the newAnnotatedinstead 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 parsedAnnotatedmetadata and the end-to-end@toolschema and invocation validation.Field(...)constraints and other Pydantic metadata placed after a string description now appear inparameters()and are enforced on invoke. Parameters with only a description are unchanged._parse_annotation. If the trailingFieldsets its owndescription, it now takes precedence over the leading string, following Pydantic's normal metadata merge order. Previously thatFieldwas dropped entirely.Related Issue
Fixes #8643
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.