-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: default web_search to approximate location (#3180) #3195
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
lrg913427-dot
wants to merge
1
commit into
openai:main
Choose a base branch
from
lrg913427-dot:feat/web-search-default-location
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.
+125
−8
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from openai._types import Omit, omit | ||
| from openai.lib._tools import _apply_web_search_default_location_tools | ||
|
|
||
|
|
||
| class TestApplyWebSearchDefaultLocationTools: | ||
| """Tests for _apply_web_search_default_location_tools.""" | ||
|
|
||
| def test_no_tools_returns_omit(self) -> None: | ||
| """When tools is Omit (not given), return it unchanged.""" | ||
| result = _apply_web_search_default_location_tools(omit) | ||
| assert result is omit | ||
|
|
||
| def test_empty_tools_list(self) -> None: | ||
| """An empty list of tools should be returned unchanged.""" | ||
| tools: list = [] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result is tools # same reference, no changes | ||
|
|
||
| def test_non_web_search_tools_unchanged(self) -> None: | ||
| """Tools that are not web_search should not be modified.""" | ||
| tools = [ | ||
| {"type": "function", "function": {"name": "my_func"}}, | ||
| {"type": "code_interpreter"}, | ||
| ] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result is tools # same reference, no changes | ||
|
|
||
| def test_web_search_injects_user_location(self) -> None: | ||
| """web_search without user_location should get one injected.""" | ||
| tools = [{"type": "web_search"}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result is not tools # new list created | ||
| assert result[0]["user_location"] == {"type": "approximate"} | ||
| assert result[0]["type"] == "web_search" | ||
|
|
||
| def test_web_search_with_existing_user_location_unchanged(self) -> None: | ||
| """web_search that already has user_location should not be overridden.""" | ||
| existing_loc = {"type": "approximate", "city": "London", "country": "GB"} | ||
| tools = [{"type": "web_search", "user_location": existing_loc}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result is tools # same reference, no changes needed | ||
| assert result[0]["user_location"] is existing_loc | ||
|
|
||
| def test_web_search_2025_08_26_injects(self) -> None: | ||
| tools = [{"type": "web_search_2025_08_26"}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result[0]["user_location"] == {"type": "approximate"} | ||
|
|
||
| def test_web_search_preview_injects(self) -> None: | ||
| tools = [{"type": "web_search_preview"}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result[0]["user_location"] == {"type": "approximate"} | ||
|
|
||
| def test_web_search_preview_2025_03_11_injects(self) -> None: | ||
| tools = [{"type": "web_search_preview_2025_03_11"}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result[0]["user_location"] == {"type": "approximate"} | ||
|
|
||
| def test_mixed_tools_only_web_search_modified(self) -> None: | ||
| """When mixing web_search and non-web-search tools, only web_search gets modified.""" | ||
| func_tool = {"type": "function", "function": {"name": "foo"}} | ||
| ws_tool = {"type": "web_search"} | ||
| tools = [func_tool, ws_tool] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| # function tool is unchanged | ||
| assert result[0] is func_tool | ||
| # web_search tool is a new dict with user_location injected | ||
| assert result[1]["user_location"] == {"type": "approximate"} | ||
| assert result[1]["type"] == "web_search" | ||
|
|
||
| def test_web_search_preserves_other_keys(self) -> None: | ||
| """Injecting user_location should not drop other keys on the tool dict.""" | ||
| tools = [{"type": "web_search", "extra_key": "value"}] | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result[0]["extra_key"] == "value" | ||
| assert result[0]["user_location"] == {"type": "approximate"} | ||
|
|
||
| def test_non_dict_tool_not_modified(self) -> None: | ||
| """Non-dict tools (e.g. string shorthand) should pass through.""" | ||
| tools = ["web_search"] # string, not dict | ||
| result = _apply_web_search_default_location_tools(tools) | ||
| assert result is tools # unchanged |
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.
If
toolsis a one-shot iterable (e.g., generator/iterator), this function consumes it in theforloop and then returns the original iterable whenchangedis false. In that case the caller (allresponses.*create/stream paths now using this helper) receives an already-exhausted iterable, so no tools are sent at all. This regression appears whenever users pass non-reiterableIterable[ToolParam]values that either contain no web-search tool or only web-search tools that already haveuser_location.Useful? React with 👍 / 👎.