`parse(to=A | None)` raises "Unsupported type" below Python 3.14 while `Union[A, None]` works
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- backend-api-design
Direzione di ricerca
Leggi _response.py e _legacy_response.py nei metodi _parse, poi verifica come _utils.is_union() gestisce types.UnionType. Riproduci il malfunzionamento con Python 3.12 usando lo snippet del trasporto mock nell’issue e confronta il comportamento di parsing della risposta grezza per A | None e Union[A, None]. Il lavoro è completato quando entrambi vengono analizzati correttamente nelle versioni precedenti a Python 3.14; esegui i test pertinenti di parsing delle risposte.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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)
- Lingua principale
- Python
- Stelle
- 32.2k
- Fork
- 5.8k
- Merge medio
- 12h 31m
- PR unite (30g)
- 101
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
openai/openai-python#4338 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`DeprecationWarning` thrown when calling websockets (17.1 and later)Forse già presa @akhil9tiet l’ha presa 1 giorno fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
openai/openai-python#4337 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersForse già presa @Rewaskc02-lang l’ha presa 2 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4311 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4310 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
openai/openai-python#4309 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 69/100
-
frontend testing
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
vendrabuck/HERD#1588 ·
I maintainer di solito rispondono entro 1 giorno
-
Lessons: record the second case of L14 (security updates cover only pip)Forse già presa @vishwanathj l’ha presa oggi. Apertaarea/docs no-test-needed type/docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
vishwanathj/ocp_lab.ai#39 · 1 commento ·
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
laaielichter/Gulliver#228 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno