openclaw - 💡(How to fix) Fix Gemini embedding provider: API key not loaded from .env (400 API_KEY_INVALID) [3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#51541Fetched 2026-04-08 01:09:50
View on GitHub
Comments
3
Participants
3
Timeline
4
Reactions
0
Timeline (top)
commented ×3subscribed ×1

When configuring agents.defaults.memorySearch.provider: "gemini", the Gemini embedding provider fails with:

gemini embeddings failed: 400 {"error":{"code":400,"message":"API Key not found. Please pass a valid API key.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID"}]}}

Error Message

gemini embeddings failed: 400 {"error":{"code":400,"message":"API Key not found. Please pass a valid API key.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID"}]}} 5. ❌ None of these work — same 400 error

Root Cause

When configuring agents.defaults.memorySearch.provider: "gemini", the Gemini embedding provider fails with:

gemini embeddings failed: 400 {"error":{"code":400,"message":"API Key not found. Please pass a valid API key.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID"}]}}

Fix Action

Workaround

Using ollama provider with nomic-embed-text model (works but slower on CPU-only VPS).

Code Example

gemini embeddings failed: 400 {"error":{"code":400,"message":"API Key not found. Please pass a valid API key.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID"}]}}

---

curl -s "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"model":"models/gemini-embedding-001","content":{"parts":[{"text":"test"}]}}'
# Returns 3072-dim embedding successfully

---

// ~/.openclaw/openclaw.json
{
  "agents": {
    "defaults": {
      "memorySearch": {
        "provider": "gemini",
        "fallback": "ollama"
      }
    }
  },
  "auth": {
    "profiles": {
      "google:default": {
        "provider": "google",
        "mode": "api_key"
      }
    }
  }
}

---

# ~/.openclaw/.env
GEMINI_API_KEY=<valid key starting with AQ.>
GOOGLE_API_KEY=<same key>
RAW_BUFFERClick to expand / collapse

Environment

  • OpenClaw version: 2026.3.13 (61d171a)
  • OS: Ubuntu 24.04 LTS
  • Node: v25.8.1
  • Gateway: systemd user service

Description

When configuring agents.defaults.memorySearch.provider: "gemini", the Gemini embedding provider fails with:

gemini embeddings failed: 400 {"error":{"code":400,"message":"API Key not found. Please pass a valid API key.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID"}]}}

The API key works fine

The same key works perfectly with curl:

curl -s "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"model":"models/gemini-embedding-001","content":{"parts":[{"text":"test"}]}}'
# Returns 3072-dim embedding successfully

Configuration

// ~/.openclaw/openclaw.json
{
  "agents": {
    "defaults": {
      "memorySearch": {
        "provider": "gemini",
        "fallback": "ollama"
      }
    }
  },
  "auth": {
    "profiles": {
      "google:default": {
        "provider": "google",
        "mode": "api_key"
      }
    }
  }
}
# ~/.openclaw/.env
GEMINI_API_KEY=<valid key starting with AQ.>
GOOGLE_API_KEY=<same key>

What I've tried

  1. ✅ Key in .env as GEMINI_API_KEY (code uses GEMINI prefix for google provider via PROVIDER_PREFIX_OVERRIDES)
  2. ✅ Added GOOGLE_API_KEY as well (fallback var)
  3. ✅ Added EnvironmentFile=/home/user/.openclaw/.env as systemd drop-in
  4. ✅ Passed key as explicit env var on CLI: GEMINI_API_KEY=... openclaw memory index --force
  5. ❌ None of these work — same 400 error

Code trace

Looking at the source, the embedding provider calls collectProviderApiKeys('google') which should resolve to GEMINI_API_KEY via PROVIDER_PREFIX_OVERRIDES. But process.env in the gateway process doesn't contain the key (verified via /proc/PID/environ).

loadDotEnv() is called and reads ~/.openclaw/.env, but the embedding provider apparently doesn't see the loaded vars.

Expected behavior

Gemini embedding provider should use GEMINI_API_KEY from .env (or GOOGLE_API_KEY as fallback).

Workaround

Using ollama provider with nomic-embed-text model (works but slower on CPU-only VPS).

extent analysis

Fix Plan

To resolve the issue, we need to ensure that the GEMINI_API_KEY environment variable is properly loaded and accessible to the Gemini embedding provider.

Here are the steps:

  • Verify that the loadDotEnv() function is correctly loading the environment variables from ~/.openclaw/.env.
  • Check if the GEMINI_API_KEY variable is being overridden or unset somewhere in the code.
  • Ensure that the PROVIDER_PREFIX_OVERRIDES configuration is correctly set up to map the google provider to the GEMINI_API_KEY environment variable.

Example code to verify the environment variables:

console.log(process.env.GEMINI_API_KEY);
console.log(process.env.GOOGLE_API_KEY);

If the variables are not being loaded, you can try loading them manually using the dotenv package:

const dotenv = require('dotenv');
dotenv.config({ path: '~/.openclaw/.env' });

Alternatively, you can try setting the environment variables directly in the systemd service file:

Environment=GEMINI_API_KEY=<valid key starting with AQ.>
Environment=GOOGLE_API_KEY=<same key>

Verification

To verify that the fix worked, you can try running the Gemini embedding provider again and check if it uses the correct API key. You can also add logging statements to the code to verify that the environment variables are being loaded correctly.

Example code to verify the API key:

const apiKey = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
console.log(`Using API key: ${apiKey}`);

Extra Tips

  • Make sure to restart the systemd service after updating the environment variables or configuration files.
  • Verify that the ~/.openclaw/.env file has the correct permissions and ownership.
  • Consider adding error handling to the code to handle cases where the API key is not found or invalid.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Gemini embedding provider should use GEMINI_API_KEY from .env (or GOOGLE_API_KEY as fallback).

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING