Skip to content

Python: fix(python): preserve Pydantic field descriptions in schemas - #14454

Open
S.J. (cstolting-collab) wants to merge 3 commits into
microsoft:mainfrom
cstolting-collab:fix/pydantic-field-description-schema
Open

S.J. (cstolting-collab) wants to merge 3 commits into
microsoft:mainfrom
cstolting-collab:fix/pydantic-field-description-schema

Conversation

@cstolting-collab

@cstolting-collab S.J. (cstolting-collab) commented Sep 17, 2026 •

Copy link
Copy Markdown

Motivation and Context

Description

Contribution Checklist

@cstolting-collab

Copy link
Copy Markdown
Author

Summary

  • Prefer Pydantic FieldInfo.description when building model field schemas.
  • Preserve the existing metadata-dictionary fallback for compatibility.
  • Add a regression test for constrained Pydantic fields.

Fixes #14443

Validation

Before this change, a constrained Pydantic field placed Ge(ge=0.0) in the generated schema description, causing json.dumps() to raise TypeError.

Verified after the change:

  • uv run pytest tests/unit/schema/test_schema_builder.py — 37 passed
  • `uv run pre-commit run --files semantic_kernel/schema/kernel_json_schema_builder.py tests/unit/schema/tes

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.

🟢 Approval recommended

The change is small, targeted, and includes a regression test validating the intended behavior for constrained Pydantic fields.

Pull request overview

This PR updates the Python JSON schema builder to correctly preserve Pydantic field description values when the field also has constraints (e.g., ge/le), and adds a unit test to prevent regressions. This fits into the semantic_kernel schema generation path used to produce structured output schemas from Python/Pydantic models.

Changes:

  • Prefer field_info.description when deriving per-field schema descriptions from Pydantic model_fields.
  • Avoid incorrectly treating Pydantic constraint metadata as a field description.
  • Add a unit test covering a constrained Pydantic field with a description.
File summaries
File Description
python/semantic_kernel/schema/kernel_json_schema_builder.py Adjusts how field descriptions are extracted from Pydantic model_fields to preserve description even when constraints exist.
python/tests/unit/schema/test_schema_builder.py Adds coverage to ensure constrained Pydantic fields keep the intended description in generated schemas.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review finding (warning): Preserve Annotated metadata descriptions

This fixes constrained Field(..., description=...) values, but drops descriptions supplied through Annotated metadata. In Pydantic v2, Annotated[str, "Description"] leaves field_info.description as None and stores the string in field_info.metadata. Please retain a list/tuple metadata fallback for string entries or dictionaries containing description, while ignoring constraint objects.

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Sep 29, 2026
@github-actions github-actions Bot changed the title fix(python): preserve Pydantic field descriptions in schemas Python: fix(python): preserve Pydantic field descriptions in schemas Sep 29, 2026

This branch was successfully deployed

1 active deployment
github-app-auth — 7555df93 Deployed Sep 29, 2026 by cstolting-collab via add_label #29171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants