anthropic-sdk-python icon indicating copy to clipboard operation
anthropic-sdk-python copied to clipboard

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

Open Zawwarsami16 opened this issue 5 months ago • 0 comments

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:

  • construct_type(value={...}, type_=dict) → ValueError: not enough values to unpack (expected 2, got 0) (_models.py) — #1619
  • construct_type(value=[...], type_=list) → IndexError: tuple index out of range (_models.py) — #1626
  • transform({...}, dict) → IndexError: tuple index out of range, in both the sync and async paths (_transform.py) — #1628

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.

Zawwarsami16 avatar Jun 13 '26 03:06 Zawwarsami16