From 75766bda5efd5438fed9046623fe24491dae5648 Mon Sep 17 00:00:00 2001 From: zhangjiarui Date: Tue, 9 Jun 2026 09:32:04 +0800 Subject: [PATCH] fix: handle bare dict annotations in construct_type Avoid unpacking missing type arguments when model construction sees an unparameterized dict field. Fixes #3341 --- src/openai/_models.py | 5 ++++- tests/test_models.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/openai/_models.py b/src/openai/_models.py index ed4c1f82d6..a63e687025 100644 --- a/src/openai/_models.py +++ b/src/openai/_models.py @@ -657,7 +657,10 @@ 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] + if len(args) < 2: + 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..a73bb3e0a5 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -133,6 +133,18 @@ class NestedModel(BaseModel): assert cast(Any, m.nested) is False +def test_bare_dictionary() -> None: + class NestedModel(BaseModel): + nested: dict + + m = NestedModel.construct(nested={"hello": {"world": True}}) + assert m.nested == {"hello": {"world": True}} + + # mismatched types + m = NestedModel.construct(nested=False) + assert cast(Any, m.nested) is False + + def test_nested_dictionary_model() -> None: class NestedModel(BaseModel): nested: Dict[str, BasicModel]