Improper handling of pydantic `extra="allow"`
Los mantenedores suelen responder en 1 día
@yashwantbezawada ya está trabajando en esto.
Desde el 9/11/2025.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 55/100
Línea de trabajo
Empieza por src/openai/lib/_pydantic.py y el método to_strict_json_schema en la línea enlazada. Ejecuta el ejemplo MyClass reportado o llama a to_strict_json_schema(MyClass) para inspeccionar el esquema generado. Está hecho cuando un modelo Pydantic de nivel superior que usa extra="allow" produce additionalProperties: false y deja de provocar el error reportado de response_format.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- Python
- Estrellas
- 31.8k
- Forks
- 7.3k
- Merge medio
- 1 d 3 h
- PR fusionados (30 d)
- 131
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openai/openai-python
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
openai/openai-python#4022 · 19 comentarios ·
Los mantenedores suelen responder en 1 día
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providersPosiblemente ocupada @mohmedmm la tomó hace 9 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
openai/openai-python#4017 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
Querystring drops explicit empty-string scalar valuesPosiblemente ocupada @sylvesterkaczmarek la tomó hace 31 días. Abiertosdk-breaking-change v4
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
openai/openai-python#3837 ·
Los mantenedores suelen responder en 1 día
-
Define + export `ServiceTiers` string literalPosiblemente ocupada @SparshGarg999 la tomó hace 57 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
openai/openai-python#3556 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
Empty OPENAI_BASE_URL prevents fallback to default API endpointPosiblemente ocupada @Sehastrajit-S la tomó hace 24 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
openai/openai-python#2927 · 6 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de openai/openai-python
Issues similares
-
Claiming namespace `jft63`Abiertonamespace operations
Dificultad 1/5 Menos de una hora Aptitud para principiantes 72/100
EclipseFdn/open-vsx.org#14043 ·
Los mantenedores suelen responder en 1 día
-
netbox status: needs triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
netbox-community/netbox#23376 ·
Los mantenedores suelen responder en 1 día
-
feedback simulation workshop
Dificultad 2/5 1-3 horas Aptitud para principiantes 73/100
githubnext/gh-aw-workshop#4455 ·
Los mantenedores suelen responder en 1 día
-
Triage 🩺
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
[BUG] Container scenario crashes without expected_recovery_time, kube DNS example uses retry_waitAbiertoneeds-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
krkn-chaos/krkn#1627 · 1 comentario ·
Los mantenedores suelen responder en 1 día