Skip to content

fix: handle bare dict annotation in construct_type()#3347

Open
gavin913-lss wants to merge 1 commit into
openai:mainfrom
gavin913-lss:fix/construct-type-bare-dict
Open

fix: handle bare dict annotation in construct_type()#3347
gavin913-lss wants to merge 1 commit into
openai:mainfrom
gavin913-lss:fix/construct-type-bare-dict

Conversation

@gavin913-lss
Copy link
Copy Markdown

What breaks

construct_type() in src/openai/_models.py crashes with ValueError when type_ is a bare, unparameterised dict (no [K, V] type arguments).

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

Fixes #3341

Root cause

Line 660 does _, items_type = get_args(type_) after checking origin == dict. For bare dict, get_args(dict) returns an empty tuple (), and unpacking with two targets raises ValueError.

Fix

Add a length check before unpacking. When get_args() returns fewer than 2 elements (bare dict), return the value as-is since there are no type parameters to recurse into.

args = get_args(type_)
if len(args) < 2:
    return value  # bare dict, no type parameters
_, items_type = args

Tests

Added test_bare_dict_annotation() covering:

  • Bare dict with string values
  • Bare dict with int values
  • Empty dict

All existing test_models.py tests continue to pass.

When type_ is a bare dict without type parameters (e.g. dict instead of
dict[str, Any]), get_args(dict) returns an empty tuple. The previous code
unconditionally unpacked two values from it, causing ValueError.

Add a length check before unpacking and return the value as-is for bare
dicts since there are no type parameters to recurse into.

Fixes openai#3341
@gavin913-lss gavin913-lss requested a review from a team as a code owner June 1, 2026 14:19
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 on bare dict annotation (no type args)

2 participants