Invalid Schema Error when Using examples Field in Pydantic Model for OpenAI Python Client
维护者通常 1 天内回复
@LuminaX-alt 已经在做这个了。
开始于 2025年8月4日。
评估
调研方向
从 client.beta.chat.completions.parse 开始,检查 Pydantic 的 response_format schema 是如何发送的。使用提供的 MathReasoning model 重现该问题,并确定是 client 还是 API 拒绝了 examples 字段。完成的标准是记录一项关于是否需要修改 client 的决定。
由索引模型根据 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
When you add the examples field in the Field definition of a Pydantic model (as shown in the code below) while using the OpenAI Python client, it raises a BadRequestError with error code 400. The error message indicates that the examples field is not permitted in the schema for the response_format parameter.
This is the error message:
raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'MathReasoning': In context=('properties', 'final_answer'), 'examples' is not permitted.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
Question:
Is this behavior due to a limitation in the OpenAI Python client or API, or is it something that could potentially be patched in the future?
To Reproduce
from openai import Client
from pydantic import BaseModel, Field
from dotenv import load_dotenv
load_dotenv()
client = Client()
class Step(BaseModel):
explanation: str
output: str
class MathReasoning(BaseModel):
steps: list[Step]
final_answer: str = Field(
title="Final Answer",
description="The final answer to the math problem",
examples=["x = -3", "x = 2"]
)
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
{"role": "user", "content": "how can I solve 8x + 7 = -23"}
],
response_format=MathReasoning,
)
math_reasoning = completion.choices[0].message
# If the model refuses to respond, you will get a refusal message
if (math_reasoning.refusal):
print(math_reasoning.refusal)
else:
print(math_reasoning.parsed)
Code snippets
OS
Windows
Python version
Python v3.11.9
Library version
openai v1.68.2
- 主要语言
- Python
- 星标
- 31.8k
- 派生
- 7.3k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 137
环境准备
在浏览器里用你自己的 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 于 11 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#4017 · 3 条评论 ·
维护者通常 1 天内回复
-
Querystring drops explicit empty-string scalar values可能已有人在做 @sylvesterkaczmarek 于 33 天前认领。 未关闭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 于 26 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#2927 · 6 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
bug difficulty:2 good first issue help wanted
难度 2/5 1-3 小时 新手友好度 88/100
MakazhanAlpamys/Soup#1865 ·
维护者通常 1 天内回复
-
area: agents area: docs good first issue
难度 2/5 1-3 小时 新手友好度 88/100
openapi/gknowledge#23 ·
-
useless-suppression reported for deprecated-* messages that should be exempt可能已有人在做 @adityasinha-ghub 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
pylint-dev/pylint#11656 ·
维护者通常 1 天内回复
-
area:provisioning effort:S priority:P2 provider:local-libvirt risk:daytime-only status:in-progress type:bug
难度 2/5 1-3 小时 新手友好度 78/100
randomparity/kdive#3203 · 2 条评论 ·
维护者通常 1 天内回复
-
get() for StrEnum可能已有人在做 @FeJo5952 今天认领。 未关闭good first issue
难度 2/5 1-3 小时 新手友好度 72/100
collective/icalendar#1921 · 1 条评论 ·
维护者通常 1 天内回复