Skip to content

fix(_transform): guard IndexError for bare dict annotation#3339

Open
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/indexerror-bare-dict
Open

fix(_transform): guard IndexError for bare dict annotation#3339
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/indexerror-bare-dict

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

Calling transform() on a TypedDict whose field is annotated with a bare, unparameterised dict (e.g. metadata: dict) raises an IndexError. The bug affects both the sync and async transform paths.

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

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

Why it happens

_transform_recursive and _async_transform_recursive both unconditionally access get_args(stripped_type)[1] when origin == dict. For a bare dict, get_args(dict) returns an empty tuple, so index 1 does not exist.

Fix

Store args = get_args(stripped_type) and only recurse with args[1] when len(args) >= 2. When no type arguments are present the data is returned unchanged, which is the correct behaviour for an unparameterised dict.

Test

Added test_bare_dict_no_indexerror in tests/test_transform.py: passes {"metadata": {"key": "value"}} through a TypedDict with a bare dict field and asserts the result is returned unchanged without error. Runs in both sync and async variants.

Fixes #3338

…tion

When a TypedDict field is annotated with a bare `dict` (no type
parameters), `get_args(dict)` returns an empty tuple and the previous
unconditional `get_args(stripped_type)[1]` raised IndexError.

Guard with `args = get_args(stripped_type)` and only recurse into the
value type when `len(args) >= 2`; otherwise return the data unchanged.
Same fix applied to `_async_transform_recursive`.
@devteamaegis devteamaegis requested a review from a team as a code owner May 31, 2026 22:45
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

1 participant