Skip to content

Commit 9cb1c17

Browse files
authored
docs(examples): use UserLocation dataclass in WebSearchTool example
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)
1 parent f20aa40 commit 9cb1c17

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

examples/tools/web_search.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import asyncio
22

3-
from agents import Agent, Runner, WebSearchTool, trace
4-
3+
from agents import Agent, Runner, WebSearchTool, UserLocation, trace
54

65
async def main():
76
agent = Agent(
87
name="Web searcher",
98
instructions="You are a helpful agent.",
10-
tools=[WebSearchTool(user_location={"type": "approximate", "city": "New York"})],
9+
tools=[
10+
WebSearchTool(
11+
user_location=UserLocation(
12+
type="approximate",
13+
city="New York"
14+
),
15+
# Feel free to adjust how much context the tool retrieves:
16+
# search_context_size="medium",
17+
)
18+
],
1119
)
1220

1321
with trace("Web search example"):
@@ -16,8 +24,7 @@ async def main():
1624
"search the web for 'local sports news' and give me 1 interesting update in a sentence.",
1725
)
1826
print(result.final_output)
19-
# The New York Giants are reportedly pursuing quarterback Aaron Rodgers after his ...
20-
21-
27+
# Now this _will_ be localized to New York!
28+
2229
if __name__ == "__main__":
2330
asyncio.run(main())

0 commit comments

Comments
 (0)