Skip to content

fix: handle bare dict annotation in _transform_recursive - #3348

Closed
gavin913-lss wants to merge 1 commit into
openai:mainfrom
gavin913-lss:fix/transform-bare-dict
Closed

fix: handle bare dict annotation in _transform_recursive#3348
gavin913-lss wants to merge 1 commit into
openai:mainfrom
gavin913-lss:fix/transform-bare-dict

Conversation

@gavin913-lss

Copy link
Copy Markdown

What breaks

When a TypedDict field is annotated with a bare, unparameterised dict (e.g. metadata: dict instead of metadata: dict[str, str]), calling transform() raises IndexError.

from typing import TypedDict
from openai._utils._transform import transform

class TestParams(TypedDict, total=False):
    metadata: dict  # bare dict — no type parameters

result = transform({"metadata": {"key": "value"}}, TestParams)
# IndexError: tuple index out of range

Fixes #3338

Root cause

Both _transform_recursive and _async_transform_recursive unconditionally do get_args(stripped_type)[1] when origin == dict. For bare dict, get_args(dict) returns an empty tuple, so the index access crashes.

Fix

Add a length check before indexing. When get_args() returns fewer than 2 elements (bare dict), return the data as-is since there are no type parameters to recurse into.

args = get_args(stripped_type)
if len(args) < 2:
    return cast(object, data)
items_type = args[1]

Tests

Added test_bare_dict_in_typeddict() covering:

  • Bare dict with nested values
  • Empty dict

Both sync and async paths pass. All existing test_transform.py tests continue to pass.

When a TypedDict field is annotated with a bare dict (e.g. metadata: dict
instead of metadata: dict[str, str]), get_args() returns an empty tuple.
The previous code unconditionally indexed [1] from it, causing IndexError.

Add a length check before indexing and return the data as-is for bare
dicts since there are no type parameters to recurse into.

Fixes openai#3338
@gavin913-lss
gavin913-lss requested a review from a team as a code owner June 1, 2026 14:30

Copy link
Copy Markdown
Contributor

Thank you for the contribution. The missing type arguments for bare dict annotations are now handled in sync/async request transformation by #3760, which is merged into main.

Closing this PR because the merged change resolves the same crash. Please use a release containing #3760 when available; the fix is not included in v3.11.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: IndexError in _transform_recursive when TypedDict field uses bare dict annotation

3 participants