Improper handling of pydantic `extra="allow"`
I maintainer di solito rispondono entro 1 giorno
@yashwantbezawada ci sta già lavorando.
Dal 9/11/2025.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 55/100
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
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
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
openai/openai-python#4022 · 19 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providersForse già presa @mohmedmm l’ha presa 12 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
openai/openai-python#4017 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Querystring drops explicit empty-string scalar valuesForse già presa @sylvesterkaczmarek l’ha presa 34 giorni fa. Apertasdk-breaking-change v4
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
openai/openai-python#3837 ·
I maintainer di solito rispondono entro 1 giorno
-
Define + export `ServiceTiers` string literalForse già presa @SparshGarg999 l’ha presa 61 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openai/openai-python#3556 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Empty OPENAI_BASE_URL prevents fallback to default API endpointForse già presa @Sehastrajit-S l’ha presa 27 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openai/openai-python#2927 · 6 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
Agentic
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
Imbad0202/academic-research-skills#979 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
reflex-dev/reflex#7561 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
prometheus-community/ansible#906 · 2 commenti ·