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
5 changes: 4 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
12 changes: 12 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down