Commit cdebce9
committed
fix: Always set additionalProperties=false for strict schema compliance
Fixes #2740
The `to_strict_json_schema` function now always sets `additionalProperties`
to `false` for object types, as required by the OpenAI API for structured
output. Previously, it only set this value when the key was missing,
which caused issues with Pydantic models using `extra="allow"`.
When Pydantic models use `ConfigDict(extra="allow")`, the generated schema
includes `"additionalProperties": true`. However, the OpenAI API requires
`"additionalProperties": false` for structured output to work correctly.
Changes:
- Modified `_ensure_strict_json_schema()` to unconditionally set
`additionalProperties = false` for all object types
- Added comprehensive test case `test_pydantic_extra_allow()` to verify
the fix handles models with `extra="allow"` correctly
This ensures API compliance while maintaining backward compatibility with
existing code.1 parent 6574bcd commit cdebce9
2 files changed
+29
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
0 commit comments