Skip to content

Python: Fix build_model_schema using pydantic constraint as field description - #14641

Closed
Leon.C (zichen0116) wants to merge 1 commit into
microsoft:mainfrom
zichen0116:fix/json-schema-constraint-description
Closed

Leon.C (zichen0116) wants to merge 1 commit into
microsoft:mainfrom
zichen0116:fix/json-schema-constraint-description

Conversation

@zichen0116

Copy link
Copy Markdown

Motivation and Context

Closes #14443.

In pydantic v2, FieldInfo.metadata is a list of constraint/annotation objects (e.g. [Ge(ge=0.0), Le(le=1.0)]), not the field description. KernelJsonSchemaBuilder.build_model_schema() took metadata[0] verbatim as the description, which caused two problems:

  1. The real Field(..., description="...") was silently dropped from the generated tool schema.
  2. The schema dict ended up containing a Ge/Le object, so json.dumps(schema) raised TypeError: Object of type Ge is not JSON serializable — breaking function-calling payloads for OpenAI-compatible connectors.

Description

In python/semantic_kernel/schema/kernel_json_schema_builder.py:

  • The list-metadata branch now only treats a plain str entry as a description.
  • Otherwise it falls back to field_info.description (where pydantic v2 actually stores it).
  • The dict-metadata branch is unchanged.

Validation

  • Reproduced the issue on current main before the fix: description came out as Ge(ge=0.0) and json.dumps raised TypeError.
  • Added two regression tests in python/tests/unit/schema/test_schema_builder.py:
    • test_build_model_schema_with_constrained_field_with_description — constrained field with description="nucleus sampling" keeps the string description and the schema serializes.
    • test_build_model_schema_with_constrained_field_without_description — constrained field without a description yields no description key and the schema serializes.
  • Both new tests fail on the unfixed code and pass with the fix.
  • pytest tests/unit/schema/test_schema_builder.py: 38 passed (36 existing + 2 new).
  • ruff check and ruff format --check clean on both touched files.

@zichen0116
Leon.C (zichen0116) requested a review from a team as a code owner October 22, 2026 19:22
Copilot AI balanced review requested due to automatic review settings October 22, 2026 19:22

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@eavanvalkenburg

Copy link
Copy Markdown
Member

Thanks for the fix. This bug is already fixed on main by #14454. I verified that constrained fields work both with and without descriptions, without leaking constraint objects into the schema. Closing as superseded; no rebase or further changes are needed here.

This branch was previously deployed

1 inactive deployment
github-app-auth — f4c9410d Deployed Oct 22, 2026 by zichen0116 via team_check #659
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.

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

3 participants