Tavily Integration for LangChain

0.2.17 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and use the `TavilySearch` tool to perform a web search. It highlights the importance of setting the `TAVILY_API_KEY` and how to invoke the tool with a natural language query to get structured results.

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'))

view raw JSON →