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

Đang mở Phù hợp với người mới
#4,339 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Đọc _response.py và _legacy_response.py tại các phương thức _parse, sau đó kiểm tra cách _utils.is_union() xử lý types.UnionType. Tái hiện lỗi trên Python 3.12 bằng đoạn mock transport trong issue và so sánh hành vi phân tích raw response cho A | None và Union[A, None]. Hoàn thành khi cả hai đều được phân tích thành công trên các phiên bản trước Python 3.14; chạy các bài kiểm thử phân tích response liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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)

Ngôn ngữ chính
Python
Star
32.2k
Fork
5.8k
Merge trung bình
12 giờ 31 phút
Pull request đã merge (30 ngày)
101

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openai/openai-python

Tất cả issue của openai/openai-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.