Skip to content

fix: guard against IndexError/ValueError when dict annotation has no type args#3405

Open
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix/bare-dict-type-args
Open

fix: guard against IndexError/ValueError when dict annotation has no type args#3405
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix/bare-dict-type-args

Conversation

@adhavan18

@adhavan18 adhavan18 commented Jun 15, 2026

Copy link
Copy Markdown

Fixes #3338 and #3341.

Problem

get_args(dict) returns an empty tuple when the annotation is a bare, unparameterised dict (no [K, V]). Two places assumed there are always two args:

  • _transform.py: get_args(stripped_type)[1]IndexError
  • _models.py: _, items_type = get_args(type_)ValueError

Minimal reproducer:

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

class Params(TypedDict, total=False):
    metadata: dict  # bare dict, no [K, V]

transform({'metadata': {'key': 'val'}}, Params)  # IndexError

Fix

Check len(args) >= 2 before indexing; return early without transformation when the annotation is unparameterised.

Test

All existing tests pass. New test added for bare dict annotation.

…type args

`_transform_recursive` and its async counterpart (in `_utils/_transform.py`)
unconditionally did `get_args(stripped_type)[1]` for any `origin == dict`
branch.  When the annotation is a bare, unparameterised `dict` (no `[K, V]`
type arguments), `get_args(dict)` returns an empty tuple, so the index
access raises `IndexError: tuple index out of range`.

`construct_type` in `_models.py` had the same assumption, unpacking
`get_args(type_)` into exactly two targets with `_, items_type = ...`, which
raises `ValueError: not enough values to unpack (expected 2, got 0)` for a
bare `dict`.

Fix: in both sites, check `len(args) >= 2` before indexing.  A bare `dict`
annotation carries no value-type information, so returning the mapping
unchanged (rather than trying to recurse) is the correct behaviour.

Adds four regression tests.

Closes openai#3338, openai#3341
@adhavan18 adhavan18 requested a review from a team as a code owner June 15, 2026 08:31
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