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
14 changes: 10 additions & 4 deletions src/openai/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ def _transform_recursive(
return _transform_typeddict(data, stripped_type)

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

if (
# List[T]
Expand Down Expand Up @@ -346,8 +349,11 @@ 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]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
args = get_args(stripped_type)
if len(args) >= 2:
items_type = args[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
return data

if (
# List[T]
Expand Down
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 TypedDictBareDict(TypedDict):
metadata: dict # bare dict, no type parameters


@parametrize
@pytest.mark.asyncio
async def test_bare_dict_no_indexerror(use_async: bool) -> None:
# bare `dict` annotation (no type params) must not raise IndexError
assert await transform({"metadata": {"key": "value"}}, TypedDictBareDict, use_async) == {
"metadata": {"key": "value"}
}