Skip to content

fix: guard against bare unparameterized dict/list in construct_type and transform#1694

Open
lavkeshdwivedi wants to merge 1 commit into
anthropics:mainfrom
lavkeshdwivedi:fix/bare-generic-type-crashes
Open

fix: guard against bare unparameterized dict/list in construct_type and transform#1694
lavkeshdwivedi wants to merge 1 commit into
anthropics:mainfrom
lavkeshdwivedi:fix/bare-generic-type-crashes

Conversation

@lavkeshdwivedi

Copy link
Copy Markdown

Summary

Three code paths crash when a model field or transform annotation uses a bare, unparameterized dict or list type (i.e. dict instead of Dict[str, T], or list instead of List[T]). This can happen when integrating with third-party schemas or when users annotate fields generically without type parameters.

get_args(dict) and get_args(list) return empty tuples () for bare types. The affected code assumed at least one or two elements were always present:

File Line Crash Type
src/anthropic/_models.py 650 _, items_type = get_args(type_) ValueError: not enough values to unpack
src/anthropic/_models.py 671 inner_type = args[0] IndexError: tuple index out of range
src/anthropic/_utils/_transform.py 183, 351 get_args(stripped_type)[1] IndexError: tuple index out of range

Fix

Guard each site with a length check and fall back to object as the element/value type when no type arguments are present. This matches the behaviour a caller would expect from an unparameterized collection.

Tests

  • test_construct_type_bare_dict_does_not_crash — previously raised ValueError
  • test_construct_type_bare_list_does_not_crash — previously raised IndexError
  • test_bare_dict_annotation_does_not_crash (sync + async) — previously raised IndexError

Closes

Fixes #1619, #1626, #1628

…nd transform

get_args(dict) and get_args(list) return empty tuples when the type has no
type parameters. Three code paths assumed at least one/two elements and
crashed on bare annotations:

- _models.py: `_, items_type = get_args(type_)` → ValueError for bare dict
- _models.py: `inner_type = args[0]`            → IndexError for bare list
- _utils/_transform.py (sync + async):
  `get_args(stripped_type)[1]`                  → IndexError for bare dict

All three now guard with len checks and fall back to `object` as the
item/value type when no type arguments are present, matching the behaviour
callers would expect from unparameterized collections.

Fixes anthropics#1619, anthropics#1626, anthropics#1628
@lavkeshdwivedi lavkeshdwivedi requested a review from a team as a code owner June 20, 2026 00:59
@lavkeshdwivedi

Copy link
Copy Markdown
Author

Friendly bump - this has been open since yesterday with no activity. Happy to address any review feedback.

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() crashes with ValueError when type_ is a bare dict

1 participant