Fix Pydantic extra='allow' handling in strict JSON schema#2741
Fix Pydantic extra='allow' handling in strict JSON schema#2741yashwantbezawada wants to merge 1 commit into
Conversation
The to_strict_json_schema function was not properly handling Pydantic models with extra='allow'. It only set additionalProperties to false if the key didn't exist, but Pydantic models with extra='allow' generate schemas with additionalProperties=true. This causes the OpenAI API to reject the schema since structured outputs require additionalProperties to always be false. Changed the condition to always set additionalProperties=false for all object types, regardless of whether the key already exists in the schema. Fixes openai#2740
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| typ = json_schema.get("type") | ||
| if typ == "object" and "additionalProperties" not in json_schema: | ||
| if typ == "object": | ||
| json_schema["additionalProperties"] = False |
There was a problem hiding this comment.
Force-disables schemas that intentionally allow dynamic properties
The new logic sets additionalProperties=False for every object schema, even when additionalProperties already contains a schema for dynamic keys (e.g. a field typed as dict[str, int] produces {"additionalProperties": {"type": "integer"}}). By overwriting that value with False we now forbid those dictionaries entirely and drop the nested schema information. The prior guard avoided this by only setting the flag when the key was absent, so this change regresses existing models that legitimately depend on additionalProperties being a schema rather than a boolean. Consider only forcing False when the value is missing or explicitly True for extra="allow" models, and preserve non‑boolean schemas.
Useful? React with 👍 / 👎.
Description
Fixes #2740
The
to_strict_json_schemafunction was not properly handling Pydantic models withextra="allow".Problem
When using Pydantic models with
model_config = ConfigDict(extra="allow"), the generated JSON schema includes"additionalProperties": true. The OpenAI structured outputs API requires"additionalProperties": falsefor all schemas.The existing code only set
additionalProperties=falsewhen the key didn't exist:This meant models with
extra="allow"(which already haveadditionalProperties=truein their schema) were not being corrected.Solution
Remove the
"additionalProperties" not in json_schemacheck to always enforceadditionalProperties=falsefor object types:This aligns with the OpenAI API requirement and fixes the bug reported in #2740.
Testing
The fix ensures all object types in JSON schemas will have
additionalPropertiesset tofalse, regardless of the Pydantic model configuration.