fix: guard against IndexError/ValueError when dict annotation has no type args
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.
Gentle ping, this has been approved for 9 days. Happy to rebase or make any changes if needed.
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.
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.
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.