Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,11 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]
if not is_mapping(value):
return value

_, items_type = get_args(type_) # Dict[_, items_type]
args = get_args(type_)
if len(args) < 2:
return value

_, items_type = args # Dict[_, items_type]
return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}

if (
Expand Down
10 changes: 8 additions & 2 deletions src/openai/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ def _transform_recursive(
return _transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
args = get_args(stripped_type)
if len(args) < 2:
return data
items_type = args[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down Expand Up @@ -346,7 +349,10 @@ async def _async_transform_recursive(
return await _async_transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
args = get_args(stripped_type)
if len(args) < 2:
return data
items_type = args[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
Comment on lines 351 to 356

if (
Expand Down
3 changes: 3 additions & 0 deletions src/openai/types/beta/realtime/realtime_response_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Error(BaseModel):
code: Optional[str] = None
"""Error code, if any."""

message: Optional[str] = None
"""A human-readable description of the error, if any."""

type: Optional[str] = None
"""The type of error."""

Expand Down
3 changes: 3 additions & 0 deletions src/openai/types/realtime/realtime_response_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Error(BaseModel):
code: Optional[str] = None
"""Error code, if any."""

message: Optional[str] = None
"""A human-readable description of the error, if any."""

type: Optional[str] = None
"""The type of error."""

Expand Down
31 changes: 31 additions & 0 deletions tests/lib/test_realtime_response_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations

from pydantic import BaseModel
import pytest

from openai.types.beta.realtime import RealtimeResponseStatus as BetaRealtimeResponseStatus
from openai.types.realtime import RealtimeResponseStatus as RealtimeResponseStatus
Comment on lines +6 to +7


@pytest.mark.parametrize(
"status_cls",
[BetaRealtimeResponseStatus, RealtimeResponseStatus],
ids=["beta", "realtime"],
)
def test_realtime_response_status_error_message(status_cls: type[BaseModel]) -> None:
status = status_cls.model_validate(
{
"error": {
"code": "bad_request",
"message": "The model could not process the request.",
"type": "invalid_request_error",
},
"type": "failed",
}
)

assert status.error is not None
assert status.error.code == "bad_request"
assert status.error.message == "The model could not process the request."
assert status.error.type == "invalid_request_error"
assert status.type == "failed"
10 changes: 10 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ class NestedModel(BaseModel):
assert cast(Any, m.nested) is False


def test_bare_dictionary_passthrough() -> None:
bare = construct_type(value={"hello": {"foo": "bar"}}, type_=dict)
assert bare == {"hello": {"foo": "bar"}}

typed = construct_type(value={"hello": {"foo": "bar"}}, type_=Dict[str, BasicModel])
assert isinstance(typed, dict)
assert isinstance(typed["hello"], BasicModel)
assert typed["hello"].foo == "bar"


def test_nested_dictionary_model() -> None:
class NestedModel(BaseModel):
nested: Dict[str, BasicModel]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ class DictItems(TypedDict):
assert await transform({"foo": {"foo_baz": "bar"}}, Dict[str, DictItems], use_async) == {"foo": {"fooBaz": "bar"}}


class BareDictItems(TypedDict):
metadata: dict # pyright: ignore[reportMissingTypeArgument]


@parametrize
@pytest.mark.asyncio
async def test_bare_dict_annotation(use_async: bool) -> None:
assert await transform({"metadata": {"key": "value"}}, BareDictItems, use_async) == {
"metadata": {"key": "value"}
}


class TypedDictIterableUnionStr(TypedDict):
foo: Annotated[Union[str, Iterable[Baz8]], PropertyInfo(alias="FOO")]

Expand Down