Skip to content

BUG: construct_type() crashes with ValueError on bare dict annotation (no type args) #3341

Description

@devteamaegis

What breaks

construct_type() in src/openai/_models.py (line 660) does _, items_type = get_args(type_) after checking origin == dict. When type_ is the bare, unparameterised dict class (no [K, V] type arguments), get_args(dict) returns an empty tuple (). Unpacking it with two targets raises ValueError: not enough values to unpack (expected 2, got 0). This crashes whenever a Pydantic BaseModel field or any response type is annotated as plain dict and the library tries to deserialise a mapping value into it.

from openai._models import construct_type
result = construct_type(value={"key": "value"}, type_=dict)
# ValueError: not enough values to unpack (expected 2, got 0)

Traceback

File "/tmp/repro.py", line 2, in <module>
    result = construct_type(value={"key": "value"}, type_=dict)
  File ".../src/openai/_models.py", line 660, in construct_type
    _, items_type = get_args(type_)  # Dict[_, items_type]
    ^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions