Tavily Integration for LangChain
The `langchain-tavily` Python package is the official LangChain integration of Tavily, providing robust web search, content extraction, website mapping, and crawling functionalities. It is an actively maintained package that receives continuous updates with the latest features, designed to enhance AI agents and RAG pipelines with real-time, accurate, and factual information.
Warnings
- breaking The `TavilySearchResults` tool previously available under `langchain_community.tools.tavily_search` has been deprecated. Users should migrate to the dedicated `langchain-tavily` package for all Tavily integrations.
- gotcha A `TAVILY_API_KEY` is required for authentication with the Tavily API. This key must be set as an environment variable or passed directly during instantiation.
- gotcha Some parameters for `TavilySearch`, such as `include_answer` and `include_raw_content`, cannot be modified during tool invocation. They must be set during the initial instantiation of the `TavilySearch` object.
- gotcha When performing time-sensitive searches (e.g., 'news today'), the default search behavior might not always return the most real-time results, potentially referencing older articles.
Install
-
pip install -U langchain-tavily
Imports
- TavilySearch
from langchain_tavily import TavilySearch
- TavilyExtract
from langchain_tavily import TavilyExtract
- TavilyMap
from langchain_tavily import TavilyMap
- TavilyCrawl
from langchain_tavily import TavilyCrawl
- TavilyResearch
from langchain_tavily import TavilyResearch
- TavilySearchResults
from langchain_tavily import TavilySearchResults
Quickstart
import os
from langchain_tavily import TavilySearch
# Set your Tavily API key as an environment variable
# You can get one from https://tavily.com/
# os.environ["TAVILY_API_KEY"] = "your_tavily_api_key"
# Ensure TAVILY_API_KEY is set (using .get for runnable example)
tavily_api_key = os.environ.get("TAVILY_API_KEY", "dummy_key")
# Instantiate the Tavily Search tool
# Parameters like 'max_results' can be set here.
# For a full list of parameters, refer to the documentation.
tool = TavilySearch(max_results=5, topic="general", tavily_api_key=tavily_api_key)
# Invoke the tool with a query
query = "Latest news about AI advancements in healthcare"
results = tool.invoke({"query": query})
print(f"Tavily Search Results for '{query}':")
for i, result in enumerate(results):
print(f"Result {i+1}: {result['title']} - {result['url']}")
# For detailed content, uncomment below (if include_raw_content was enabled at instantiation)
# print(result.get('raw_content', 'No raw content'))