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
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 cast(object, 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 cast(object, data)
items_type = args[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
Expand Down
16 changes: 16 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ class DictItems(TypedDict):
assert await transform({"foo": {"foo_baz": "bar"}}, Dict[str, DictItems], use_async) == {"foo": {"fooBaz": "bar"}}



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


@parametrize
@pytest.mark.asyncio
async def test_bare_dict_in_typeddict(use_async: bool) -> None:
"""transform should not crash on bare dict annotations in TypedDict."""
result = await transform({"metadata": {"key": "value"}}, BareDictTypedDict, use_async)
assert result == {"metadata": {"key": "value"}}

result = await transform({"metadata": {}}, BareDictTypedDict, use_async)
assert result == {"metadata": {}}


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

Expand Down