{"record":{"id":"01cacab46a8b5d79","repo":"Mintplex-Labs/anything-llm","slug":"no-gemini-api-key-was-set","errorCode":null,"errorMessage":"No Gemini API key was set.","messagePattern":"No Gemini API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/gemini/index.js","lineNumber":30,"sourceCode":"const { defaultGeminiModels, v1BetaModels } = require(\"./defaultModels\");\nconst { safeJsonParse } = require(\"../../http\");\nconst cacheFolder = path.resolve(\n  process.env.STORAGE_DIR\n    ? path.resolve(process.env.STORAGE_DIR, \"models\", \"gemini\")\n    : path.resolve(__dirname, `../../../storage/models/gemini`)\n);\n\nconst NO_SYSTEM_PROMPT_MODELS = [\n  \"gemma-3-1b-it\",\n  \"gemma-3-4b-it\",\n  \"gemma-3-12b-it\",\n  \"gemma-3-27b-it\",\n];\n\nclass GeminiLLM {\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.GEMINI_API_KEY)\n      throw new Error(\"No Gemini API key was set.\");\n\n    this.className = \"GeminiLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n    this.model =\n      modelPreference ||\n      process.env.GEMINI_LLM_MODEL_PREF ||\n      \"gemini-2.0-flash-lite\";\n\n    const isExperimental = this.isExperimentalModel(this.model);\n    this.openai = new OpenAIApi({\n      apiKey: process.env.GEMINI_API_KEY,\n      // Even models that are v1 in gemini API can be used with v1beta/openai/ endpoint and nobody knows why.\n      baseURL: \"https://generativelanguage.googleapis.com/v1beta/openai/\",\n    });\n\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/utils/AiProviders/gemini/index.js#L12-L48","documentation":"The GeminiLLM constructor throws when GEMINI_API_KEY is not present in the environment. Gemini is accessed here through its OpenAI-compatible endpoint with the SDK key set to that env var, so without it no request can be authorized. Like the other provider constructors, it runs when a request selects Gemini, so the error surfaces as a chat/embedding failure rather than a boot failure.","triggerScenarios":"Selecting Gemini as the LLM provider while GEMINI_API_KEY is unset: fresh .env, var added after server start, or container/deployment env that never received it.","commonSituations":"Using a Google AI Studio key but naming the variable GOOGLE_API_KEY or GEMINI_API_TOKEN instead of GEMINI_API_KEY; Docker deploy missing the env; key defined in a different shell than the one that launched the server.","solutions":["Create a key at aistudio.google.com and add GEMINI_API_KEY=<key> to .env, then restart","Verify the exact name GEMINI_API_KEY (not GOOGLE_API_KEY)","For Docker, pass the var via env_file/environment and recreate the container","Sanity check: node -e \"console.log(!!process.env.GEMINI_API_KEY)\" in the server's environment"],"exampleFix":"# before (.env)\n# missing\n\n# after (.env)\nGEMINI_API_KEY=AIza...","handlingStrategy":"validation","validationCode":"if (!process.env.GEMINI_API_KEY) {\n  return setupError(\"Set GEMINI_API_KEY (from aistudio.google.com) to use Gemini.\");\n}\nconst llm = new GeminiLLM(embedder, modelPref);","typeGuard":null,"tryCatchPattern":"try {\n  const llm = new GeminiLLM(embedder, modelPref);\n} catch (err) {\n  if (err.message === \"No Gemini API key was set.\") {\n    return respond(\"Add GEMINI_API_KEY to your environment and restart.\");\n  }\n  throw err;\n}","preventionTips":["Audit env vars at boot; fail with a consolidated missing-vars report","Name the var exactly GEMINI_API_KEY — not GOOGLE_API_KEY","Rotate keys on a schedule so expired keys are caught by monitoring, not by users"],"tags":["gemini","missing-env-var","api-key","constructor"],"backgroundTag":"missing-env-var","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}