Skip to content

Python: fix build_model_schema putting pydantic constraint objects into field descriptions - #14448

Open
Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
microsoft:mainfrom
Sanjays2402:fix/schema-description-json
Open

Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
microsoft:mainfrom
Sanjays2402:fix/schema-description-json

Conversation

@Sanjays2402

Copy link
Copy Markdown

The bug

KernelJsonSchemaBuilder.build_model_schema() pulled the field description from FieldInfo.metadata[0] when metadata was a non-empty list. In pydantic v2, metadata is the list of constraint objects (Ge, Le, ...), so a field like

top_p: float = Field(default=0.9, ge=0.0, le=1.0, description="nucleus sampling")

got description=Ge(ge=0.0) in the generated schema. The schema then failed json.dumps(...) with TypeError: Object of type Ge is not JSON serializable, and the real description was silently dropped.

Reproduced on current main:

from pydantic import BaseModel, Field
import json
from semantic_kernel.schema.kernel_json_schema_builder import KernelJsonSchemaBuilder

class Params(BaseModel):
    top_p: float = Field(default=0.9, ge=0.0, le=1.0, description="nucleus sampling")

json.dumps(KernelJsonSchemaBuilder.build_model_schema(Params)["properties"]["top_p"])
# TypeError: Object of type Ge is not JSON serializable

The fix

python/semantic_kernel/schema/kernel_json_schema_builder.py: use field_info.description (the documented pydantic attribute) instead of field_info.metadata[0]. The dict-metadata fallback is kept for non-pydantic field-info objects.

Tests

  • Added test_build_model_schema_with_field_constraints_keeps_description to python/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.
  • Ran the schema builder unit suite: 37 passed.
  • ruff check and ruff format --check are clean on both touched files.

Fixes #14443.

… 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.

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.

🔵 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.description for 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 Annotated metadata. The existing Items model in this file uses Annotated[str, "Description of the item"]; its FieldInfo.description is None, so this change silently drops that nested field description. Keep a fallback only for string metadata, and only when .description is absent/None, so Ge/Le objects remain excluded while Annotated descriptions 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: build_model_schema puts pydantic constraint objects into field descriptions (breaks tool payload JSON)

2 participants