assafelovic/gpt-researcher · error · Exception

Bing API key not found. Please set the BING_API_KEY environm

Error message

Bing API key not found. Please set the BING_API_KEY environment variable.

What it means

Exception raised by the Bing retriever when the BING_API_KEY environment variable is missing (os.environ lookup fails and the except re-raises with this message). The retriever cannot authenticate to the Bing Search API without it.

Source

Thrown at gpt_researcher/retrievers/bing/bing.py:35

        Initializes the BingSearch object
        Args:
            query:
        """
        self.query = query
        self.query_domains = query_domains or None
        self.api_key = self.get_api_key()
        self.logger = logging.getLogger(__name__)

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

        """
        try:
            api_key = os.environ["BING_API_KEY"]
        except Exception:
            raise Exception(
                "Bing API key not found. Please set the BING_API_KEY environment variable.")
        return api_key

    def search(self, max_results=7) -> list[dict[str]]:
        """
        Searches the query
        Returns:

        """
        print("Searching with query {0}...".format(self.query))
        """Useful for general internet search queries using the Bing API."""

        # Search the query
        url = "https://api.bing.microsoft.com/v7.0/search"

        headers = {
            'Ocp-Apim-Subscription-Key': self.api_key,
            'Content-Type': 'application/json'

View on GitHub (pinned to 6f998577d5)

Solutions

  1. export BING_API_KEY=<your key> (get one from Azure portal / Bing Search).
  2. Add BING_API_KEY to your .env and ensure it's loaded before the retriever is constructed.
  3. In Docker/CI, pass it via -e / secrets so it exists in the container environment.
  4. Optionally validate env vars at startup before research begins.

Example fix

# before
retriever = Bing(query="...")  # raises

# after
import os
assert os.environ.get("BING_API_KEY"), "Set BING_API_KEY first"
retriever = Bing(query="...")
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.environ.get("BING_API_KEY"):
    raise SystemExit("BING_API_KEY missing — get one from Azure portal")

Type guard

null

Try / catch

try:
    retriever = Bing(query)
except Exception as e:
    if "BING_API_KEY" in str(e):
        logger.error(e); sys.exit(1)
    raise

Prevention

When it happens

Trigger: Instantiating the Bing retriever (which calls get_api_key() from __init__) in a process where BING_API_KEY is unset or empty-not-present in os.environ.

Common situations: Forgot to export the var, .env file not loaded, key set in a different shell/CI environment, or Docker/Kubernetes secrets not wired to the container.

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/e09166d2adf4075e. Report an issue: GitHub.