Skip to content

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

Backlog

Description

@Yasser-Ameur

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)

Activity

  1. Pablo-aps commented on Nov 27, 2026

    @Pablo-aps

    Confirmed the missing Python 3.13 case on 3.13.7 with openai 3.60.0. It also affects await response.parse(...) through AsyncOpenAI.models.with_streaming_response, not just the legacy raw-response path.

    With a mock JSON response, these passed:

    • Optional[Model] with {"id": "m1"} and with JSON null.
    • Union[dict, list] with an object and with an array.

    The corresponding Model | None and dict | list forms raised RuntimeError in each case. I used Model derived from openai.BaseModel to keep the union issue separate from acceptance of third-party model classes. No API requests were made.

    The null-body case is worth keeping in the regression tests: an optional response needs to accept actual JSON null, not only a populated model. For a fix using the existing is_union() helper, I would cover both _parse implementations and retain the Union[...] controls, so the change restores the alternate spelling without changing existing parsing behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions