Skip to content

fix(_models): guard against empty get_args() on bare dict annotation#3342

Open
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/indexerror-valueerror-unsafe-tuple-unpack-on-bare-dict-annotation
Open

fix(_models): guard against empty get_args() on bare dict annotation#3342
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/indexerror-valueerror-unsafe-tuple-unpack-on-bare-dict-annotation

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

construct_type() in src/openai/_models.py crashes with ValueError: not enough values to unpack (expected 2, got 0) when the field type is a bare, unparameterised dict with no type arguments. The code does _, items_type = get_args(type_) unconditionally after detecting origin == dict, but get_args(dict) returns an empty tuple () for the bare class.

Why it happens

The guard only checks origin == dict; it never verifies that get_args actually returned two arguments before unpacking.

Fix

Added a two-line guard: if get_args(type_) returns an empty tuple (bare dict), return the mapping value as-is instead of attempting the unpack. The parameterised Dict[K, V] path is unchanged.

Test

Added test_construct_type_bare_dict_annotation to tests/test_models.py. It calls construct_type(value={"key": "value"}, type_=dict) and asserts the result equals the input dict without raising.

Fixes #3341

construct_type() unpacked get_args(type_) with two targets directly after
detecting origin == dict. When type_ is the bare unparameterised dict class,
get_args(dict) returns () and the unpack raised ValueError. Add a guard that
returns the value as-is when no type arguments are present.

Fixes openai#3341
@devteamaegis devteamaegis requested a review from a team as a code owner June 1, 2026 03:18
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)

1 participant