Skip to content

Python: Require optional fields in strict structured output schemas - #14837

Open
Andrew Chen (chuenchen309) wants to merge 1 commit into
microsoft:mainfrom
chuenchen309:fix/strict-schema-required-optional
Open

Andrew Chen (chuenchen309) wants to merge 1 commit into
microsoft:mainfrom
chuenchen309:fix/strict-schema-required-optional

Conversation

@chuenchen309

Copy link
Copy Markdown

Motivation and Context

When response_format is 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 with KernelJsonSchemaBuilder.build(..., structured_output=True) and send it with strict: true. Strict mode requires every property to be listed in required, with optional values expressed as a nullable type. The builder left Optional fields out of required, so a class like

@dataclass
class Answer:
    title: str
    note: str | None = None

produces "required": ["title"]. The same fields on a Pydantic model go through openai's type_to_response_format_param, which gives "required": ["title", "note"].

Description

In build_model_schema, every field is added to required when structured_output is set. Optional fields already get ["<type>", "null"] from the Optional branch, so they stay nullable. Tool/function parameter schemas never pass structured_output=True, so they are unchanged.

Added a regression test in test_schema_builder.py that fails on main. The schema, functions, OpenAI, Azure AI Inference, NVIDIA and OpenAI agent unit tests pass.

Contribution Checklist

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.

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.

🟢 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 required when structured_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

@chuenchen309

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch was previously deployed

1 inactive deployment
github-app-auth — dac7f261 Deployed Nov 20, 2026 by chuenchen309 via add_label #29764
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.

2 participants