Created
May 9, 2026 23:50
-
-
Save thetafferboy/61d30d33258490e1497ffb95396dfcaa to your computer and use it in GitHub Desktop.
OpenAI Responses API example
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
| """ | |
| Inspecting the web searches performed by the OpenAI Responses API. | |
| When you enable the `web_search` tool, the model decides whether to issue one or | |
| more search queries. Each query is recorded as a `web_search_call` item in the | |
| response output, with the action type and the actual queries the model ran. | |
| Docs: https://developers.openai.com/api/docs/guides/tools-web-search | |
| """ | |
| import os | |
| from openai import OpenAI | |
| client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) | |
| QUERY = "What are the latest Google algorithm updates announced in 2026?" | |
| response = client.responses.create( | |
| model="gpt-5.5", | |
| tools=[{"type": "web_search"}], | |
| input=QUERY, | |
| ) | |
| # The model's final answer | |
| print("=== Answer ===") | |
| print(response.output_text) | |
| print() | |
| # Inspect every web_search_call to see the actual queries the model ran | |
| print("=== Web searches performed ===") | |
| for item in response.output: | |
| if item.type != "web_search_call": | |
| continue | |
| action = item.action # SearchAction | OpenPageAction | FindInPageAction | |
| if action.type == "search": | |
| # `queries` is the array of queries for this call. | |
| # The legacy `query` field is deprecated — prefer `queries`. | |
| for q in (action.queries or []): | |
| print(f"- search: {q}") | |
| # Sources the model considered for this search call (when available) | |
| for src in (getattr(action, "sources", None) or []): | |
| print(f" source: {src.url}") | |
| elif action.type == "open_page": | |
| print(f"- open_page: {action.url}") | |
| elif action.type == "find_in_page": | |
| print(f"- find_in_page: {action.pattern!r} on {action.url}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment