aider icon indicating copy to clipboard operation
aider copied to clipboard

Keep getting auth error with gemini... even though I have the Gemini API key env variable

Open spott opened this issue 8 months ago • 3 comments

Issue

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "Explain how AI works"}]
    }]
   }'

works.

However, aider --model=gemini/gemini-2.5-pro-exp-03-25 spits out:

litellm.AuthenticationError: geminiException - {
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "generativelanguage.googleapis.com"
        }
      },
      {
        "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
        "locale": "en-US",
        "message": "API key not valid. Please pass a valid API key."
      }
    ]
  }
}

The API provider is not able to authenticate you. Check your API key.
litellm.AuthenticationError: geminiException - {
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "generativelanguage.googleapis.com"
        }
      },
      {
        "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
        "locale": "en-US",
        "message": "API key not valid. Please pass a valid API key."
      }
    ]
  }
}

No idea why... not sure how to debug either.

Version and model info

aider 0.82.0 model: gemini/gemini-2.5-pro-exp-03-25

spott avatar Apr 15 '25 22:04 spott

Have you tried using aider to debug your curl request?

neo-picasso-2112 avatar Apr 16 '25 03:04 neo-picasso-2112

  • 1, having the same issue with Gemini (the most recent version of Aider, on a Mac + zsh) (env vars with API keys are set up properly)
Aider v0.82.0
Main model: gemini/gemini-2.5-pro-preview-03-25 with diff-fenced edit format
Weak model: gemini/gemini-1.5-flash

Have you tried using aider to debug your curl request?

bot spam?

paprikka avatar Apr 16 '25 08:04 paprikka

Figured it out.

Aider silently loads a .env file if it finds one, which in my case contains a bunch of env variables with things like "op://vault/Anthropic API Key/credential"... because I use direnv and one password to load them.

I've added a pull request: #3888 to make the loaded environment supercede the values in the .env.

spott avatar Apr 25 '25 17:04 spott