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
5 changes: 4 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,10 @@ 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[1]
return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}
Comment on lines +660 to 664

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


@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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a Pydantic-v1-compatible model constructor

The repository supports Pydantic 1.x, and the test-pydantic-v1 nox session runs this test without excluding it, but Pydantic v1's BaseModel has no model_validate method and the SDK does not provide that shim. This therefore raises AttributeError for both parametrized cases in the compatibility suite; construct the models through a version-compatible helper such as parse_obj instead.

Useful? React with 👍 / 👎.

{
"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"
4 changes: 4 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,10 @@ def __getattr__(self, attr: str) -> Item: ...
assert model.other == "foo"


def test_bare_dict_annotation() -> None:
assert construct_type(value={"key": "value"}, type_=dict) == {"key": "value"}


# NOTE: Workaround for Pydantic Iterable behavior.
# Iterable fields are replaced with a ValidatorIterator and may be consumed
# during serialization, which can cause subsequent dumps to return empty data.
Expand Down