Skip to content

fix(_models): guard against IndexError in construct_type for bare list#1627

Open
devteamaegis wants to merge 1 commit into
anthropics:mainfrom
devteamaegis:fix/indexerror
Open

fix(_models): guard against IndexError in construct_type for bare list#1627
devteamaegis wants to merge 1 commit into
anthropics:mainfrom
devteamaegis:fix/indexerror

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

Calling construct_type(value=[...], type_=list) with a bare, unparameterized list raises IndexError: tuple index out of range. The origin == list branch at line 671 of src/anthropic/_models.py unconditionally executes inner_type = args[0], but get_args(list) returns an empty tuple when no type parameter is present.

Why it happens

get_origin(list) returns list (matching the branch), but get_args(list) returns () rather than a one-element tuple, so args[0] is always out of range for unparameterized bare list.

Fix

Changed inner_type = args[0] to inner_type = args[0] if args else object. When no type parameter is present, elements are returned unchanged (identity via construct_type(value=entry, type_=object)), which is the correct safe fallback.

Test

Added test_construct_type_bare_list in tests/test_models.py confirming that construct_type(value=["a", "b", "c"], type_=list) returns ["a", "b", "c"] without raising.

Fixes #1626

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 anthropics#1626
@devteamaegis devteamaegis requested a review from a team as a code owner June 1, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: construct_type() raises IndexError when called with bare unparameterized list

1 participant