Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,12 @@ 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 len(args) < 2:
# bare `dict` with no type parameters (e.g. `dict` instead of
# `dict[str, Any]`) — nothing to recurse into, return as-is.
return value
_, items_type = args
return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}

if (
Expand Down
16 changes: 16 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ class NestedModel(BaseModel):
assert cast(Any, m.nested) is False



def test_bare_dict_annotation() -> None:
"""construct_type should not crash on bare dict annotations without type parameters."""
# This was crashing with ValueError: not enough values to unpack
# because get_args(dict) returns () for unparameterised dict.
result = construct_type(value={"key": "value"}, type_=dict)
assert result == {"key": "value"}

result = construct_type(value={"a": 1, "b": 2}, type_=dict)
assert result == {"a": 1, "b": 2}

# empty dict
result = construct_type(value={}, type_=dict)
assert result == {}


def test_nested_dictionary_model() -> None:
class NestedModel(BaseModel):
nested: Dict[str, BasicModel]
Expand Down