assafelovic/gpt-researcher · error · Exception

SearchApi key not found. Please set the SEARCHAPI_API_KEY en

Error message

SearchApi key not found. Please set the SEARCHAPI_API_KEY environment variable. You can get a key at https://www.searchapi.io/

What it means

Exception raised by the SearchApi retriever when the SEARCHAPI_API_KEY environment variable is missing; the failed os.environ lookup is caught and re-raised with this message. searchapi.io requires this key for authentication.

Source

Thrown at gpt_researcher/retrievers/searchapi/searchapi.py:32

    def __init__(self, query, query_domains=None):
        """
        Initializes the SearchApiSearch object
        Args:
            query:
        """
        self.query = query
        self.api_key = self.get_api_key()

    def get_api_key(self):
        """
        Gets the SearchApi API key
        Returns:

        """
        try:
            api_key = os.environ["SEARCHAPI_API_KEY"]
        except Exception:
            raise Exception("SearchApi key not found. Please set the SEARCHAPI_API_KEY environment variable. "
                            "You can get a key at https://www.searchapi.io/")
        return api_key

    def search(self, max_results=7):
        """
        Searches the query
        Returns:

        """
        print("SearchApiSearch: Searching with query {0}...".format(self.query))
        """Useful for general internet search queries using SearchApi."""


        url = "https://www.searchapi.io/api/v1/search"
        params = {
            "q": self.query,
            "engine": "google",
        }

View on GitHub (pinned to 6f998577d5)

Solutions

  1. export SEARCHAPI_API_KEY=<key> (get one at https://www.searchapi.io/).
  2. Add it to .env / secrets and confirm loading before retriever creation.
  3. In CI, register it as a secret environment variable.

Example fix

# before
retriever = SearchApi(query="...")

# after
import os
assert os.environ.get("SEARCHAPI_API_KEY"), "Set SEARCHAPI_API_KEY"
retriever = SearchApi(query="...")
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.environ.get("SEARCHAPI_API_KEY"):
    raise SystemExit("Set SEARCHAPI_API_KEY (https://www.searchapi.io/")

Type guard

null

Try / catch

try:
    retriever = SearchApi(query)
except Exception as e:
    if "SEARCHAPI_API_KEY" in str(e):
        sys.exit(str(e))
    raise

Prevention

When it happens

Trigger: Instantiating the SearchApi retriever (get_api_key() called from __init__) in an environment without SEARCHAPI_API_KEY.

Common situations: Key not set, .env not loaded, or the variable missing in Docker/CI deployment environments.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/ada0619ce5257147. Report an issue: GitHub.