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
3 changes: 2 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ 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]
_type_args = get_args(type_)
items_type = _type_args[1] if len(_type_args) >= 2 else Any # bare dict has no type args
return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}

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

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
_type_args = get_args(stripped_type)
items_type = _type_args[1] if len(_type_args) >= 2 else Any # bare dict has no type args
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down Expand Up @@ -346,7 +347,8 @@ 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]
_type_args = get_args(stripped_type)
items_type = _type_args[1] if len(_type_args) >= 2 else Any # bare dict has no type args
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down
7 changes: 7 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,10 @@ class Model(BaseModel):
# falls back to list of chars rather than calling str(["h", "e", "l", "l", "o"])
assert m.data["items"] == ["h", "e", "l", "l", "o"]
assert m.model_dump()["data"]["items"] == ["h", "e", "l", "l", "o"]


def test_construct_type_bare_dict() -> None:
# Regression: bare `dict` annotation (no type args) crashed with ValueError
# because get_args(dict) returns () and the old code did `_, v = get_args(dict)`.
result = construct_type(value={"key": "value"}, type_=dict)
assert result == {"key": "value"}
13 changes: 13 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,16 @@ async def test_strips_notgiven(use_async: bool) -> None:
async def test_strips_omit(use_async: bool) -> None:
assert await transform({"foo_bar": "bar"}, Foo1, use_async) == {"fooBar": "bar"}
assert await transform({"foo_bar": omit}, Foo1, use_async) == {}


class BareDict(TypedDict, total=False):
metadata: dict # bare dict, no type parameters


@parametrize
@pytest.mark.asyncio
async def test_bare_dict_annotation(use_async: bool) -> None:
# Regression: bare `dict` TypedDict field crashed with IndexError because
# get_args(dict) returns () and the old code did get_args(dict)[1].
result = await transform({"metadata": {"key": "value"}}, BareDict, use_async)
assert result == {"metadata": {"key": "value"}}