`parse(to=A | None)` raises "Unsupported type" below Python 3.14 while `Union[A, None]` works
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
- Run the snippet below on Python 3.12. It answers the request from a mock transport, so it needs no key and no network.
Union[Model, None]andOptional[Model]returnModel(id='m1').Model | Noneanddict | listraise:
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
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.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openai/openai-python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
openai/openai-python#4338 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`DeprecationWarning` thrown when calling websockets (17.1 and later)Có thể đã có người làm @akhil9tiet đã nhận 1 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
openai/openai-python#4337 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersCó thể đã có người làm @Rewaskc02-lang đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4311 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4310 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
openai/openai-python#4309 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openai/openai-python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 69/100
-
frontend testing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
vendrabuck/HERD#1588 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Lessons: record the second case of L14 (security updates cover only pip)Có thể đã có người làm @vishwanathj đã nhận hôm nay. Đang mởarea/docs no-test-needed type/docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
vishwanathj/ocp_lab.ai#39 · 1 bình luận ·
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
laaielichter/Gulliver#228 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày