Skip to content

Improper handling of pydantic extra="allow" #2740

@lucaswiman

Description

@lucaswiman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions