Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openai/lib/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def _ensure_strict_json_schema(
_ensure_strict_json_schema(definition_schema, path=(*path, "definitions", definition_name), root=root)

typ = json_schema.get("type")
if typ == "object" and "additionalProperties" not in json_schema:
if typ == "object":
# Always set additionalProperties to False for strict schema compliance.
# The OpenAI API requires additionalProperties=false for structured output,
# even if Pydantic models use extra="allow" which sets it to True.
json_schema["additionalProperties"] = False
Comment on lines 49 to 54

Choose a reason for hiding this comment

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

P1 Badge Preserve schemas for typed dictionaries when enforcing strictness

Overwriting additionalProperties unconditionally means any object schema that intentionally defines a value schema for dynamic keys (e.g. Pydantic fields of type Dict[str, T]) now loses that schema and becomes additionalProperties: False. These dict fields previously produced {"additionalProperties": {…}} so the model could accept arbitrary keys whose values match the inner schema; after this change the generated schema forbids all keys, so structured outputs containing mappings can never validate even though the underlying model allows them. Consider only forcing False when the value is a boolean and leaving structured additionalProperties schemas intact.

Useful? React with 👍 / 👎.


# object types
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,28 @@ def test_nested_inline_ref_expansion() -> None:
"additionalProperties": False,
}
)


def test_pydantic_extra_allow() -> None:
"""Test that models with extra='allow' correctly set additionalProperties to False.

Regression test for issue #2740.
The OpenAI API requires additionalProperties=false for structured output,
even when Pydantic models use extra="allow" which generates True by default.
"""
from pydantic import ConfigDict

class MyClassWithExtraAllow(BaseModel):
model_config = ConfigDict(extra="allow")
field: str = Field(description="A test field")

schema = to_strict_json_schema(MyClassWithExtraAllow)

# The schema must have additionalProperties set to False
assert schema.get("additionalProperties") == False, \
"additionalProperties must be False for API compliance, even with extra='allow'"

# Verify the rest of the schema is correct
assert schema["type"] == "object"
assert "field" in schema["properties"]
assert schema["required"] == ["field"]
Comment on lines +433 to +436
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's use inline snapshots here