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.
What breaks
When a
TypedDictfield is annotated with a bare, unparameteriseddict(e.g.metadata: dictinstead ofmetadata: dict[str, str]), callingtransform()raises anIndexError.How to trigger
Traceback
Root cause
In
_transform_recursive(and its async counterpart), the branch that handlesorigin == dictunconditionally doesget_args(stripped_type)[1]. For a baredict,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 withargs[1]whenlen(args) >= 2; otherwise fall through and returndataunchanged.