Improper handling of pydantic `extra="allow"`
维护者通常 1 天内回复
@yashwantbezawada 已经在做这个了。
开始于 2025年11月9日。
评估
调研方向
从 src/openai/lib/_pydantic.py 和链接行中的 to_strict_json_schema 方法开始。运行报告中的 MyClass 示例,或调用 to_strict_json_schema(MyClass) 来检查生成的 schema。当使用 extra="allow" 的顶层 Pydantic 模型生成 additionalProperties: false,并且不再触发报告中的 response_format 错误时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Python
- 星标
- 31.8k
- 派生
- 7.3k
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 131
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
openai/openai-python#4022 · 19 条评论 ·
维护者通常 1 天内回复
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providers可能已有人在做 @mohmedmm 于 10 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#4017 · 3 条评论 ·
维护者通常 1 天内回复
-
Querystring drops explicit empty-string scalar values可能已有人在做 @sylvesterkaczmarek 于 32 天前认领。 未关闭sdk-breaking-change v4
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#3837 ·
维护者通常 1 天内回复
-
Define + export `ServiceTiers` string literal可能已有人在做 @SparshGarg999 于 59 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#3556 · 3 条评论 ·
维护者通常 1 天内回复
-
Empty OPENAI_BASE_URL prevents fallback to default API endpoint可能已有人在做 @Sehastrajit-S 于 25 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#2927 · 6 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 87/100
adri0/scadustats#128 ·
维护者通常 1 天内回复
-
`TIMESTAMP`, `TIMESTAMP_NTZ`, `TINYINT` and the other `TypeDecorator`s don't declare `python_type`未关闭
难度 2/5 1-3 小时 新手友好度 78/100
-
RPBC未关闭awaiting policy acceptance precheck-passed
难度 1/5 1 小时以内 新手友好度 78/100
Bioconductor/BiocContributions#240 · 1 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
documentation good first issue launch
难度 2/5 1-3 小时 新手友好度 83/100
FlologixAI/etchwork#4 ·