Response parsing returns str for dict/list when Content-Type is mixed-case or has whitespace before ';'
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
バグは、_response.py と _legacy_response.py にある Content-Type の JSON サフィックス判定が大文字小文字を区別していることです。まず、issue のスニペットをモックしたトランスポートに対して実行し、どの Content-Type のバリエーションで dict ではなく str が返るかを確認してください。Application/JSON と application/json ; charset=utf-8 が sync と async の両方で dict または list にデコードされ、既存のレスポンステストがすべて通れば完了です。
索引モデルが 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分
- マージ済み PR(30日)
- 112
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 日以内に返信