How to Intermediate · 4 min read

How to use web search with Responses API

Quick answer
Use the OpenAI SDK to call the responses.search endpoint with your query and specify the web_search tool in the tools parameter. This enables the model to perform live web searches and incorporate results in its responses.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the official openai Python package and set your API key as an environment variable.

  • Install package: pip install openai
  • Set environment variable: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install openai

Step by step

This example shows how to perform a web search using the Responses API with the web_search tool enabled. The model will use live web search results to answer the query.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define the web search tool
web_search_tool = [{
    "type": "web_search",
    "name": "web_search",
    "description": "Search the web for relevant information"
}]

messages = [
    {"role": "user", "content": "What are the latest advancements in AI in 2026?"}
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=web_search_tool
)

print("Response:", response.choices[0].message.content)
output
Response: The latest advancements in AI in 2026 include improvements in multimodal models, real-time web search integration, and more efficient reasoning capabilities...

Common variations

You can customize the web search integration by:

  • Using different models like gpt-4o-mini for faster responses.
  • Enabling streaming by adding stream=True to chat.completions.create.
  • Using async calls with asyncio for concurrent requests.
python
import asyncio
import os
from openai import OpenAI

async def async_web_search():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    web_search_tool = [{
        "type": "web_search",
        "name": "web_search",
        "description": "Search the web for relevant information"
    }]
    messages = [{"role": "user", "content": "Latest AI trends in 2026?"}]

    stream = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        tools=web_search_tool,
        stream=True
    )

    async for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        print(delta, end="", flush=True)

asyncio.run(async_web_search())
output
The latest AI trends in 2026 include advancements in large multimodal models, integration of real-time web search capabilities, and improved efficiency in reasoning tasks...

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the response does not include web search results, ensure you have included the tools parameter with the web_search tool.
  • For rate limit errors, consider retrying after some delay or upgrading your API plan.

Key Takeaways

  • Use the tools parameter with web_search to enable live web search in Responses API calls.
  • Always set your API key via os.environ["OPENAI_API_KEY"] for secure authentication.
  • Streaming and async calls improve responsiveness and scalability for web search queries.
  • Verify tool inclusion if web search results are missing in responses.
  • Check rate limits and authentication if you encounter errors.
Verified 2026-04 · gpt-4o-mini
Verify ↗