Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "1.93.2"
".": "1.93.3"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.93.3 (2025-07-09)

Full Changelog: [v1.93.2...v1.93.3](https://github.com/openai/openai-python/compare/v1.93.2...v1.93.3)

### Bug Fixes

* **parsing:** correctly handle nested discriminated unions ([fc8a677](https://github.com/openai/openai-python/commit/fc8a67715d8f1b45d8639b8b6f9f6590fe358734))

## 1.93.2 (2025-07-08)

Full Changelog: [v1.93.1...v1.93.2](https://github.com/openai/openai-python/compare/v1.93.1...v1.93.2)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openai"
version = "1.93.2"
version = "1.93.3"
description = "The official Python library for the openai API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
11 changes: 7 additions & 4 deletions src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Any, Type, Tuple, Union, Generic, TypeVar, Callable, Optional, cast
from datetime import date, datetime
from typing_extensions import (
List,
Unpack,
Literal,
ClassVar,
Expand Down Expand Up @@ -391,7 +392,7 @@ def _construct_field(value: object, field: FieldInfo, key: str) -> object:
if type_ is None:
raise RuntimeError(f"Unexpected field type is None for {key}")

return construct_type(value=value, type_=type_)
return construct_type(value=value, type_=type_, metadata=getattr(field, "metadata", None))


def is_basemodel(type_: type) -> bool:
Expand Down Expand Up @@ -445,7 +446,7 @@ def construct_type_unchecked(*, value: object, type_: type[_T]) -> _T:
return cast(_T, construct_type(value=value, type_=type_))


def construct_type(*, value: object, type_: object) -> object:
def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]] = None) -> object:
"""Loose coercion to the expected type with construction of nested values.

If the given value does not match the expected type then it is returned as-is.
Expand All @@ -463,8 +464,10 @@ def construct_type(*, value: object, type_: object) -> object:
type_ = type_.__value__ # type: ignore[unreachable]

# unwrap `Annotated[T, ...]` -> `T`
if is_annotated_type(type_):
meta: tuple[Any, ...] = get_args(type_)[1:]
if metadata is not None:
meta: tuple[Any, ...] = tuple(metadata)
elif is_annotated_type(type_):
meta = get_args(type_)[1:]
type_ = extract_type_arg(type_, 0)
else:
meta = tuple()
Expand Down
2 changes: 1 addition & 1 deletion src/openai/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "openai"
__version__ = "1.93.2" # x-release-please-version
__version__ = "1.93.3" # x-release-please-version
45 changes: 45 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,48 @@ class ModelB(BaseModel):
)

assert isinstance(m, ModelB)


def test_nested_discriminated_union() -> None:
class InnerType1(BaseModel):
type: Literal["type_1"]

class InnerModel(BaseModel):
inner_value: str

class InnerType2(BaseModel):
type: Literal["type_2"]
some_inner_model: InnerModel

class Type1(BaseModel):
base_type: Literal["base_type_1"]
value: Annotated[
Union[
InnerType1,
InnerType2,
],
PropertyInfo(discriminator="type"),
]

class Type2(BaseModel):
base_type: Literal["base_type_2"]

T = Annotated[
Union[
Type1,
Type2,
],
PropertyInfo(discriminator="base_type"),
]

model = construct_type(
type_=T,
value={
"base_type": "base_type_1",
"value": {
"type": "type_2",
},
},
)
assert isinstance(model, Type1)
assert isinstance(model.value, InnerType2)