Web search works in playground, but not via api

In playground, I choose gtp-4o-mini, enter a simple system instruction and select web search tool. I grab the code and run it using the responses rest api, and I get an error:

Hosted tool ‘web_search_preview’ is not supported with gtp-4o-mini.

I can’t figure out why it works in playgroud but not via code.

I tried creating a new API key as some other developers mentioned they thought a new key needed to be generated that works with this new tool.
I tried gtp-4o, but same issue as well.

Any tips?

1 Like

Well I know its working as I use it with Kruel.ai,

So it could be your call?

Here is a test script for you.

import os
import requests
import json
from dotenv import load_dotenv
import os

load_dotenv()  # This loads the .env file
api_key = os.getenv('OPENAI_API_KEY')
def test_web_search_api():
    # 1) Retrieve your API key from .env or environment variable
    openai_api_key = os.getenv("OPENAI_API_KEY")
    if not openai_api_key:
        print("Error: OPENAI_API_KEY is not set. Make sure it's in your environment or .env file.")
        return

    url = "https://api.openai.com/v1/chat/completions"

    # 2) Minimal payload to request a web search
    payload = {
        "model": "gpt-4o-mini-search-preview",  # or "gpt-4o-search-preview"
        "web_search_options": {
            "search_context_size": "low"
        },
        "messages": [
            {
                "role": "user",
                "content": "What is the stock price of Apple Inc. right now?",
            }
        ]
    }

    headers = {
        "Authorization": f"Bearer {openai_api_key}",
        "Content-Type": "application/json"
    }

    # 3) Make the request
    response = requests.post(url, headers=headers, json=payload, timeout=30)

    # 4) Log results
    print("Status Code:", response.status_code)
    print("Response JSON:")
    try:
        data = response.json()
        print(json.dumps(data, indent=2))
    except Exception as e:
        print(f"Failed to parse JSON response. Error: {e}")
        print("Raw response text:", response.text)


if __name__ == "__main__":
    test_web_search_api()

you will have to set your openai API key in your .env that that script is ran in.
if this works for you than you know its your function. if it doesn’t work for you could be something else. not sure if they limited the tool to certain tiers of accounts , or regions in the world. those are other factors to research if it was a world wide release.

Apologies for not mentioning up front - I’m using the responses api.

Hey! You mentioned you’re using the responses API — just a heads up, that one doesn’t support tools like web_search_preview. You’ll need to use the chat/completions endpoint with gpt-4o-mini-search-preview or gpt-4o-search-preview for tool access.

Try the code I gave you. If it still fails, check:

  • Whether your OpenAI account has access to the preview tools
  • If you’re using the correct endpoint and model
  • If there are any regional restrictions (rare but possible)

Alternative a two step approach in calls one for the web info and feed that into your response along with your other stuff than you can respond on web and other data.

Ps. I could be wrong on this so if I am let me know but pretty sure

here is the info
https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat

Welcome to the dev forum @omacoder

AFAIK web-search tool is supported on Responses API.

Here’s the boilerplate code to try this on your end:

from openai import OpenAI
client = OpenAI()

response = client.responses.create(
    model="gpt-4o-mini",
    tools=[{"type": "web_search_preview"}],
    input="What was a positive news story from today?"
)

print(response.output_text)
1 Like

In Chat Completions, that is where you have to use the special model that costs you every time, not ideal.

The Responses API is specifically built for serving internal tools.

Here’s that example call - which works fine, just tested. It just needs your annotation parser and content markup.

from openai import OpenAI; import json
client = OpenAI()

response = client.responses.create(
  model="gpt-4o-mini",
  input=[
    {
      "role": "system",
      "content": [
        {
          "type": "input_text",
          "text": "You are a research assistant. Today is Mon March 31, 2025"
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "New stuff announced by OpenAI recently?"
        }
      ]
    }
  ],
  tools=[
    {
      "type": "web_search_preview",
      "user_location": {
        "type": "approximate",
        "country": "US"
      },
      "search_context_size": "medium"
    }
  ],
  max_output_tokens=2048,
  top_p=0.7,
  store=False
)
for i in response.output:
    print(json.dumps(i.model_dump(),indent=3))
OpenAI news

OpenAI has recently announced several significant developments:

Expansion of Leadership Roles

On March 24, 2025, OpenAI’s Chief Operating Officer, Brad Lightcap, was appointed to lead global expansion and corporate partnerships. CEO Sam Altman will focus more on technical aspects, including research and product initiatives. This strategic shift aims to strengthen OpenAI’s position in the rapidly growing AI industry. (reuters.com)

Transition to For-Profit Entity

OpenAI is in the process of transitioning to a for-profit entity by the end of 2025 to fully secure a $40 billion funding round led by SoftBank. This move is essential for obtaining the necessary capital to develop advanced AI models. (reuters.com)

New Developer Tools

On March 11, 2025, OpenAI launched the Responses API, replacing the Assistants API, to aid developers in building advanced AI agents capable of executing complex tasks autonomously. This tool is free for developers, with the old API expected to phase out by mid-2026. (reuters.com)

Partnerships and Investments

OpenAI signed a nearly $12 billion contract with CoreWeave to supply computing power for training and running AI models over the next five years. Additionally, OpenAI is taking a $350 million equity stake in CoreWeave ahead of its anticipated $35 billion public listing. (ft.com)

Financial Projections

Despite generating $3.7 billion in revenue last year, OpenAI does not expect to achieve positive cash flow until 2029 due to significant costs associated with developing advanced AI systems, including expenses for chips, data centers, and talent acquisition. (reuters.com)

Recent OpenAI Developments:

You can just use print(response.output_text) to see the collected user output.


Continued inability to use the model in the file, and you may need to contact OpenAI through the platform site’s “help” to have it provisioned correctly.

EDIT
I found that if you prompt heavily, it improves massively. Add instructions that it must use the tool, and repeat in the prompt as well. that solved my use case :slight_smile:
**

I also find the web search api unreliable so far. Sometimes it does the search, and other times it doesn’t. I tried instructions, and also forcing tool use with tool_choice.

Hopefully they work out the issues soon.

I added one of the key elements in my example: knowledge of the date.

Knowing the date powers decision-making of when tool calling would be useful, and can be used in the query your AI writes.

The challenge is the web tool has OpenAI’s language, not yours.

Also, providing the web search location, and its subfields not employed in the tool example, will help power the search after it is called also.

Because of this tool’s one-shot and dump-out-text nature, that makes it pretty hard to make a Deep Researcher that can go after multi-turn knowledge.