Skip to content

BUG: construct_type() crashes with ValueError when type_ is a bare dict #1619

@devteamaegis

Description

@devteamaegis

What breaks

Calling `construct_type()` with `type_=dict` (no type parameters) raises a `ValueError`:

```
ValueError: not enough values to unpack (expected 2, got 0)
```

Traceback:
```
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)
```

How to trigger

```python
from anthropic.models import construct_type
result = construct_type(value={"key": "value"}, type
=dict)
```

This occurs when a model field is annotated as plain `dict` instead of `Dict[str, SomeType]`. The `origin == dict` branch unconditionally unpacks `get_args(type_)` as exactly two values, but `get_args(dict)` returns an empty tuple `()` for a bare `dict`.

Root cause

Line 650 in `models.py`:
```python
, items_type = get_args(type) # Dict[
, items_type]
```
There is no guard for the case where `get_args` returns fewer than two elements.

Fix

Guard with `if args:` and fall back to `object` as the items type when no type parameters are present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions