`parse(to=A | None)` raises "Unsupported type" below Python 3.14 while `Union[A, None]` works

オープン 初心者向け
#4,339 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

_response.py と _legacy_response.py の _parse メソッドを読み、次に _utils.is_union() が types.UnionType をどのように扱うか確認してください。issue にある mock transport のスニペットを使って Python 3.12 で失敗を再現し、A | None と Union[A, None] の raw response の解析動作を比較してください。Python 3.14 より前のバージョンで両方とも正常に解析できれば完了です。関連する response parsing テストを実行してください。

索引モデルが 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

parse(to=...) on a raw response accepts Union[A, None] and Optional[A], but the same union written as A | None raises RuntimeError: Unsupported type on Python 3.12. On 3.14 both spellings work. I only ran 3.12 and 3.14; from the cause below, 3.10, 3.11 and 3.13 should behave like 3.12.

APIResponse._parse in _response.py and LegacyAPIResponse._parse in _legacy_response.py skip the "unsupported type" check with origin is Union. Below 3.14 the origin of A | None is types.UnionType, not typing.Union, so the check falls through to the error. The is_union() helper in _utils already handles both.

To Reproduce
  1. Run the snippet below on Python 3.12. It answers the request from a mock transport, so it needs no key and no network.
  2. Union[Model, None] and Optional[Model] return Model(id='m1').
  3. Model | None and dict | list raise:
RuntimeError: Unsupported type, expected __main__.Model | None to be a subclass of <class 'openai.BaseModel'>, <class 'dict'>, <class 'list'>, typing.Union, <class 'NoneType'>, <class 'str'> or <class 'httpx2.Response'>.

On Python 3.14.7 all four return a value.

Code snippets
from typing import Optional, Union

import httpx2
from openai import OpenAI
from pydantic import BaseModel

BODY = {"id": "m1", "object": "model", "created": 1, "owned_by": "x"}
client = OpenAI(
    api_key="test",
    base_url="http://localhost/v1",
    http_client=httpx2.Client(transport=httpx2.MockTransport(lambda request: httpx2.Response(200, json=BODY))),
)


class Model(BaseModel):
    id: str


for to in (Union[Model, None], Optional[Model], Model | None, dict | list):
    raw = client.models.with_raw_response.retrieve("m1")
    try:
        print(to, "->", repr(raw.parse(to=to)))
    except Exception as exc:
        print(to, "->", type(exc).__name__, exc)
OS

Windows 11

Python version

Python v3.12.14 (fails), v3.14.7 (works)

Library version

openai v3.60.0 (main at f2b2399)

主要言語
Python
スター
32.2k
フォーク
5.8k
平均マージ
12時間 31分
マージ済み PR(30日)
101

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-python のほかの issue

openai/openai-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。