Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Improper handling of pydantic `extra="allow"`

未关闭
#2,740 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@yashwantbezawada 已经在做这个了。

开始于 2025年11月9日。

  • #2741 来自 @yashwantbezawada —— 已关闭,未合并
  • #2743 来自 @yashwantbezawada —— 未关闭
  • #3805 来自 @Bhumika-1432006 —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
api

调研方向

从 src/openai/lib/_pydantic.py 和链接行中的 to_strict_json_schema 方法开始。运行报告中的 MyClass 示例,或调用 to_strict_json_schema(MyClass) 来检查生成的 schema。当使用 extra="allow" 的顶层 Pydantic 模型生成 additionalProperties: false,并且不再触发报告中的 response_format 错误时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Python
星标
31.8k
派生
7.3k
平均合并
1 天 3 小时
30 天内合并 PR
131

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openai/openai-python 的其他 Issue

查看 openai/openai-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。