rohitg00/agentmemory · error · Error

GEMINI_API_KEY (or GOOGLE_API_KEY) is required for the gemin

Error message

GEMINI_API_KEY (or GOOGLE_API_KEY) is required for the gemini provider

What it means

When the configured chat provider type is 'gemini', agentmemory routes it through OpenRouterProvider and requires a Gemini/Google key from GEMINI_API_KEY or GOOGLE_API_KEY. If neither is set the factory throws this message before constructing the provider.

Source

Thrown at src/providers/index.ts:112

  switch (config.provider) {
    case "minimax":
      return new MinimaxProvider(
        requireEnvVar("MINIMAX_API_KEY"),
        config.model,
        config.maxTokens,
      );
    case "anthropic":
      return new AnthropicProvider(
        requireEnvVar("ANTHROPIC_API_KEY"),
        config.model,
        config.maxTokens,
        config.baseURL,
      );
    case "gemini": {
      const geminiKey =
        getEnvVar("GEMINI_API_KEY") || getEnvVar("GOOGLE_API_KEY");
      if (!geminiKey) {
        throw new Error(
          "GEMINI_API_KEY (or GOOGLE_API_KEY) is required for the gemini provider",
        );
      }
      return new OpenRouterProvider(
        geminiKey,
        config.model,
        config.maxTokens,
        "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions",
      );
    }
    case "openrouter":
      return new OpenRouterProvider(
        requireEnvVar("OPENROUTER_API_KEY"),
        config.model,
        config.maxTokens,
        "https://openrouter.ai/api/v1/chat/completions",
      );
    case "openai": {

View on GitHub (pinned to e04ba88819)

Solutions

  1. Get a key from Google AI Studio and set GEMINI_API_KEY (or GOOGLE_API_KEY) in ~/.agentmemory/.env or the environment
  2. Confirm exact variable names GEMINI_API_KEY / GOOGLE_API_KEY (not GEMINI_KEY or GOOGLE_AI_API_KEY)
  3. Pass the key in config/explicitly where the factory supports it
  4. Or switch config.type to a provider whose credentials you already have (e.g. 'openai')

Example fix

// before
// config: { type: 'gemini', model: 'gemini-2.0-flash' } — no key set
const provider = await createProvider(config); // throws
// after
// ~/.agentmemory/.env: GEMINI_API_KEY=AIza...
const provider = await createProvider(config);
Defensive patterns

Strategy: validation

Validate before calling

function assertGeminiKey() {
  const key = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
  if (!key) throw new Error('Set GEMINI_API_KEY (or GOOGLE_API_KEY) for the gemini provider');
  return key;
}
assertGeminiKey(); // before createProvider({ type: 'gemini', ... })

Type guard

const hasGeminiKey = (c: { type: string }): c is { type: 'gemini' } & { keyResolvable: true } =>
  c.type !== 'gemini' || !!(process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY);

Try / catch

try {
  provider = await createProvider({ type: 'gemini', model });
} catch (err) {
  if (err instanceof Error && err.message.startsWith('GEMINI_API_KEY')) {
    console.error('Get a Google AI Studio key and set GEMINI_API_KEY in ~/.agentmemory/.env');
    process.exit(1);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling createProvider/createBaseProvider (or createFallbackProvider) with config.type === 'gemini' while both GEMINI_API_KEY and GOOGLE_API_KEY are unset in env and ~/.agentmemory/.env.

Common situations: Users assuming the gemini provider talks directly to Google and setting only OPENAI-style vars; config generated by a template that omits the key; key set under GEMINI_KEY (wrong name); secrets synced to a different environment than the daemon's; Google AI Studio key created but never exported.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/943f33eddac4597d. Report an issue: GitHub.