Skip to main content
The web search tool for chat completions allows you to search the web for current information during a conversation. This guide explains how to use the web search tool in a chat completion and provides an example of how to use it.
You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

Tool structure

The web search tool allows you to search the web during a chat completion.
To customize search parameters like including raw source text, limiting the number of sources, or specifying time ranges, include these requirements in your prompt to the model. The model uses these parameters when making the web search call. For example, your message to the model might include:
  • “Include raw source text”
  • “Limit the number of sources to 5”
  • “Search for news articles from the last 30 days”
Example:
To use the web search tool, add it to the tools array in your chat-completion endpoint request. The web search tool object has the following structure: Below shows the structure of the web search tool object.
You can only pass one prebuilt tool in the tools array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:

Response format

For non-streaming responses, the search results and answer are in the choices[0].message.content field. For streaming responses, the search results and answer are in the choices[0].delta.content field. The response also contains a web_search_data field that contains the following information:
You can use the web search tool with streaming chat responses, but the web search tool call is not streamed. The sources comes back in the choices[0].delta.content field once the full search is complete. Then, the response from the model is streamed.If you are unfamiliar with the chat completions endpoint or streaming vs. non-streaming responses, learn more in the chat completion guide.
See the chat completion endpoint for more information on the response fields.

Usage example

This example uses the web search tool to find current information about AI developments during a chat completion. To use the web search tool:
  1. Create a tools array that specifies the web search tool.
  • The tool array should include the type and function parameters. The function parameter should include the include_domains and exclude_domains parameters.
  1. Create a messages array that contains the user message that prompts the model to use the web search tool.
  • The message should contain the requirements for the search, such as including raw source text, limiting the number of sources, or specifying time ranges.
  1. Call the chat.chat method with the tools parameter set to the tools array and tool_choice set to auto.
If you want to verify the web search data in the response, you can print the web_search_data field to see the search query, answer, and sources that the web search tool used.
By following this guide, you can use the web search tool to have the model search the web for current information during a chat completion.

Next steps

Learn more about the prebuilt tools available for chat completions: