Python
Integrate Tavily’s powerful APIs natively in your Python apps.
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
Tavily Search
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
Parameter | Type | Description | Default |
---|---|---|---|
query (required) | str | The query to run a search on. | |
search_depth | str | The depth of the search. It can be "basic" or "advanced" . | "basic" |
topic | str | The category of the search. Determines which agent will be used. Supported values are "general" and "news" . | "general" |
days | int | The number of days back from the current date to include in the results. Available only when using the "news" topic. | 3 |
time_range | str | The time range back from the current date. Accepted values include "day" , "week" , "month" , "year" or shorthand values "d" , "w" , "m" , "y" . | |
max_results | int | The maximum number of search results to return. It must be between 0 and 20 . | 5 |
include_images | bool | Include a list of query-related images in the response. | False |
include_image_descriptions | bool | Include a list of query-related images and their descriptions in the response. | False |
include_answer | bool or str | Include an answer to the query generated by an LLM based on search results. A "basic" (or True ) answer is quick but less detailed; an "advanced" answer is more detailed. | False |
include_raw_content | bool | Include the cleaned and parsed HTML content of each search result. | False |
include_domains | list[str] | A list of domains to specifically include in the search results. | [] |
exclude_domains | list[str] | A list of domains to specifically exclude from the search results. | [] |
Response format
The response object you receive will be in the following format:
Key | Type | Description |
---|---|---|
results | list[Result] | A list of sorted search results ranked by relevancy. |
query | str | Your search query. |
response_time | float | Your search result response time. |
answer (optional) | str | The answer to your search query, generated by an LLM based on Tavily’s search results. This is only available if include_answer is set to True . |
images (optional) | list[str] or list[ImageResult] | This is only available if include_images is set to True . A list of query-related image URLs. If include_image_descriptions is set to True , each entry will be an ImageResult . |
Results
Key | Type | Description |
---|---|---|
title | str | The title of the search result. |
url | str | The URL of the search result. |
content | str | The most query-related content from the scraped URL. Tavily uses proprietary AI to extract the most relevant content based on context quality and size. |
score | float | The relevance score of the search result. |
raw_content (optional) | str | The parsed and cleaned HTML content of the site. This is only available if include_raw_content is set to True . |
published_date (optional) | str | The publication date of the source. This is only available if the search topic is set to "news" . |
Image Results
If includeImageDescriptions
is set to true
, each image in the images
list will be in the following ImageResult
format:
Key | Type | Description |
---|---|---|
url | string | The URL of the image. |
description | string | An LLM-generated description of the image. |
Example
Tavily Extract
You can access Tavily Extract in Python through the client’s extract
function.
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
urls (required) | str or list[str] | The URL (or URLs) you want to extract. If a list is provided, it must not contain more than 20 URLs. | |
include_images | bool | Include a list of images extracted from the URLs in the response. | False |
extract_depth | str | The depth of the extraction process. You may experience higher latency with "advanced" extraction, but it offers a higher success rate and retrieves more data from the URL (e.g., tables, embedded content). "basic" extraction costs 1 API Credit per 5 successful URL extractions, while advanced extraction costs 2 API Credits per 5 successful URL extractions. | "basic" |
Response format
The response object you receive will be in the following format:
Key | Type | Description |
---|---|---|
results | list[SuccessfulResult] | A list of extracted content. |
failed_results | list[FailedResult] | A list of URLs that could not be processed. |
response_time | float | The search result response time. |
Successful Results
Each successful result in the results
list will be in the following SuccessfulResult
format:
Key | Type | Description |
---|---|---|
url | str | The URL of the webpage. |
raw_content | str | The raw content extracted. |
images (optional) | list[str] | This is only available if include_images is set to True . A list of extracted image URLs. |
Failed Results
Each failed result in the results
list will be in the following FailedResult
format:
Key | Type | Description |
---|---|---|
url | str | The URL that failed. |
error | str | An error message describing why it could not be processed. |