You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
parse(to=A | None) raises "Unsupported type" below Python 3.14 while Union[A, None] works #4339
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] and Optional[Model] return Model(id='m1').
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'>.
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.
Activity
Pablo-aps commentedon Nov 27, 2026
Confirmed the missing Python 3.13 case on 3.13.7 with openai 3.60.0. It also affects
await response.parse(...)throughAsyncOpenAI.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 JSONnull.Union[dict, list]with an object and with an array.The corresponding
Model | Noneanddict | listforms raisedRuntimeErrorin each case. I usedModelderived fromopenai.BaseModelto 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 existingis_union()helper, I would cover both_parseimplementations and retain theUnion[...]controls, so the change restores the alternate spelling without changing existing parsing behavior.