From d3a36be3254f3affa292a101ac530cdfdb4f5119 Mon Sep 17 00:00:00 2001 From: Aegis Dev Date: Sun, 31 May 2026 18:47:59 -0400 Subject: [PATCH] fix(_models): guard against bare dict in construct_type() get_args(dict) returns () for a plain unparameterised dict, causing the unconditional two-value unpack on line 650 to raise ValueError. Guard the unpack with `if args:` and fall back to `object` as the items_type when no type parameters are present. Fixes #1619 --- src/anthropic/_models.py | 3 ++- tests/test_models.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index dc00516bc..2acf3a892 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 args else object # Dict[_, items_type]; plain dict has no args return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} if ( diff --git a/tests/test_models.py b/tests/test_models.py index 195f23079..513302b8e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1014,4 +1014,9 @@ class Model(BaseModel): # falls back to list of chars rather than calling str(["h", "e", "l", "l", "o"]) assert m.data["items"] == ["h", "e", "l", "l", "o"] - assert m.model_dump()["data"]["items"] == ["h", "e", "l", "l", "o"] + + +def test_construct_type_bare_dict() -> None: + # plain `dict` with no type parameters should not raise ValueError + result = construct_type(value={"key": "value"}, type_=dict) + assert result == {"key": "value"}