Is the new web search parameter working?

Testing out the new web search tool but getting an error either the supplied code from the Documentation over at

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


TypeError Traceback (most recent call last)
in <cell line: 0>()
10
11 # Make the API request
—> 12 completion = client.chat.completions.create(
13 model=“gpt-4o-search-preview”,
14 web_search_options={},

/usr/local/lib/python3.11/dist-packages/openai/_utils/_utils.py in wrapper(*args, **kwargs)
277 msg = f"Missing required argument: {quote(missing[0])}"
278 raise TypeError(msg)
→ 279 return func(*args, **kwargs)
280
281 return wrapper # type: ignore

TypeError: Completions.create() got an unexpected keyword argument ‘web_search_options’

Using Python.

This reply has been updated:

Here is a sample code snippet from the docs for using the web search tool with the completions API:

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

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-search-preview",
    web_search_options={},
    messages=[
        {
            "role": "user",
            "content": "What was a positive news story from today?",
        }
    ],
)

print(completion.choices[0].message.content)

Hi!
You need to use the responses API.
In your code your are invoking the chat.completions API:

completion = client.chat.completions.create(

I copied this from the documentation:

from openai import OpenAI
client = OpenAI()

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

print(response.output_text)

https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses#page-top

4 Likes

so you’re saying web searches don’t work with the chat completions API? Because I see it web_search_options as a valid argument in self.client.beta.chat.completions.parse

for me personally I’m getting this error:

openai.BadRequestError: Error code: 400 - {‘error’: {‘message’: “Unknown parameter: ‘web_search_options’.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘web_search_options’, ‘code’: ‘unknown_parameter’}}

Can you rephrase the question?
It’s a valid argument but you get a invalid parameter error message?

You do not need to use the responses API.

On chat completions, you’d use the offered model gpt-4o-search-preview-2025-03-11. It has built-in internet search retrieval, and some limitations in what else it can do.

Then the only parameter you need to address is the usefulness of adding those web_search_options, where size has a bit of effect on the default price of $0.035 per call (currently erroneously billed at $0.11 per call).

The API SDK code can deny unknown API parameters if not updated.


web_search_options (object, optional)

Tool configuration for searching the web to gather relevant results for a response.

  • search_context_size (string, optional, default: medium)
     High-level guidance on how much context window space to use for the search. One of low, medium, high.

  • user_location (object or null, optional)
     Approximate location parameters for the search.

    • type (string, required)
       The location approximation type. Always approximate.

    • approximate (object, required)
       Approximate location parameters.

      • city (string, optional)
         Free text for the user’s city, e.g. San Francisco.
      • country (string, optional)
         Two-letter ISO country code, e.g. US.
      • region (string, optional)
         Free text for the user’s region, e.g. California.
      • timezone (string, optional)
         IANA timezone of the user, e.g. America/Los_Angeles.
3 Likes

You are correct.
I just noted there is a little toggle on the documents page.

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

This should work when using chat completions:

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-search-preview",
    web_search_options={},
    messages=[
        {
            "role": "user",
            "content": "What was a positive news story from today?",
        }
    ],
)

print(completion.choices[0].message.content)

@rabbitandpork

3 Likes