From 6e1f7357f2603c665950952b0ba575ba15380aeb Mon Sep 17 00:00:00 2001 From: Ishaan Samantray Date: Sun, 31 May 2026 23:20:44 -0400 Subject: [PATCH] fix(_models): guard against IndexError in construct_type for bare list get_args(list) returns () when no type parameter is present, causing args[0] to raise IndexError. Guard with `args[0] if args else object` so bare list falls through to identity behavior. Fixes #1626 --- src/anthropic/_models.py | 2 +- tests/test_models.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index dc00516bc..98ea7239c 100644 --- a/src/anthropic/_models.py +++ b/src/anthropic/_models.py @@ -668,7 +668,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] or bare list return [construct_type(value=entry, type_=inner_type) for entry in value] if origin == float: diff --git a/tests/test_models.py b/tests/test_models.py index 195f23079..94cb47a2b 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_list() -> None: + # Bare list (no type parameter) must not raise IndexError + result = construct_type(value=["a", "b", "c"], type_=list) + assert result == ["a", "b", "c"]