Skip to content

fix(models): handle bare dict/list type annotations without type parameters#3360

Open
tianming-1996 wants to merge 1 commit into
openai:mainfrom
tianming-1996:fix/issue-3338-3341-bare-dict
Open

fix(models): handle bare dict/list type annotations without type parameters#3360
tianming-1996 wants to merge 1 commit into
openai:mainfrom
tianming-1996:fix/issue-3338-3341-bare-dict

Conversation

@tianming-1996
Copy link
Copy Markdown

@tianming-1996 tianming-1996 commented Jun 4, 2026

Summary

  • Fixed IndexError in _transform_recursive when TypedDict fields use bare dict annotations (e.g. metadata: dict instead of metadata: dict[str, str])
  • Fixed ValueError in construct_type() when deserializing bare dict annotations
  • Fixed RuntimeError from extract_type_arg() for bare list annotations in transform
  • Fixed the same issues in async counterparts (_async_transform_recursive)

Changes

File Change
src/openai/_utils/_transform.py Add get_args() length check in dict branch; handle bare list in list/iterable/sequence branches (sync + async)
src/openai/_models.py Add get_args() length check in dict branch; handle bare list in list branch
tests/test_transform.py Add 3 regression tests for bare dict/list annotations (sync + async)
tests/test_models.py Add 4 regression tests for construct_type() with bare dict/list

Root Cause

All four bugs share the same root cause: when a type like dict or list is used without generic parameters, typing.get_args() returns an empty tuple (). Code that unconditionally indexed into this tuple (e.g. get_args(type_)[1]) or unpacked it (e.g. _, items_type = get_args(type_)) would crash.

Test Plan

  • All 10 new regression tests pass (sync + async)
  • All 124 existing tests in test_transform.py and test_models.py pass
  • CI checks pass

Fixes #3338
Fixes #3341

…meters

When a TypedDict field is annotated with bare 'dict' or 'list' (without
type parameters like dict[str, str] or list[int]), calling transform() or
construct_type() would crash with IndexError or ValueError.

Fixes openai#3338: IndexError in _transform_recursive when TypedDict field uses
bare dict annotation
Fixes openai#3341: construct_type() crashes with ValueError on bare dict annotation
Also fixes: bare list annotation causing extract_type_arg() to fail

Changes:
- _transform.py: Add args length check in dict branch, handle bare list in
  list/iterable/sequence branches for both sync and async versions
- _models.py: Add args length check in dict branch, handle bare list in
  list branch
- Add regression tests for bare dict and bare list annotations

Signed-off-by: tianming-1996 <tianming@example.com>
@tianming-1996 tianming-1996 marked this pull request as ready for review June 4, 2026 03:39
@tianming-1996 tianming-1996 requested a review from a team as a code owner June 4, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant