Skip to content

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

Open
kazuki-netizen wants to merge 1 commit into
anthropics:mainfrom
kazuki-netizen:fix/bare-container-types
Open

fix(client): handle bare dict/list annotations in construct_type and transform#1731
kazuki-netizen wants to merge 1 commit into
anthropics:mainfrom
kazuki-netizen:fix/bare-container-types

Conversation

@kazuki-netizen

Copy link
Copy Markdown

Fixes #1619, #1626, #1628.

Problem

construct_type() and _transform_recursive / _async_transform_recursive index the result of get_args() unconditionally. For an unparameterized annotation, get_args(dict) and get_args(list) return (), so the index access fails whenever a field is annotated as a bare dict or list instead of Dict[K, V] / List[T]:

  • construct_type(value={"k": "v"}, type_=dict)ValueError: not enough values to unpack (expected 2, got 0) (_models.py)
  • construct_type(value=["a", "b"], type_=list)IndexError: tuple index out of range (_models.py)
  • transform({"k": "v"}, dict) / async_transform({"k": "v"}, dict)IndexError: tuple index out of range (_utils/_transform.py)

Fix

Guard each access and fall back to object as the item type when no type parameter is present. This matches the semantics of Dict[str, object] / List[object]: items are returned as-is with no further coercion. Parameterized annotations and the non-mapping / non-list passthrough branches are unchanged.

Verification

  • Added regression tests in tests/test_models.py and tests/test_transform.py (sync + async). They fail on main and pass with this change.
  • tests/test_models.py + tests/test_transform.py: 122 passed.
  • ruff check and ruff format --check clean.

🤖 Generated with Claude Code

…transform

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@kazuki-netizen kazuki-netizen requested a review from a team as a code owner July 3, 2026 05:35
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