Improper handling of pydantic `extra="allow"`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.6k
- Forks
- 5.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
The response_format option requires "additionalProperties": false to be present in the json schema that is submitted. When using Pydantic models, setting the extra="allow" option causes the json schema return addtionalProperties=True. The to_strict_json_schema method attempts to fix this, but doesn't correctly handle top-level models with extra="allow". In particular, the bug is here, where it only sets additionalProperties=False when the key is not already present, rather than always as required by the API.
Per the support forum, the library should be handling this, but is not.
To Reproduce
I set up a venv with the latest versions of pydantic and openai:
python3.10 -m venv /tmp/openaivenv
/tmp/openaivenv/bin/pip install openai pydantic ipython
/tmp/openaivenv/bin/ipython
Then run a simple chat completion with a response format:
from openai import AsyncOpenAI
from pydantic import BaseModel, ConfigDict
client = AsyncOpenAI() # set OPENAI_API_KEY env variable
class MyClass(BaseModel):
model_config = ConfigDict(extra="allow")
field: str
response = await client.beta.chat.completions.parse(
model='gpt-4.1',
messages=[
{
"role": "user",
"content": "Yo i need some fake data for a test plz halp."
},
],
response_format=MyClass,
)
This outputs a 400 bad request:
BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'MyClass': In context=(), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
The cause is that the schema is not correctly updated:
In [2]: from openai.lib._pydantic import to_strict_json_schema
...:
In [3]: to_strict_json_schema(MyClass)
...:
Out[3]:
{'additionalProperties': True,
'properties': {'field': {'title': 'Field', 'type': 'string'}},
'required': ['field'],
'title': 'MyClass',
'type': 'object'}
The examples above are using the following versions:
# /tmp/openaivenv/bin/python --version
Python 3.10.12
# /tmp/openaivenv/bin/pip freeze | grep -P 'openai|pydantic'
openai==2.7.1
pydantic==2.12.4
pydantic_core==2.41.5
Code snippets
OS
ubuntu
Python version
python v3.1
Library version
2.7.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/openai/lib/_pydantic.py and the to_strict_json_schema method at the linked line. Run the reported MyClass example or call to_strict_json_schema(MyClass) to inspect the generated schema. Done means a top-level Pydantic model using extra="allow" produces additionalProperties: false and no longer triggers the reported response_format error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100