-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: make ResponseFunctionWebSearch.action optional to handle null API responses #3449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
C1-BA-B1-F3
wants to merge
2
commits into
openai:main
Choose a base branch
from
C1-BA-B1-F3:fix/response-function-web-search-action-optional
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Tests for ResponseFunctionWebSearch type | ||
| # Verifies that the action field correctly handles None values from the API | ||
| import pytest | ||
|
|
||
| from openai.types.responses.response_function_web_search import ( | ||
| ActionSearch, | ||
| ActionOpenPage, | ||
| ActionFind, | ||
| ResponseFunctionWebSearch, | ||
| ) | ||
|
|
||
|
|
||
| def test_response_function_web_search_with_search_action(): | ||
| """Test that a web search call with a search action works correctly.""" | ||
| data = { | ||
| "id": "ws_123", | ||
| "action": { | ||
| "type": "search", | ||
| "query": "test query", | ||
| "queries": ["test query"], | ||
| }, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
| result = ResponseFunctionWebSearch(**data) | ||
| assert result.id == "ws_123" | ||
| assert result.action is not None | ||
| assert isinstance(result.action, ActionSearch) | ||
| assert result.action.type == "search" | ||
| assert result.action.query == "test query" | ||
|
|
||
|
|
||
| def test_response_function_web_search_with_open_page_action(): | ||
| """Test that a web search call with an open_page action works correctly.""" | ||
| data = { | ||
| "id": "ws_456", | ||
| "action": { | ||
| "type": "open_page", | ||
| "url": "https://example.com", | ||
| }, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
| result = ResponseFunctionWebSearch(**data) | ||
| assert result.id == "ws_456" | ||
| assert result.action is not None | ||
| assert isinstance(result.action, ActionOpenPage) | ||
| assert result.action.type == "open_page" | ||
| assert result.action.url == "https://example.com" | ||
|
|
||
|
|
||
| def test_response_function_web_search_with_find_action(): | ||
| """Test that a web search call with a find_in_page action works correctly.""" | ||
| data = { | ||
| "id": "ws_789", | ||
| "action": { | ||
| "type": "find_in_page", | ||
| "pattern": "search term", | ||
| "url": "https://example.com", | ||
| }, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
| result = ResponseFunctionWebSearch(**data) | ||
| assert result.id == "ws_789" | ||
| assert result.action is not None | ||
| assert isinstance(result.action, ActionFind) | ||
| assert result.action.type == "find_in_page" | ||
| assert result.action.pattern == "search term" | ||
|
|
||
|
|
||
| def test_response_function_web_search_with_none_action(): | ||
| """Test that a web search call with action=None works correctly. | ||
|
|
||
| This is a regression test for GitHub issue #3179. | ||
| The API can return null for the action field in some cases | ||
| (e.g., when the search is still in progress or the action | ||
| hasn't been determined yet). | ||
| """ | ||
| data = { | ||
| "id": "ws_abc", | ||
| "action": None, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
| result = ResponseFunctionWebSearch(**data) | ||
| assert result.id == "ws_abc" | ||
| assert result.action is None | ||
| assert result.status == "completed" | ||
|
|
||
|
|
||
| def test_response_function_web_search_without_action(): | ||
| """Test that a web search call without action field defaults to None.""" | ||
| data = { | ||
| "id": "ws_def", | ||
| "status": "in_progress", | ||
| "type": "web_search_call", | ||
| } | ||
| result = ResponseFunctionWebSearch(**data) | ||
| assert result.id == "ws_def" | ||
| assert result.action is None | ||
| assert result.status == "in_progress" | ||
|
|
||
|
|
||
| def test_response_function_web_search_action_none_safe_access(): | ||
| """Test that users can safely check action type with None action. | ||
|
|
||
| This demonstrates the fix for issue #3179 - users can now safely | ||
| access action.type without AttributeError when action is None. | ||
| """ | ||
| data_with_action = { | ||
| "id": "ws_1", | ||
| "action": {"type": "search", "query": "test"}, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
| data_without_action = { | ||
| "id": "ws_2", | ||
| "action": None, | ||
| "status": "completed", | ||
| "type": "web_search_call", | ||
| } | ||
|
|
||
| result_with = ResponseFunctionWebSearch(**data_with_action) | ||
| result_without = ResponseFunctionWebSearch(**data_without_action) | ||
|
|
||
| # This pattern should work without errors | ||
| search_count = 0 | ||
| for result in [result_with, result_without]: | ||
| if result.action is not None and result.action.type == "search": | ||
| search_count += 1 | ||
|
|
||
| assert search_count == 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repo's
ruff check .gate fails on this new file:ruff check tests/test_response_function_web_search.pyreportsI001for the import block andF401becausepytestis imported but unused. Remove the unused import and let ruff/isort reorder the remaining imports so CI can pass.Useful? React with 👍 / 👎.