chatlas websitechatlas
  • Reference
  • Changelog
  1. Built-in tools
  2. tool_web_search
  • Function reference
  • Chat model providers
    • ChatAnthropic
    • ChatAuto
    • ChatAzureOpenAI
    • ChatBedrockAnthropic
    • ChatCloudflare
    • ChatDatabricks
    • ChatDeepSeek
    • ChatGithub
    • ChatGoogle
    • ChatGroq
    • ChatHuggingFace
    • ChatMistral
    • ChatOllama
    • ChatOpenAI
    • ChatOpenRouter
    • ChatPerplexity
    • ChatPortkey
    • ChatSnowflake
    • ChatVertex
  • The chat object
    • Chat
  • Image input
    • content_image_file
    • content_image_plot
    • content_image_url
  • PDF input
    • content_pdf_file
    • content_pdf_url
  • Tool calling
    • Tool
    • ToolRejectError
  • Built-in tools
    • tool_web_search
    • tool_web_fetch
  • Parallel and batch chat
    • parallel_chat
    • parallel_chat_text
    • parallel_chat_structured
    • batch_chat
    • batch_chat_text
    • batch_chat_structured
    • batch_chat_completed
  • Prompt interpolation
    • interpolate
    • interpolate_file
  • Turns
    • AssistantTurn
    • UserTurn
    • SystemTurn
    • Turn
  • Query token usage
    • token_usage
  • Implement a model provider
    • Provider
  • User-facing types
    • types.Content
    • types.ContentImage
    • types.ContentImageInline
    • types.ContentImageRemote
    • types.ContentJson
    • types.ContentText
    • types.ContentToolRequest
    • types.ContentToolResult
    • types.ContentToolRequestSearch
    • types.ContentToolResponseSearch
    • types.ContentToolRequestFetch
    • types.ContentToolResponseFetch
    • types.ChatResponse
    • types.ChatResponseAsync
    • types.ImageContentTypes
    • types.MISSING_TYPE
    • types.MISSING
    • types.SubmitInputArgsT
    • types.TokenUsage
    • types.ToolAnnotations
    • types.ToolInfo

On this page

  • tool_web_search
    • Prerequisites
    • Parameters
    • Returns
    • Examples
    • Note
  • Report an issue
  • Edit this page

tool_web_search

tool_web_search(
    allowed_domains=None,
    blocked_domains=None,
    user_location=None,
    max_uses=None,
)

Create a web search tool for use with chat models.

This function creates a provider-agnostic web search tool that can be registered with any supported chat provider. The tool allows the model to search the web for up-to-date information.

Supported providers: OpenAI, Claude (Anthropic), Google (Gemini)

Prerequisites

  • OpenAI: Web search is available by default.
  • Claude: Web search must be enabled in the Anthropic Console by your organization administrator. It costs extra ($10 per 1,000 searches at time of writing).
  • Google: Web search (grounding) is available by default with Gemini.

Parameters

Name Type Description Default
allowed_domains Optional[list[str]] Restrict searches to specific domains (e.g., ['nytimes.com', 'bbc.com']). Supported by OpenAI and Claude. Cannot be used with blocked_domains. None
blocked_domains Optional[list[str]] Exclude specific domains from searches. Supported by Claude and Google. Cannot be used with allowed_domains. None
user_location 'Optional[UserLocation]' Location information to localize search results. A dictionary with optional keys: country (2-letter ISO code), city, region, and timezone (IANA timezone like ‘America/New_York’). Supported by OpenAI and Claude. None
max_uses Optional[int] Maximum number of searches allowed per request. Only supported by Claude. None

Returns

Name Type Description
ToolWebSearch A web search tool that can be registered with chat.register_tool().

Examples

from chatlas import ChatOpenAI, tool_web_search

# Basic usage
chat = ChatOpenAI()
chat.register_tool(tool_web_search())
chat.chat("What are the top news stories today?")

# With domain restrictions
chat = ChatOpenAI()
chat.register_tool(tool_web_search(allowed_domains=["nytimes.com", "bbc.com"]))
chat.chat("What's happening in the economy?")

# With location for localized results
chat = ChatOpenAI()
chat.register_tool(
    tool_web_search(
        user_location={
            "country": "US",
            "city": "San Francisco",
            "timezone": "America/Los_Angeles",
        }
    )
)
chat.chat("What's the weather forecast?")

Note

Not all parameters are supported by all providers:

  • allowed_domains: OpenAI, Claude
  • blocked_domains: Claude, Google
  • user_location: OpenAI, Claude
  • max_uses: Claude only

Unsupported parameters are silently ignored by providers that don’t support them.

ToolRejectError
tool_web_fetch

Proudly supported by Posit

 
  • Report an issue
  • Edit this page