Skip to content

Web search

Web search lets a model retrieve current information with a provider-hosted tool. Add a WebSearchTool to the request; the provider executes the search and the SDK returns the answer as text with normalized Citation metadata.

types.ts
interface WebSearchTool {
type: "web_search";
/**
* Restricts search results to these domains when supported by the provider.
*/
allowed_domains?: string[];
/**
* An approximate user location used to localize web search results.
*/
user_location?: WebSearchUserLocation;
}
interface WebSearchUserLocation {
/**
* The city of the user.
*/
city?: string;
/**
* The region or state of the user.
*/
region?: string;
/**
* The two-letter ISO 3166-1 country code of the user.
*/
country?: string;
/**
* The IANA timezone of the user.
*/
timezone?: string;
}

allowed_domains restricts results to specific domains, while user_location provides an approximate location for localized results. OpenAI and Anthropic support both options. Google supports the base web search tool and rejects these options rather than silently ignoring them.

Web search is supported by OpenAI Responses, Anthropic Messages, and Google GenerateContent. It is not supported by the OpenAI Chat Completions, Cohere V2 Chat, or Mistral Chat Completions adapters.

The example defaults to OpenAI. Set PROVIDER and MODEL to run the same code with another supported provider and model.

web-search.ts
import { getModel } from "./get-model.ts";
const provider = process.env["PROVIDER"] ?? "openai";
const modelId = process.env["MODEL"] ?? "gpt-5.6-sol";
const model = getModel(provider, modelId);
const response = await model.generate({
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Use web search to find the official IANA page about reserved domains. Reply with one sentence containing the word IANA and cite the source.",
},
],
},
],
tools: [{ type: "web_search" }],
});
console.dir(response.content, { depth: null });

Streaming returns citation deltas with the text deltas. The accumulator combines them into citations on the final text parts.

stream-web-search.ts
import { StreamAccumulator } from "@hoangvvo/llm-sdk";
import { getModel } from "./get-model.ts";
const provider = process.env["PROVIDER"] ?? "openai";
const modelId = process.env["MODEL"] ?? "gpt-5.6-sol";
const model = getModel(provider, modelId);
const stream = model.stream({
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Use web search to find the official IANA page about reserved domains. Reply with one sentence containing the word IANA and cite the source.",
},
],
},
],
tools: [{ type: "web_search" }],
});
const accumulator = new StreamAccumulator();
let current = await stream.next();
while (!current.done) {
console.dir(current.value, { depth: null });
accumulator.addPartial(current.value);
current = await stream.next();
}
const response = accumulator.computeResponse();
console.dir(response.content, { depth: null });