Skip to content

Conversation

@yashwantbezawada
Copy link

Description

Fixes #2740

The to_strict_json_schema function was not properly handling Pydantic models with extra="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": false for all schemas.

The existing code only set additionalProperties=false when the key didn't exist:

if typ == "object" and "additionalProperties" not in json_schema:
    json_schema["additionalProperties"] = False

This meant models with extra="allow" (which already have additionalProperties=true in their schema) were not being corrected.

Solution

Remove the "additionalProperties" not in json_schema check to always enforce additionalProperties=false for object types:

if typ == "object":
    json_schema["additionalProperties"] = False

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 additionalProperties set to false, regardless of the Pydantic model configuration.

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
@yashwantbezawada yashwantbezawada requested a review from a team as a code owner November 8, 2025 02:45
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines 49 to 51
typ = json_schema.get("type")
if typ == "object" and "additionalProperties" not in json_schema:
if typ == "object":
json_schema["additionalProperties"] = False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

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.

Improper handling of pydantic extra="allow"

2 participants