diff --git a/src/openai/_utils/_transform.py b/src/openai/_utils/_transform.py index 414f38c340..6061e38c3e 100644 --- a/src/openai/_utils/_transform.py +++ b/src/openai/_utils/_transform.py @@ -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] @@ -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] diff --git a/tests/test_transform.py b/tests/test_transform.py index bece75dfc7..4f50799572 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -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"} + }