|
| 1 | +--- |
| 2 | +title: "TavilyFetcher" |
| 3 | +id: tavilyfetcher |
| 4 | +slug: "/tavilyfetcher" |
| 5 | +description: "Use Tavily Extract to fetch and parse content from URLs as Haystack Documents. Unlike web search, it retrieves content from the URLs you provide rather than discovering them via a query." |
| 6 | +--- |
| 7 | + |
| 8 | +# TavilyFetcher |
| 9 | + |
| 10 | +Use Tavily Extract to fetch and parse content from URLs as Haystack Documents. Unlike web search, it retrieves content from the URLs you provide rather than discovering them via a query. |
| 11 | + |
| 12 | +<div className="key-value-table"> |
| 13 | + |
| 14 | +| | | |
| 15 | +| --- | --- | |
| 16 | +| **Most common position in a pipeline** | In indexing or query pipelines as the data fetching step | |
| 17 | +| **Mandatory init variables** | `api_key`: The Tavily API key. Can be set with the `TAVILY_API_KEY` env var. | |
| 18 | +| **Mandatory run variables** | `urls`: A list of URLs (strings) to extract content from (max 20 per request) | |
| 19 | +| **Output variables** | `documents`: A list of [Documents](../../concepts/data-classes.mdx)<br />`meta`: Request-level metadata (`response_time`, `usage`, `request_id`, `failed_results`) | |
| 20 | +| **API reference** | [Tavily](/reference/integrations-tavily) | |
| 21 | +| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/tavily | |
| 22 | +| **Package name** | `tavily-haystack` | |
| 23 | + |
| 24 | +</div> |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +`TavilyFetcher` wraps the [Tavily Extract API](https://docs.tavily.com/documentation/api-reference/endpoint/extract) to retrieve and parse web page content from one or more specified URLs. PDF URLs are also supported. Each successful URL becomes a Haystack `Document` with page content in `content` and metadata such as `url` (and optionally `images`) in `meta`. |
| 29 | + |
| 30 | +This component is complementary to [`TavilyWebSearch`](../websearch/tavilywebsearch.mdx): search discovers URLs from a query, while `TavilyFetcher` extracts full content from URLs you already have. |
| 31 | + |
| 32 | +### Extract parameters |
| 33 | + |
| 34 | +You can control extraction behavior at initialization: |
| 35 | + |
| 36 | +- `extract_depth`: `"basic"` (fast, lower cost) or `"advanced"` (more data including tables; higher latency and cost). Defaults to `"basic"`. |
| 37 | +- `include_images`: When `True`, image URLs are stored on each Document under `meta["images"]`. Defaults to `False`. |
| 38 | +- `extract_params`: Extra kwargs forwarded to the Tavily Extract API (for example `format`, `include_favicon`, `query`, `chunks_per_source`). See the [Tavily Extract API reference](https://docs.tavily.com/documentation/api-reference/endpoint/extract). |
| 39 | + |
| 40 | +Of these, only `extract_params` can also be passed to `run()` to override it for a single call. Note that an `extract_params` dictionary passed to `run()` fully replaces the one set at initialization instead of being merged with it. |
| 41 | + |
| 42 | +### Authorization |
| 43 | + |
| 44 | +`TavilyFetcher` uses the `TAVILY_API_KEY` environment variable by default. You can also pass the key explicitly: |
| 45 | + |
| 46 | +```python |
| 47 | +from haystack.utils import Secret |
| 48 | +from haystack_integrations.components.fetchers.tavily import TavilyFetcher |
| 49 | + |
| 50 | +fetcher = TavilyFetcher(api_key=Secret.from_token("<your-api-key>")) |
| 51 | +``` |
| 52 | + |
| 53 | +To get an API key, sign up at [tavily.com](https://tavily.com). |
| 54 | + |
| 55 | +### Installation |
| 56 | + |
| 57 | +Install the Tavily integration with: |
| 58 | + |
| 59 | +```shell |
| 60 | +pip install tavily-haystack |
| 61 | +``` |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +### On its own |
| 66 | + |
| 67 | +```python |
| 68 | +from haystack_integrations.components.fetchers.tavily import TavilyFetcher |
| 69 | + |
| 70 | +fetcher = TavilyFetcher(extract_depth="basic") |
| 71 | + |
| 72 | +result = fetcher.run(urls=["https://docs.haystack.deepset.ai/docs/intro"]) |
| 73 | +documents = result["documents"] |
| 74 | +meta = result["meta"] |
| 75 | + |
| 76 | +for doc in documents: |
| 77 | + print(f"{doc.meta.get('url')}: {len(doc.content or '')} chars") |
| 78 | + |
| 79 | +print("failed:", meta.get("failed_results")) |
| 80 | +``` |
| 81 | + |
| 82 | +### In a pipeline |
| 83 | + |
| 84 | +Below is an example of an indexing pipeline that uses `TavilyFetcher` to extract documentation pages and store them in an `InMemoryDocumentStore`. |
| 85 | + |
| 86 | +```python |
| 87 | +from haystack import Pipeline |
| 88 | +from haystack.document_stores.in_memory import InMemoryDocumentStore |
| 89 | +from haystack.components.preprocessors import DocumentSplitter |
| 90 | +from haystack.components.writers import DocumentWriter |
| 91 | +from haystack_integrations.components.fetchers.tavily import TavilyFetcher |
| 92 | + |
| 93 | +document_store = InMemoryDocumentStore() |
| 94 | + |
| 95 | +fetcher = TavilyFetcher(extract_depth="basic") |
| 96 | +splitter = DocumentSplitter(split_by="sentence", split_length=5) |
| 97 | +writer = DocumentWriter(document_store=document_store) |
| 98 | + |
| 99 | +indexing_pipeline = Pipeline() |
| 100 | +indexing_pipeline.add_component("fetcher", fetcher) |
| 101 | +indexing_pipeline.add_component("splitter", splitter) |
| 102 | +indexing_pipeline.add_component("writer", writer) |
| 103 | + |
| 104 | +indexing_pipeline.connect("fetcher.documents", "splitter.documents") |
| 105 | +indexing_pipeline.connect("splitter.documents", "writer.documents") |
| 106 | + |
| 107 | +indexing_pipeline.run( |
| 108 | + data={ |
| 109 | + "fetcher": { |
| 110 | + "urls": ["https://docs.haystack.deepset.ai/docs/intro"], |
| 111 | + }, |
| 112 | + }, |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +### Asynchronous execution |
| 117 | + |
| 118 | +`TavilyFetcher` also supports asynchronous execution through `run_async()`: |
| 119 | + |
| 120 | +```python |
| 121 | +import asyncio |
| 122 | + |
| 123 | +from haystack_integrations.components.fetchers.tavily import TavilyFetcher |
| 124 | + |
| 125 | +fetcher = TavilyFetcher() |
| 126 | + |
| 127 | + |
| 128 | +async def fetch(): |
| 129 | + result = await fetcher.run_async( |
| 130 | + urls=["https://docs.haystack.deepset.ai/docs/intro"], |
| 131 | + ) |
| 132 | + return result["documents"] |
| 133 | + |
| 134 | + |
| 135 | +documents = asyncio.run(fetch()) |
| 136 | +``` |
| 137 | + |
| 138 | +The underlying clients are created lazily on the first call. To avoid the cold-start latency of the first call, you can call `warm_up()` explicitly. |
0 commit comments