Mintplex-Labs/anything-llm · error · Error

No Gemini API key was set.

Error message

No Gemini API key was set.

What it means

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.

Solutions

  1. Create a key at aistudio.google.com and add GEMINI_API_KEY=<key> to .env, then restart
  2. Verify the exact name GEMINI_API_KEY (not GOOGLE_API_KEY)
  3. For Docker, pass the var via env_file/environment and recreate the container
  4. Sanity check: node -e "console.log(!!process.env.GEMINI_API_KEY)" in the server's environment

Example fix

# before (.env)
# missing

# after (.env)
GEMINI_API_KEY=AIza...
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.GEMINI_API_KEY) {
  return setupError("Set GEMINI_API_KEY (from aistudio.google.com) to use Gemini.");
}
const llm = new GeminiLLM(embedder, modelPref);

Try / catch

try {
  const llm = new GeminiLLM(embedder, modelPref);
} catch (err) {
  if (err.message === "No Gemini API key was set.") {
    return respond("Add GEMINI_API_KEY to your environment and restart.");
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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 Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18). Data as JSON: /api/errors/01cacab46a8b5d79. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/AiProviders/gemini/index.js:30

const { defaultGeminiModels, v1BetaModels } = require("./defaultModels");
const { safeJsonParse } = require("../../http");
const cacheFolder = path.resolve(
  process.env.STORAGE_DIR
    ? path.resolve(process.env.STORAGE_DIR, "models", "gemini")
    : path.resolve(__dirname, `../../../storage/models/gemini`)
);

const NO_SYSTEM_PROMPT_MODELS = [
  "gemma-3-1b-it",
  "gemma-3-4b-it",
  "gemma-3-12b-it",
  "gemma-3-27b-it",
];

class GeminiLLM {
  constructor(embedder = null, modelPreference = null) {
    if (!process.env.GEMINI_API_KEY)
      throw new Error("No Gemini API key was set.");

    this.className = "GeminiLLM";
    const { OpenAI: OpenAIApi } = require("openai");
    this.model =
      modelPreference ||
      process.env.GEMINI_LLM_MODEL_PREF ||
      "gemini-2.0-flash-lite";

    const isExperimental = this.isExperimentalModel(this.model);
    this.openai = new OpenAIApi({
      apiKey: process.env.GEMINI_API_KEY,
      // Even models that are v1 in gemini API can be used with v1beta/openai/ endpoint and nobody knows why.
      baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
    });

    this.limits = {
      history: this.promptWindowLimit() * 0.15,
      system: this.promptWindowLimit() * 0.15,

View on GitHub (pinned to 3aec848f28)