fix: make ResponseFunctionWebSearch.action optional to handle null API responses#3449
Conversation
Guard against empty type args when using bare dict or list annotations (no type parameters, e.g. instead of ). Previously: - construct_type() raised ValueError on bare dict - construct_type() raised IndexError on bare list - _transform_recursive() raised IndexError on bare dict Now these cases gracefully return the data as-is, matching the behavior of parameterized types when the data already matches. Fixes openai#3338, openai#3341
…I responses Fixes openai#3179 The API can return null for the action field in web search calls (e.g., when the search is still in progress or the action hasn't been determined yet). This change makes the action field Optional[Action] with a default of None, matching the actual API behavior. This allows users to safely check action.type without getting an AttributeError when action is None.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 54fafb3494
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -0,0 +1,133 @@ | |||
| # Tests for ResponseFunctionWebSearch type | |||
| # Verifies that the action field correctly handles None values from the API | |||
| import pytest | |||
There was a problem hiding this comment.
Fix new test imports to satisfy ruff
The repo's ruff check . gate fails on this new file: ruff check tests/test_response_function_web_search.py reports I001 for the import block and F401 because pytest is imported but unused. Remove the unused import and let ruff/isort reorder the remaining imports so CI can pass.
Useful? React with 👍 / 👎.
Problem
The field is typed as (required), but the API can return for this field in some cases. This causes when users try to access without null-checking first.
Related issue: #3179
Root Cause
When the API returns a web search call with (e.g., when the search is still in progress or the action hasn't been determined yet), the SDK crashes because the type definition doesn't allow values.
Fix
Changed to in , matching the actual API behavior.
Testing
Added comprehensive tests covering:
All tests pass.