Repository navigation
Commit 2281d4d
authored
Python: add typing.Literal support to KernelJsonSchemaBuilder (#14514)
### 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
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄1 parent 2a3236d commit 2281d4d
2 files changed
Lines changed: 58 additions & 3 deletions
File tree
- python
- semantic_kernel/schema
- tests/unit/schema
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
199 | 207 | | |
200 | 208 | | |
201 | 209 | | |
202 | 210 | | |
203 | 211 | | |
204 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
205 | 216 | | |
206 | 217 | | |
207 | 218 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
455 | 455 | | |
456 | 456 | | |
457 | 457 | | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
0 commit comments