diff --git a/src/groq/_utils/_transform.py b/src/groq/_utils/_transform.py index 5207549..f70b4b6 100644 --- a/src/groq/_utils/_transform.py +++ b/src/groq/_utils/_transform.py @@ -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 not args: + return data + items_type = args[1] return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} if ( @@ -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 not args: + return data + items_type = args[1] return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} if ( diff --git a/tests/test_transform.py b/tests/test_transform.py index affc3ee..8807fa0 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -453,6 +453,15 @@ async def test_strips_notgiven(use_async: bool) -> None: assert await transform({"foo_bar": not_given}, Foo1, use_async) == {} +@parametrize +@pytest.mark.asyncio +async def test_bare_dict_annotation_no_indexerror(use_async: bool) -> None: + # A bare `dict` annotation (no type args) previously raised IndexError because + # get_args(dict) returns () and the code unconditionally indexed [1]. + result = await transform({"key": {"a": 1}}, Annotated[dict, PropertyInfo(alias="key")], use_async) + assert result == {"key": {"a": 1}} + + @parametrize @pytest.mark.asyncio async def test_strips_omit(use_async: bool) -> None: