Repository navigation
Python: fix build_model_schema putting pydantic constraint objects into field descriptions - #14448
Sanjay Santhanam (Sanjays2402) wants to merge 2 commits 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.
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.
Eduard van Valkenburg (eavanvalkenburg)
left a comment
There was a problem hiding this comment.
Review finding (warning): Keep the string metadata fallback for Annotated fields
Removing the list-metadata branch loses descriptions from fields such as Annotated[str, "Description of the item"]. Pydantic v2 stores those strings in field_info.metadata while field_info.description remains None. Please scan list/tuple metadata for strings or dictionaries containing description, while continuing to ignore constraint objects.
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.