Skip to content

fix(_models): guard against bare dict in construct_type()#1620

Open
devteamaegis wants to merge 1 commit into
anthropics:mainfrom
devteamaegis:fix/valueerror-not-enough-values-to-unpack
Open

fix(_models): guard against bare dict in construct_type()#1620
devteamaegis wants to merge 1 commit into
anthropics:mainfrom
devteamaegis:fix/valueerror-not-enough-values-to-unpack

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

Calling construct_type() with type_=dict (a bare, unparameterised dict) raises ValueError: not enough values to unpack (expected 2, got 0). This hits whenever a model field is annotated as plain dict rather than Dict[str, SomeType].

File "src/anthropic/_models.py", line 650, in construct_type
    _, items_type = get_args(type_)  # Dict[_, items_type]
ValueError: not enough values to unpack (expected 2, got 0)

Why it happens

The origin == dict branch unconditionally unpacks get_args(type_) as exactly two values. For a bare dict with no type parameters, get_args returns an empty tuple (), making the two-value unpack fail.

Fix

Capture get_args(type_) into a local args variable and fall back to object as items_type when args is empty. When items_type is object, every value passes through construct_type unchanged, which is the correct behaviour for an untyped dict.

Test

Added test_construct_type_bare_dict in tests/test_models.py — passes type_=dict and asserts the original dict is returned without raising.

Fixes #1619

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 anthropics#1619
@devteamaegis devteamaegis requested a review from a team as a code owner May 31, 2026 22:48
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