Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Improper handling of pydantic `extra="allow"`

Aperta
#2,740 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@yashwantbezawada ci sta già lavorando.

Dal 9/11/2025.

  • #2741 di @yashwantbezawada — chiusa senza merge
  • #2743 di @yashwantbezawada — aperta
  • #3805 di @Bhumika-1432006 — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
api

Direzione di ricerca

Inizia da src/openai/lib/_pydantic.py e dal metodo to_strict_json_schema alla riga collegata. Esegui l’esempio MyClass segnalato oppure chiama to_strict_json_schema(MyClass) per esaminare lo schema generato. Il lavoro è completato quando un modello Pydantic di primo livello che usa extra="allow" produce additionalProperties: false e non attiva più l’errore response_format segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
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

Lingua principale
Python
Stelle
31.8k
Fork
8k
Merge medio
1g 1h
PR unite (30g)
137

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openai/openai-python

Tutte le issue di openai/openai-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.