Skip to content

Python: add typing.Literal support to KernelJsonSchemaBuilder - #14514

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
VANDRANKI:fix/schema-builder-literal
Oct 26, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
VANDRANKI:fix/schema-builder-literal

Conversation

@VANDRANKI

Copy link
Copy Markdown
Contributor

Motivation and Context

KernelJsonSchemaBuilder had no handling for typing.Literal. A parameter typed Literal["fast", "slow"] was advertised to the model as a bare {"type": "object"} with no enum, so the model was never told the allowed values. This also hit Pydantic fields, Optional[Literal[...]] and list[Literal[...]].

Fixes #14511

Description

In handle_complex_type, a Literal now builds {"type": ..., "enum": [...]}, the same shape build_enum_schema returns for Enum classes:

  • type is the JSON type of the values when they all share one, otherwise a list of the types (with "null" last). None maps to "null".
  • Enum members inside a Literal are converted to their .value.
  • Optional[Literal[...]] (and Literal[...] | None) now adds "null" to type and None to enum. The Optional branch also no longer nests a list inside type when the inner schema already has a list type.
  • list[Literal[...]] works through the existing list branch and puts the enum on items.

Tests added in tests/unit/schema/test_schema_builder.py cover str, int, bool, mixed, Optional, | None, list, Enum members, descriptions, and a Pydantic model with Literal fields. The new tests fail on current main and pass now.

tests/unit/schema, tests/unit/functions, tests/unit/connectors/utils and test_function_choice_behavior.py: all pass. ruff and mypy are clean on the changed files. The repro from the issue now gives {"type": "string", "enum": ["fast", "slow"], "description": "speed mode"} for mode and an enum on the nested level field.

Contribution Checklist

Copilot AI balanced review requested due to automatic review settings September 30, 2026 16:41

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 encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Merged via the queue into microsoft:main with commit 2281d4d Oct 26, 2026
36 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — f72cfb4e Deployed Sep 30, 2026 by VANDRANKI via team_check #565
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.

.Net: Python: KernelJsonSchemaBuilder emits {"type": "object"} with no enum for typing.Literal parameters and fields

3 participants