Skip to content

fix(client): handle bare dict/list annotations in construct_type and transform#1672

Open
Zawwarsami16 wants to merge 2 commits into
anthropics:mainfrom
Zawwarsami16:fix/bare-container-annotations
Open

fix(client): handle bare dict/list annotations in construct_type and transform#1672
Zawwarsami16 wants to merge 2 commits into
anthropics:mainfrom
Zawwarsami16:fix/bare-container-annotations

Conversation

@Zawwarsami16

Copy link
Copy Markdown

What

A field annotated with a bare, unparameterized dict or list (rather than Dict[K, V] / List[T]) crashes the runtime helpers, because get_args() returns an empty tuple in that case and the code indexes into it unconditionally.

Three reported crashes, one root cause:

Fix

Guard each get_args() lookup and fall back to object as the element type when no type parameters are present. With object as the element type, values pass straight through unchanged — which is the right behavior for an unparameterized container, since there's no inner type to coerce to.

Repro (before)

from anthropic._models import construct_type
from anthropic._utils._transform import transform

construct_type(value={"k": "v"}, type_=dict)   # ValueError
construct_type(value=["a", "b"], type_=list)   # IndexError
transform({"k": "v"}, dict)                     # IndexError

All three (plus the async transform path) now return the input unchanged.

Tests

  • tests/test_models.py: test_bare_dict_annotation, test_bare_list_annotation
  • tests/test_transform.py: test_bare_dict_annotation (parametrized sync + async)

Full test_models.py and test_transform.py suites pass locally (122 tests); ruff check + format are clean.

get_args() returns an empty tuple for an unparameterized `dict` or
`list` annotation, so the dict branch's two-value unpack raised
ValueError and the list branch's args[0] raised IndexError. Guard both
and fall back to `object` as the element type so values pass through
unchanged instead of crashing.

Fixes anthropics#1619, anthropics#1626
A bare `dict` annotation (no type parameters) made both the sync and
async transform helpers index get_args(...)[1] on an empty tuple,
raising IndexError. Guard the lookup and fall back to `object` so the
mapping is passed through unchanged.

Fixes anthropics#1628
@Zawwarsami16 Zawwarsami16 requested a review from a team as a code owner June 13, 2026 03:04
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.

1 participant