openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

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

Open adhavan18 opened this issue 5 months ago • 1 comments

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.

adhavan18 avatar Jun 15 '26 08:06 adhavan18

Gentle ping, this has been approved for 9 days. Happy to rebase or make any changes if needed.

adhavan18 avatar Jun 26 '26 03:06 adhavan18

this has three approvals now (@nomiveritas and @jbeckwith-oai) and has been sitting at blocked since 3 aug.

is there something still outstanding on my side, or is it just waiting on someone with merge rights? happy to rebase, split it, or adjust anything if there's a concern that hasn't been written down. just want to make sure it isn't stuck purely by accident.

adhavan18 avatar Aug 10 '26 13:08 adhavan18

From my side, there’s nothing else needed. I’ve approved the PR, and @jbeckwith-oai also gave a thorough approval with no outstanding technical concerns. It looks like the remaining step is on the maintainer/merge side.

nomiveritas avatar Aug 15 '26 11:08 nomiveritas

Thank you for the fix, tests, and follow-ups, and sorry for the wait. The missing type arguments for bare dict annotations are now handled in model construction and 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.

marcuswood-oai avatar Sep 10 '26 00:09 marcuswood-oai