diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index dc00516b..ac1b212b 100644 --- a/src/anthropic/_models.py +++ b/src/anthropic/_models.py @@ -647,7 +647,8 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_mapping(value): return value - _, items_type = get_args(type_) # Dict[_, items_type] + args = get_args(type_) + items_type = args[1] if len(args) >= 2 else object # Dict[_, items_type] return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} if ( @@ -668,7 +669,7 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_list(value): return value - inner_type = args[0] # List[inner_type] + inner_type = args[0] if args else object # List[inner_type] return [construct_type(value=entry, type_=inner_type) for entry in value] if origin == float: diff --git a/src/anthropic/_utils/_transform.py b/src/anthropic/_utils/_transform.py index 1331da17..0229f3d5 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 object return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} if ( @@ -348,7 +349,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 object return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} if ( diff --git a/tests/test_models.py b/tests/test_models.py index 195f2307..d7e5850f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -655,6 +655,18 @@ class Model(BaseModel): assert m.value == "foo" +def test_bare_dict_annotation() -> None: + # a bare `dict` annotation (no type parameters) should not raise + m = construct_type(value={"key": "value", "n": 1}, type_=cast(Any, dict)) + assert m == {"key": "value", "n": 1} + + +def test_bare_list_annotation() -> None: + # a bare `list` annotation (no type parameters) should not raise + m = construct_type(value=["a", "b", 1], type_=cast(Any, list)) + assert m == ["a", "b", 1] + + def test_discriminated_unions_invalid_data() -> None: class A(BaseModel): type: Literal["a"] diff --git a/tests/test_transform.py b/tests/test_transform.py index 513b09ca..406d838d 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -61,6 +61,13 @@ class Baz2(TypedDict): my_baz: Annotated[str, PropertyInfo(alias="myBaz")] +@parametrize +@pytest.mark.asyncio +async def test_bare_dict_annotation(use_async: bool) -> None: + # a bare `dict` annotation (no type parameters) should pass values through unchanged + assert await transform({"key": "value", "n": 1}, cast(Any, dict), use_async) == {"key": "value", "n": 1} + + @parametrize @pytest.mark.asyncio async def test_recursive_typeddict(use_async: bool) -> None: