Response parsing returns str for dict/list when Content-Type is mixed-case or has whitespace before ';'
维护者通常 1 天内回复
评估
调研方向
这个 bug 是 _response.py 和 _legacy_response.py 中对 Content-Type 的 JSON 后缀检查区分大小写。先在模拟的 transport 上运行 issue 中的代码片段,看看哪些 Content-Type 变体会返回 str 而不是 dict。完成的标准是:Application/JSON 和 application/json ; charset=utf-8 在 sync 和 async 下都能解码为 dict 或 list,并且现有的 response 测试仍然通过。
由索引模型根据 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
Parsing JSON as dict/list depends on the casing and whitespace of the response Content-Type. At main 3dae6090cc31cc64a437fe0bcee740b6d81ed2cc, application/json returns a container, but Application/JSON and application/json ; charset=utf-8 return text with default validation or raise APIResponseValidationError with strict validation.
RFC 9110 §8.3.1 makes type/subtype case-insensitive; §5.6.6 permits whitespace before parameter semicolons. The raw response parse API explicitly supports dict/list. Both _response.py and _legacy_response.py have the case-sensitive suffix check.
This was reproduced locally through mocked transports and a loopback HTTP server serving a real JSON file, including a generated Responses raw wrapper. No hosted-provider incidence or security impact is claimed. Existing BaseModel fallback can already parse these forms, so the report concerns requested container parsing.
To Reproduce
Run the snippet with openai 3.51.0 (the current release) installed. Expect dict for all equivalent JSON types. The latter two instead produce str with default validation.
Code snippets
import httpx2
from openai import OpenAI
for content_type in (
"application/json",
"Application/JSON",
"application/json ; charset=utf-8",
):
def respond(request):
return httpx2.Response(
200, json={"zero": 0, "one": 1},
headers={"Content-Type": content_type},
)
with OpenAI(
api_key="abcdefghijklmnopqrstuvwxyz12345",
http_client=httpx2.Client(transport=httpx2.MockTransport(respond)),
) as client:
value = client.get("/probe", cast_to=dict)
print(content_type, type(value).__name__, value)
OS
Windows AMD64
Python version
Python 3.14.7; httpx2 2.16.0; Pydantic 2.14.3.
Library version
openai 3.51.0, main 3dae6090cc31cc64a437fe0bcee740b6d81ed2cc.
Root cause and local validation
Normalizing the split media token with strip().lower() before the existing endswith("json") check fixes both implementations. The reference patch preserves the existing permissive suffix rule rather than adding a MIME validator, leaves parameter values untouched, and adds sync/async ordinary/raw/streaming-response regressions.
Behavior changes: case/whitespace variants now decode JSON; strict parsing no longer rejects those types alone. Malformed JSON on newly recognized variants now raises the existing JSONDecodeError instead of returning text or reporting a Content-Type mismatch. Python whitespace stripping remains broader than a full MIME grammar check.
Fresh local checks on 2026-11-14: baseline regressions 192 failed/48 passed; after patch 374 focused tests passed. Changed-file Ruff lint/format and ty 0.0.95 passed. A 100-example generated run plus deterministic cases exercised 1,488 valid parses, 144 non-JSON controls and 24 malformed bodies without a new failure. The full diff applies cleanly to a second fresh clone at the same commit.
These used compatible pip-installed Windows wheels; locked all-extras bootstrap, full provider/Pydantic-v1/runtime matrix and patch CI were not run. Maintainer review and those adoption checks remain necessary.
Fresh open/closed issue/PR searches found no exact duplicate; the complete 49-open-PR file inventory had no overlap. Historical #854 addresses model fallback, and #3854 addresses boolean body whitespace.
Per CONTRIBUTING.md, this is an issue report; no non-collaborator PR is proposed. A two-line reference patch (one predicate in each file) with sync and async regressions is ready to share if useful.
Found and validated with AI assistance (GPT-6.1 via OpenAI Codex); the snippet above was run again against the published openai 3.51.0 before filing.
- 主要语言
- Python
- 星标
- 32.2k
- 派生
- 5.8k
- 平均合并
- 12 小时 29 分钟
- 30 天内合并 PR
- 112
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
openai/openai-python#4338 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
openai/openai-python#4337 ·
维护者通常 1 天内回复
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters可能已有人在做 @Rewaskc02-lang 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-python#4311 · 4 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-python#4310 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
openai/openai-python#4309 · 1 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
triage
难度 1/5 1-3 小时 新手友好度 88/100
kornia/kornia-rs#1416 · 1 条评论 ·
维护者通常 2 天内回复
-
ui
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
PolicyEngine/ai-inequality#64 ·
维护者通常 1 天内回复
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over time可能已有人在做 @PabloRoque 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
pymc-devs/pymc-extras#839 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复