fix: handle bare dict annotations in transform#3380
fix: handle bare dict annotations in transform#3380Muhtasim-Munif-Fahim wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves type transformation and realtime error typing by handling “bare” dict annotations during request transformation and exposing an error message field on realtime status models.
Changes:
- Handle
dictannotations without type arguments in_transform_recursive/_async_transform_recursive(avoidget_args(...)[1]failures). - Add
messagetoRealtimeResponseStatus.Error(beta and non-beta) and validate via a new test. - Add a regression test to ensure
TypedDictfields annotated as baredictare preserved.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_transform.py | Adds coverage for transforming a TypedDict containing a bare dict field. |
| tests/lib/test_realtime_response_status.py | Adds test ensuring realtime status error payload includes message. |
| src/openai/types/realtime/realtime_response_status.py | Adds Error.message to the non-beta realtime status type. |
| src/openai/types/beta/realtime/realtime_response_status.py | Adds Error.message to the beta realtime status type. |
| src/openai/_utils/_transform.py | Avoids indexing missing type args for bare dict annotations (sync/async paths). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| args = get_args(stripped_type) | ||
| if len(args) < 2: | ||
| return data | ||
| items_type = args[1] | ||
| return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()} |
| args = get_args(stripped_type) | ||
| if len(args) < 2: | ||
| return data |
| import pytest | ||
|
|
||
| from openai.types.beta.realtime import RealtimeResponseStatus as BetaRealtimeResponseStatus | ||
| from openai.types.realtime import RealtimeResponseStatus as RealtimeResponseStatus |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 136d4ec6b4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ids=["beta", "realtime"], | ||
| ) | ||
| def test_realtime_response_status_error_message(status_cls: type[BaseModel]) -> None: | ||
| status = status_cls.model_validate( |
There was a problem hiding this comment.
Use pydantic-compatible parsing in the new test
In the test-pydantic-v1 nox session, noxfile.py installs pydantic<2 and runs the test suite, but these generated models inherit from openai._models.BaseModel, which does not provide model_validate under Pydantic v1. This new test will raise AttributeError before reaching the assertions in that CI context; use the repository compatibility helper such as openai._compat.parse_obj/model_parse, or another v1-compatible construction path.
Useful? React with 👍 / 👎.
Fix
_transform_recursive()so baredictannotations do not crash when the code path inspectsget_args(stripped_type)[1].This keeps both sync and async transform paths aligned and preserves the original mapping when the dict annotation has no type arguments.
Tests:
python -m pytest tests/test_transform.py -k bare_dict_annotation -qpython -m pytest tests/test_transform.py -k "bare_dict_annotation or dictionary_items" -qpython -m py_compile src/openai/_utils/_transform.py tests/test_transform.py