From 715194b9ec120cff0cbd086347ee6f13bde82063 Mon Sep 17 00:00:00 2001 From: Ishaan Samantray Date: Sun, 31 May 2026 23:18:01 -0400 Subject: [PATCH] fix(_models): guard against empty get_args() on bare dict annotation 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 #3341 --- src/openai/_models.py | 6 +++++- tests/test_models.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/openai/_models.py b/src/openai/_models.py index ed4c1f82d6..52b56a2387 100644 --- a/src/openai/_models.py +++ b/src/openai/_models.py @@ -657,7 +657,11 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_mapping(value): return value - _, items_type = get_args(type_) # Dict[_, items_type] + args = get_args(type_) + if not args: + # bare `dict` with no type arguments — return value as-is + return value + _, items_type = args # Dict[_, items_type] return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} if ( diff --git a/tests/test_models.py b/tests/test_models.py index cc204bac1d..92111840bf 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -655,6 +655,12 @@ class Model(BaseModel): assert m.value == "foo" +def test_construct_type_bare_dict_annotation() -> None: + # bare `dict` (no type args) must not raise ValueError on unpack + result = construct_type(value={"key": "value"}, type_=dict) + assert result == {"key": "value"} + + def test_discriminated_unions_invalid_data() -> None: class A(BaseModel): type: Literal["a"]