danny-avila/LibreChat · error · Error

Missing ${this.envVarApiKey} or ${this.envVarSearchEngineId}

Error message

Missing ${this.envVarApiKey} or ${this.envVarSearchEngineId} environment variable.

What it means

Constructor guard for the GoogleSearch (Google Custom Search) tool. It resolves apiKey from fields.GOOGLE_SEARCH_API_KEY or process.env.GOOGLE_SEARCH_API_KEY, and searchEngineId from fields.GOOGLE_CSE_ID or process.env.GOOGLE_CSE_ID. If either is falsy and override is false, construction throws.

Source

Thrown at api/app/clients/tools/structured/GoogleSearch.js:42

    return 'google';
  }

  static get jsonSchema() {
    return googleSearchJsonSchema;
  }

  constructor(fields = {}) {
    super(fields);
    this.name = 'google';
    this.envVarApiKey = 'GOOGLE_SEARCH_API_KEY';
    this.envVarSearchEngineId = 'GOOGLE_CSE_ID';
    this.override = fields.override ?? false;
    this.apiKey = fields[this.envVarApiKey] ?? getEnvironmentVariable(this.envVarApiKey);
    this.searchEngineId =
      fields[this.envVarSearchEngineId] ?? getEnvironmentVariable(this.envVarSearchEngineId);

    if (!this.override && (!this.apiKey || !this.searchEngineId)) {
      throw new Error(
        `Missing ${this.envVarApiKey} or ${this.envVarSearchEngineId} environment variable.`,
      );
    }

    this.kwargs = fields?.kwargs ?? {};
    this.name = 'google';
    this.description =
      'A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events.';

    this.schema = googleSearchJsonSchema;
  }

  async _call(input) {
    const { query, max_results = 5 } = input;

    const response = await fetch(
      `https://www.googleapis.com/customsearch/v1?key=${this.apiKey}&cx=${
        this.searchEngineId

View on GitHub (pinned to 5ff282f900)

Solutions

  1. Create a Programmable Search Engine at google.com/cse, grab its ID, set `GOOGLE_CSE_ID=<id>`.
  2. Enable Custom Search API in Google Cloud and create an API key, then set `GOOGLE_SEARCH_API_KEY=<key>`.
  3. Restart the API process so it picks up the new env vars.
  4. For manifest-only loading, pass `override: true` to skip the check.
  5. Double-check the exact variable names: GOOGLE_SEARCH_API_KEY and GOOGLE_CSE_ID (not GOOGLE_SEARCH_KEY or CSE_ID).

Example fix

// before
const t = new GoogleSearchResults({}); // throws

// after
// .env:
//   GOOGLE_SEARCH_API_KEY=AIza...
//   GOOGLE_CSE_ID=a1b2c3d4...
const t = new GoogleSearchResults({ override: true }); // or rely on env
Defensive patterns

Strategy: validation

Validate before calling

function assertGoogleSearchConfig(fields = {}) {
  const env = process.env;
  const apiKey = fields.GOOGLE_SEARCH_API_KEY || env.GOOGLE_SEARCH_API_KEY;
  const cseId = fields.GOOGLE_CSE_ID || env.GOOGLE_CSE_ID;
  if (!fields.override && (!apiKey || !cseId)) {
    throw new Error('GoogleSearch requires GOOGLE_SEARCH_API_KEY and GOOGLE_CSE_ID.');
  }
}

Type guard

function hasGoogleSearchConfig(fields) {
  const env = process.env;
  return Boolean(
    (fields?.GOOGLE_SEARCH_API_KEY || env.GOOGLE_SEARCH_API_KEY) &&
      (fields?.GOOGLE_CSE_ID || env.GOOGLE_CSE_ID),
  );
}

Prevention

When it happens

Trigger: Instantiating GoogleSearchResults (e.g., during structured-tool loading) when at least one of GOOGLE_SEARCH_API_KEY / GOOGLE_CSE_ID is absent from both the fields argument and process.env, and `fields.override` is not true.

Common situations: Enabled the Google search tool without creating a Custom Search Engine and API key in Google Cloud Console; set GOOGLE_API_KEY (wrong name) instead of GOOGLE_SEARCH_API_KEY; copied .env.example but left the CSE ID blank; deploy missing the secret injection.

Related errors


AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12). Data as JSON: /api/errors/e488411d4f21be30. Report an issue: GitHub.