From 9cb1c17f114701ab1ef46d8c120e4cb69114552e Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sat, 19 Jul 2025 18:31:12 +0500 Subject: [PATCH 1/4] docs(examples): use UserLocation dataclass in WebSearchTool example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update refines the `examples/web_search.py` snippet to instantiate `WebSearchTool.user_location` with the proper `UserLocation` dataclass rather than a raw `dict`. This change improves type safety, editor autocompletion, and future‑proofs the example against any additional `UserLocation` fields. See the official docs for `WebSearchTool` and its `user_location` parameter here: [https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool](https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool) --- examples/tools/web_search.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/tools/web_search.py b/examples/tools/web_search.py index 35eeb6804..97e9abfa5 100644 --- a/examples/tools/web_search.py +++ b/examples/tools/web_search.py @@ -1,13 +1,21 @@ import asyncio -from agents import Agent, Runner, WebSearchTool, trace - +from agents import Agent, Runner, WebSearchTool, UserLocation, trace async def main(): agent = Agent( name="Web searcher", instructions="You are a helpful agent.", - tools=[WebSearchTool(user_location={"type": "approximate", "city": "New York"})], + tools=[ + WebSearchTool( + user_location=UserLocation( + type="approximate", + city="New York" + ), + # Feel free to adjust how much context the tool retrieves: + # search_context_size="medium", + ) + ], ) with trace("Web search example"): @@ -16,8 +24,7 @@ async def main(): "search the web for 'local sports news' and give me 1 interesting update in a sentence.", ) print(result.final_output) - # The New York Giants are reportedly pursuing quarterback Aaron Rodgers after his ... - - + # Now this _will_ be localized to New York! + if __name__ == "__main__": asyncio.run(main()) From 2d9e3f8637a5f7f39c2f5dcf7b2c40b32dfaf6d3 Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sat, 19 Jul 2025 18:36:59 +0500 Subject: [PATCH 2/4] docs(examples): use UserLocation dataclass in WebSearchTool example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update refines the `examples/web_search.py` snippet to instantiate `WebSearchTool.user_location` with the proper `UserLocation` dataclass rather than a raw `dict`. This change improves type safety, editor autocompletion, and future‑proofs the example against any additional `UserLocation` fields. See the official docs for `WebSearchTool` and its `user_location` parameter here: [https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool](https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool) --- examples/tools/web_search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/tools/web_search.py b/examples/tools/web_search.py index 97e9abfa5..b7022a848 100644 --- a/examples/tools/web_search.py +++ b/examples/tools/web_search.py @@ -2,6 +2,7 @@ from agents import Agent, Runner, WebSearchTool, UserLocation, trace + async def main(): agent = Agent( name="Web searcher", @@ -25,6 +26,6 @@ async def main(): ) print(result.final_output) # Now this _will_ be localized to New York! - + if __name__ == "__main__": asyncio.run(main()) From 74b46d5c5de65b3d5d7bbf4252f768b4c3c7d4dd Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sat, 19 Jul 2025 18:44:31 +0500 Subject: [PATCH 3/4] docs(examples): use UserLocation dataclass in WebSearchTool example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update refines the `examples/web_search.py` snippet to instantiate `WebSearchTool.user_location` with the proper `UserLocation` dataclass rather than a raw `dict`. This change improves type safety, editor autocompletion, and future‑proofs the example against any additional `UserLocation` fields. See the official docs for `WebSearchTool` and its `user_location` parameter here: [https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool](https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool) --- examples/tools/web_search.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/tools/web_search.py b/examples/tools/web_search.py index b7022a848..2bdcd2ede 100644 --- a/examples/tools/web_search.py +++ b/examples/tools/web_search.py @@ -1,6 +1,7 @@ import asyncio -from agents import Agent, Runner, WebSearchTool, UserLocation, trace +from agents import Agent, Runner, WebSearchTool, trace +from agents.tool import UserLocation async def main(): @@ -27,5 +28,6 @@ async def main(): print(result.final_output) # Now this _will_ be localized to New York! + if __name__ == "__main__": asyncio.run(main()) From e2468a22162f02bc936208f98fea48aa57415c32 Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sat, 19 Jul 2025 18:49:43 +0500 Subject: [PATCH 4/4] docs(examples): use UserLocation dataclass in WebSearchTool example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update refines the `examples/web_search.py` snippet to instantiate `WebSearchTool.user_location` with the proper `UserLocation` dataclass rather than a raw `dict`. This change improves type safety, editor autocompletion, and future‑proofs the example against any additional `UserLocation` fields. See the official docs for `WebSearchTool` and its `user_location` parameter here: [https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool](https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.WebSearchTool) --- examples/tools/web_search.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/tools/web_search.py b/examples/tools/web_search.py index 2bdcd2ede..43a05eaea 100644 --- a/examples/tools/web_search.py +++ b/examples/tools/web_search.py @@ -1,7 +1,7 @@ import asyncio from agents import Agent, Runner, WebSearchTool, trace -from agents.tool import UserLocation +from agents.tool import UserLocation # type: ignore[attr-defined] async def main(): @@ -28,6 +28,5 @@ async def main(): print(result.final_output) # Now this _will_ be localized to New York! - if __name__ == "__main__": asyncio.run(main())