Python: fix build_model_schema putting pydantic constraint objects into field descriptions - #14448
Open
Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
Open
Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
Conversation
… descriptions FieldInfo.metadata is a list of pydantic constraint objects (Ge, Le, ...); the builder used metadata[0] as the field description, making the schema not JSON-serializable and dropping the real description. Use field_info.description instead, and add a regression test.
Sanjay Santhanam (Sanjays2402)
requested a review
from a team
as a code owner
September 13, 2026 23:28
Sanjay Santhanam (Sanjays2402)
had a problem deploying
to
github-app-auth
September 13, 2026 23:28 — with
GitHub Actions
Failure
Sanjay Santhanam (Sanjays2402)
had a problem deploying
to
github-app-auth
September 13, 2026 23:28 — with
GitHub Actions
Failure
Copilot started reviewing on behalf of
Sanjay Santhanam (Sanjays2402)
September 13, 2026 23:29
View session
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Restore string metadata fallback for Annotated field descriptions while excluding constraint objects.
Pull request overview
Fixes Pydantic constraint objects being emitted as non-serializable field descriptions.
Changes:
- Uses
field_info.descriptionfor Pydantic fields. - Adds regression coverage for constrained fields and JSON serialization.
File summaries
| File | Changes |
|---|---|
python/tests/unit/schema/test_schema_builder.py |
Adds regression test coverage. |
python/semantic_kernel/schema/kernel_json_schema_builder.py |
Updates field-description extraction. |
Review details
Suppressed comments (1)
python/semantic_kernel/schema/kernel_json_schema_builder.py:98
- This removes the list-metadata fallback wholesale, but list metadata is also how Pydantic v2 stores arbitrary
Annotatedmetadata. The existingItemsmodel in this file usesAnnotated[str, "Description of the item"]; itsFieldInfo.descriptionisNone, so this change silently drops that nested field description. Keep a fallback only for string metadata, and only when.descriptionis absent/None, soGe/Leobjects remain excluded whileAnnotateddescriptions continue to work.
elif hasattr(field_info, "description"):
# Pydantic v2 keeps constraints (Ge, Le, ...) in field_info.metadata,
# which is a list of constraint objects and not a description,
# so the description must come from field_info.description instead.
field_description = field_info.description
- 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.
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.
The bug
KernelJsonSchemaBuilder.build_model_schema()pulled the field description fromFieldInfo.metadata[0]when metadata was a non-empty list. In pydantic v2,metadatais the list of constraint objects (Ge,Le, ...), so a field likegot
description=Ge(ge=0.0)in the generated schema. The schema then failedjson.dumps(...)withTypeError: Object of type Ge is not JSON serializable, and the real description was silently dropped.Reproduced on current
main:The fix
python/semantic_kernel/schema/kernel_json_schema_builder.py: usefield_info.description(the documented pydantic attribute) instead offield_info.metadata[0]. The dict-metadata fallback is kept for non-pydantic field-info objects.Tests
test_build_model_schema_with_field_constraints_keeps_descriptiontopython/tests/unit/schema/test_schema_builder.py— asserts the real description survives and the schema is JSON-serializable. Verified it fails on the unfixed code and passes with the fix.ruff checkandruff format --checkare clean on both touched files.Fixes #14443.