Keep getting auth error with gemini... even though I have the Gemini API key env variable
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
Have you tried using aider to debug your curl request?
- 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?
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.