From 450507be8391d36cb3a3c98af1bf25f412884c01 Mon Sep 17 00:00:00 2001 From: C1-BA-B1-F3 Date: Fri, 26 Jun 2026 07:56:46 +0800 Subject: [PATCH] fix: handle bare dict/list annotations in construct_type and transform Guard against empty type args when using bare dict or list annotations (no type parameters, e.g. instead of ). Previously: - construct_type() raised ValueError on bare dict - construct_type() raised IndexError on bare list - _transform_recursive() raised IndexError on bare dict Now these cases gracefully return the data as-is, matching the behavior of parameterized types when the data already matches. Fixes #3338, #3341 --- src/openai/_models.py | 13 +++++++++---- src/openai/_utils/_transform.py | 26 ++++++++++++++++++++++---- tests/test_models.py | 22 ++++++++++++++++++++++ tests/test_transform.py | 22 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/openai/_models.py b/src/openai/_models.py index ed4c1f82d6..e2877caad5 100644 --- a/src/openai/_models.py +++ b/src/openai/_models.py @@ -657,8 +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] - return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} + dict_args = get_args(type_) + if len(dict_args) >= 2: + items_type = dict_args[1] + return {key: construct_type(value=item, type_=items_type) for key, item in value.items()} + return dict(value) if ( not is_literal_type(type_) @@ -678,8 +681,10 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any] if not is_list(value): return value - inner_type = args[0] # List[inner_type] - return [construct_type(value=entry, type_=inner_type) for entry in value] + if args: + inner_type = args[0] # List[inner_type] + return [construct_type(value=entry, type_=inner_type) for entry in value] + return list(value) if origin == float: if isinstance(value, int): diff --git a/src/openai/_utils/_transform.py b/src/openai/_utils/_transform.py index 414f38c340..76079b5aa5 100644 --- a/src/openai/_utils/_transform.py +++ b/src/openai/_utils/_transform.py @@ -180,8 +180,11 @@ def _transform_recursive( return _transform_typeddict(data, stripped_type) if origin == dict and is_mapping(data): - items_type = get_args(stripped_type)[1] - return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} + args = get_args(stripped_type) + if len(args) >= 2: + items_type = args[1] + return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} + return dict(data) if ( # List[T] @@ -196,6 +199,12 @@ def _transform_recursive( if isinstance(data, dict): return cast(object, data) + # bare list/iterable/sequence with no type args - return as-is + if not get_args(stripped_type): + if is_list(data): + return data + return list(data) + inner_type = extract_type_arg(stripped_type, 0) if _no_transform_needed(inner_type): # for some types there is no need to transform anything, so we can get a small @@ -346,8 +355,11 @@ async def _async_transform_recursive( return await _async_transform_typeddict(data, stripped_type) if origin == dict and is_mapping(data): - items_type = get_args(stripped_type)[1] - return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} + args = get_args(stripped_type) + if len(args) >= 2: + items_type = args[1] + return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} + return dict(data) if ( # List[T] @@ -362,6 +374,12 @@ async def _async_transform_recursive( if isinstance(data, dict): return cast(object, data) + # bare list/iterable/sequence with no type args - return as-is + if not get_args(stripped_type): + if is_list(data): + return data + return list(data) + inner_type = extract_type_arg(stripped_type, 0) if _no_transform_needed(inner_type): # for some types there is no need to transform anything, so we can get a small diff --git a/tests/test_models.py b/tests/test_models.py index cc204bac1d..6e2770be11 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1015,3 +1015,25 @@ class Model(BaseModel): # falls back to list of chars rather than calling str(["h", "e", "l", "l", "o"]) assert m.data["items"] == ["h", "e", "l", "l", "o"] assert m.model_dump()["data"]["items"] == ["h", "e", "l", "l", "o"] + + +def test_bare_dict_annotation() -> None: + 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} + + result = construct_type(value="not a dict", type_=dict) + assert result == "not a dict" + + +def test_bare_list_annotation() -> None: + result = construct_type(value=[1, 2, 3], type_=list) + assert result == [1, 2, 3] + + result = construct_type(value=["a", "b"], type_=list) + assert result == ["a", "b"] + + result = construct_type(value="not a list", type_=list) + assert result == "not a list" diff --git a/tests/test_transform.py b/tests/test_transform.py index bece75dfc7..e4024aa3a5 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -458,3 +458,25 @@ async def test_strips_notgiven(use_async: bool) -> None: async def test_strips_omit(use_async: bool) -> None: assert await transform({"foo_bar": "bar"}, Foo1, use_async) == {"fooBar": "bar"} assert await transform({"foo_bar": omit}, Foo1, use_async) == {} + + +class BareDictParams(TypedDict, total=False): + metadata: dict + + +@parametrize +@pytest.mark.asyncio +async def test_bare_dict_in_typeddict(use_async: bool) -> None: + result = await transform({"metadata": {"key": "value"}}, BareDictParams, use_async) + assert result == {"metadata": {"key": "value"}} + + +class BareListParams(TypedDict, total=False): + items: list + + +@parametrize +@pytest.mark.asyncio +async def test_bare_list_in_typeddict(use_async: bool) -> None: + result = await transform({"items": [1, 2, 3]}, BareListParams, use_async) + assert result == {"items": [1, 2, 3]}