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.searchEngineIdView on GitHub (pinned to 5ff282f900)
Solutions
- Create a Programmable Search Engine at google.com/cse, grab its ID, set `GOOGLE_CSE_ID=<id>`.
- Enable Custom Search API in Google Cloud and create an API key, then set `GOOGLE_SEARCH_API_KEY=<key>`.
- Restart the API process so it picks up the new env vars.
- For manifest-only loading, pass `override: true` to skip the check.
- 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
- Use exact names GOOGLE_SEARCH_API_KEY and GOOGLE_CSE_ID.
- Validate both at startup.
- Pass override:true when registering the tool only for the catalog.
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
- Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_IN
- Missing DALLE_API_KEY environment variable.
- Missing FLUX_API_KEY environment variable.
- Missing IMAGE_GEN_OAI_API_KEY environment variable.
- Gemini Image Generation requires one of: user-provided API k
AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12).
Data as JSON: /api/errors/e488411d4f21be30.
Report an issue: GitHub.