{"record":{"id":"f30b7a3a393fb692","repo":"assafelovic/gpt-researcher","slug":"tavily-api-key-not-found-please-set-the-tavily-ap","errorCode":null,"errorMessage":"Tavily API key not found. Please set the TAVILY_API_KEY environment variable.","messagePattern":"Tavily API key not found\\. Please set the TAVILY_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"gpt_researcher/scraper/tavily_extract/tavily_extract.py","lineNumber":22,"sourceCode":"\nclass TavilyExtract:\n\n    def __init__(self, link, session=None):\n        self.link = link\n        self.session = session\n        from tavily import TavilyClient\n        self.tavily_client = TavilyClient(api_key=self.get_api_key())\n\n    def get_api_key(self) -> str:\n        \"\"\"\n        Gets the Tavily API key\n        Returns:\n        Api key (str)\n        \"\"\"\n        try:\n            api_key = os.environ[\"TAVILY_API_KEY\"]\n        except KeyError:\n            raise Exception(\n                \"Tavily API key not found. Please set the TAVILY_API_KEY environment variable.\")\n        return api_key\n\n    def scrape(self) -> tuple:\n        \"\"\"\n        This function extracts content from a specified link using the Tavily Python SDK, the title and\n        images from the link are extracted using the functions from `gpt_researcher/scraper/utils.py`.\n\n        Returns:\n          The `scrape` method returns a tuple containing the extracted content, a list of image URLs, and\n        the title of the webpage specified by the `self.link` attribute. It uses the Tavily Python SDK to\n        extract and clean content from the webpage. If any exception occurs during the process, an error\n        message is printed and an empty result is returned.\n        \"\"\"\n\n        try:\n            response = self.tavily_client.extract(urls=self.link)\n            if not isinstance(response, dict):","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/scraper/tavily_extract/tavily_extract.py#L4-L40","documentation":"TavilyExtract.get_api_key raises a generic Exception when the TAVILY_API_KEY environment variable is not set. The Tavily extraction scraper requires this key at construction time, before any scraping happens.","triggerScenarios":"Constructing TavilyExtract (or selecting 'tavily_extract' / 'tavily' as the scraper) without TAVILY_API_KEY defined in os.environ — including .env files that were never loaded because python-dotenv's load_dotenv() wasn't called.","commonSituations":"Missing/duplicate .env file, deploying to Docker/Cloud Run where env vars aren't forwarded, CI pipelines lacking secrets, or a typo in the variable name (e.g. TAVILYKEY).","solutions":["export TAVILY_API_KEY=<key> (get one at tavily.com)","Add TAVILY_API_KEY=... to your .env and ensure load_dotenv() runs before scraper construction","In Docker: pass it with -e TAVILY_API_KEY=... or in the compose environment block","Or switch to a scraper that doesn't need an API key, like 'beautifulsoup' or 'pymupdf'"],"exampleFix":"# before\nscraper = Scraper('tavily_extract')  # Exception: Tavily API key not found...\n\n# after\nimport os\nos.environ.setdefault('TAVILY_API_KEY', 'tvly-xxxx')\nscraper = Scraper('tavily_extract')","handlingStrategy":"validation","validationCode":"import os\n\nif scraper_choice == 'tavily_extract' and not os.getenv('TAVILY_API_KEY'):\n    raise SystemExit('Set TAVILY_API_KEY or use a non-API scraper')","typeGuard":null,"tryCatchPattern":"try:\n    result = Scraper('tavily_extract').scrape_with_url(url)\nexcept Exception as e:\n    if 'TAVILY_API key' in str(e) or 'Tavily' in str(e):\n        result = Scraper('beautifulsoup').scrape_with_url(url)\n    else:\n        raise","preventionTips":["Set required API keys in .env and call load_dotenv() early","Use a startup env-var checklist that fails fast on missing keys","Keep a keyless fallback scraper configured for resilience"],"tags":["missing-env-var","tavily","api-key","authentication"],"backgroundTag":"missing-api-key-env-var","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}