diff --git a/src/anthropic/_utils/_transform.py b/src/anthropic/_utils/_transform.py index 1e7e5ac80..cdf59c531 100644 --- a/src/anthropic/_utils/_transform.py +++ b/src/anthropic/_utils/_transform.py @@ -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] + args = get_args(stripped_type) + items_type = args[1] if len(args) >= 2 else Any return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} if ( @@ -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] + args = get_args(stripped_type) + items_type = args[1] if len(args) >= 2 else Any 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 4a874ff86..a1a2e8a03 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -458,3 +458,12 @@ 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) == {} + + +@parametrize +@pytest.mark.asyncio +async def test_transform_recursive_bare_dict_no_index_error(use_async: bool) -> None: + # A bare (unparameterized) `dict` annotation must not raise IndexError when + # get_args(dict) returns () and the code tries to index position [1]. + result = await transform({"key": "value"}, dict, use_async) + assert result == {"key": "value"}