Skip to content

Commit c2dea26

Browse files
author
zhangjiarui
committed
fix: handle bare dict annotations in transform
Avoid indexing missing type arguments when request transform logic sees an unparameterized dict field. Fixes #3338
1 parent 3842a5e commit c2dea26

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/openai/_utils/_transform.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ def _transform_recursive(
180180
return _transform_typeddict(data, stripped_type)
181181

182182
if origin == dict and is_mapping(data):
183-
items_type = get_args(stripped_type)[1]
183+
args = get_args(stripped_type)
184+
if len(args) < 2:
185+
return data
186+
187+
items_type = args[1]
184188
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
185189

186190
if (
@@ -346,7 +350,11 @@ async def _async_transform_recursive(
346350
return await _async_transform_typeddict(data, stripped_type)
347351

348352
if origin == dict and is_mapping(data):
349-
items_type = get_args(stripped_type)[1]
353+
args = get_args(stripped_type)
354+
if len(args) < 2:
355+
return data
356+
357+
items_type = args[1]
350358
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
351359

352360
if (

tests/test_transform.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ class Bar7(TypedDict):
154154
foo: str
155155

156156

157+
class Foo7BareDict(TypedDict):
158+
metadata: dict
159+
160+
161+
@parametrize
162+
@pytest.mark.asyncio
163+
async def test_bare_dict_typeddict_field(use_async: bool) -> None:
164+
data = {"metadata": {"key": "value"}}
165+
166+
assert await transform(data, Foo7BareDict, use_async) == data
167+
168+
157169
@parametrize
158170
@pytest.mark.asyncio
159171
async def test_ignores_invalid_input(use_async: bool) -> None:

0 commit comments

Comments
 (0)