Skip to content

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

@devteamaegis

Description

@devteamaegis

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 an IndexError.

How to trigger

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)

Traceback

File ".../openai/_utils/_transform.py", line 183, in _transform_recursive
    items_type = get_args(stripped_type)[1]
                 ~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

Root cause

In _transform_recursive (and its async counterpart), the branch that handles origin == dict unconditionally does get_args(stripped_type)[1]. For a bare dict, get_args(dict) returns an empty tuple, so the index access crashes.

Fix

Guard the index access: store args = get_args(stripped_type) and only recurse with args[1] when len(args) >= 2; otherwise fall through and return data unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions