Repository navigation
Python: Require optional fields in strict structured output schemas - #14837
Open
Andrew Chen (chuenchen309) wants to merge 1 commit into
Open
Andrew Chen (chuenchen309) wants to merge 1 commit into
Andrew Chen (chuenchen309) wants to merge 1 commit into
Conversation
Non-Pydantic classes used as a structured-output response_format are sent with strict: true, which requires every property in "required". Optional fields were left out; they stay nullable through their type.
Andrew Chen (chuenchen309)
temporarily deployed
to
github-app-auth
November 20, 2026 11:59 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are blocking the structured-output schema update.
No open issues 🎉
What changed in this PR
Updates Python schema generation to meet strict structured output requirements: optional fields are listed as required while remaining nullable.
Changes:
- Includes every model field in
requiredwhenstructured_output=True. - Preserves existing behavior for non-structured schemas.
- Adds a regression test for required, nullable optional fields.
| File | Description |
|---|---|
python/tests/unit/schema/test_schema_builder.py |
Tests that optional fields are required and nullable in structured outputs. |
python/semantic_kernel/schema/kernel_json_schema_builder.py |
Requires all model fields when generating structured output schemas. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This branch was previously deployed
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.
Motivation and Context
When
response_formatis a plain Python class (the non-Pydantic case the structured outputs samples describe), the OpenAI handler, Azure AI Inference and the OpenAI assistant/responses agents build the schema withKernelJsonSchemaBuilder.build(..., structured_output=True)and send it withstrict: true. Strict mode requires every property to be listed inrequired, with optional values expressed as a nullable type. The builder leftOptionalfields out ofrequired, so a class likeproduces
"required": ["title"]. The same fields on a Pydantic model go through openai'stype_to_response_format_param, which gives"required": ["title", "note"].Description
In
build_model_schema, every field is added torequiredwhenstructured_outputis set. Optional fields already get["<type>", "null"]from theOptionalbranch, so they stay nullable. Tool/function parameter schemas never passstructured_output=True, so they are unchanged.Added a regression test in
test_schema_builder.pythat fails on main. The schema, functions, OpenAI, Azure AI Inference, NVIDIA and OpenAI agent unit tests pass.Contribution Checklist