Skip to main content

Instantiating a client

To interact with Tavily in Python, you must instatiate a client with your API key. For greater flexibility, we provide both a synchronous and an asynchronous client class. Once you have instantiated a client, call one of our supported methods (detailed below) to access the API.

Synchronous Client

Asynchronous Client

Project Tracking

You can attach a Project ID to your client to organize and track API usage by project. This is useful when a single API key is used across multiple projects.
Alternatively, you can set the TAVILY_PROJECT environment variable:
All requests made with this client will include the Project ID, allowing you to filter by project in the /logs endpoint and platform usage dashboard.

Session Tracking

You can attach session and user identifiers to your client for attribution and analytics across multi-step interactions and agent workflows. Each ID is sent as an HTTP header (X-Session-Id, X-Human-Id) on every request the client makes — across search, extract, crawl, map, and research.
You can also override either value on a per-call basis:
For security, Tavily hashes human IDs before processing or storing them. See Session Tracking in the API reference for the underlying header contract.

Proxies

If you would like to specify a proxy to be used when making requests, you can do so by passing in a proxy parameter on client instantiation. Proxy configuration is available in both the synchronous and asynchronous clients.
Alternatively, you can specify which proxies to use by setting the TAVILY_HTTP_PROXY and TAVILY_HTTPS_PROXY variables in your environment file.
NEW! Try our interactive API Playground to see each parameter in action, and generate ready-to-use Python snippets.
You can access Tavily Search in Python through the client’s search function.

Parameters

Response format

The response object you receive will be in the following format:

Results

Image Results

If includeImageDescriptions is set to true, each image in the images list will be in the following ImageResult format:

Example

Exact Match Example

Use exact_match with quoted phrases in your query to find results containing a specific name or phrase verbatim:

Tavily Extract

You can access Tavily Extract in Python through the client’s extract function.

Parameters

Response format

The response object you receive will be in the following format:

Successful Results

Each successful result in the results list will be in the following SuccessfulResult format:

Failed Results

Each failed result in the results list will be in the following FailedResult format:

Example

Tavily Crawl

You can access Tavily Crawl in Python through the crawl function.

Parameters

Response format

The response object you receive will be in the following format:

Results

Each successful result in the results list will be in the following Result format:

Example

Tavily Map

Tavily Map allows you to obtain a sitemap starting from a base URL. You can access Tavily Map in Python through the map function.

Parameters

Response format

The response object you receive will be in the following format:

Example

Tavily Hybrid RAG

Tavily Hybrid RAG is an extension of the Tavily Search API built to retrieve relevant data from both the web and an existing database collection. This way, a RAG agent can combine web sources and locally available data to perform its tasks. Additionally, data queried from the web that is not yet in the database can optionally be inserted into it. This will allow similar searches in the future to be answered faster, without the need to query the web again.

Parameters

The TavilyHybridClient class is your gateway to Tavily Hybrid RAG. There are a few important parameters to keep in mind when you are instantiating a Tavily Hybrid Client.

Methods

search(query, max_results=10, max_local=None, max_foreign=None, save_foreign=False, **kwargs) Performs a Tavily Hybrid RAG query and returns the retrieved documents as a list[dict] where the documents are sorted by decreasing relevancy to your query. Each returned document will have three properties - content (str), score (float), and origin, which is either local or foreign. Additional parameters can be provided as keyword arguments (detailed below). The keyword arguments supported by this method are: search_depth, topic, include_raw_content, include_domains,exclude_domains.

Setup

MongoDB setup

You will need to have a MongoDB collection with a vector search index. You can follow the MongoDB Documentation to learn how to set this up.

Cohere API Key

By default, embedding and ranking use the Cohere API, our recommended option. Unless you want to provide a custom embedding and ranking function, you’ll need to get an API key from Cohere and set it as an environment variable named CO_API_KEY If you decide to stick with Cohere, please note that you’ll need to install the Cohere Python package as well:

Tavily Hybrid RAG Client setup

Once you are done setting up your database, you’ll need to create a MongoDB Client as well as a Tavily Hybrid RAG Client. A minimal setup would look like this:

Usage

Once you create the proper clients, you can easily start searching. A few simple examples are shown below. They assume you’ve followed earlier steps. You can use most of the Tavily Search parameters with Tavily Hybrid RAG as well.

Simple Tavily Hybrid RAG example

This example will look for context about Leo Messi on the web and in the local database. Here, we get 5 sources, both from our database and from the web, but we want to exclude unwanted-domain.com from our web search results:
Here, we want to prioritize the number of local sources, so we will get 2 foreign (web) sources, and 5 sources from our database:
Note: The sum of max_local and max_foreign can exceed max_results, but only the top max_results results will be returned.

Adding retrieved data to the database

If you want to add the retrieved data to the database, you can do so by setting the save_foreign parameter to True:
This will use our default saving function, which stores the content and its embedding.

Examples

Sample 1: Using a custom saving function

You might want to add some extra properties to documents you’re inserting or even discard some of them based on custom criteria. This can be done by passing a function to the save_foreign parameter:

Sample 2: Using a custom embedding function

By default, we use Cohere for our embeddings. If you want to use your own embeddings, can pass a custom embedding function to the TavilyHybridClient: