Critical severity beginner · Fix: 2-5 min

ValueError

builtins.ValueError: API key not set in genai.configure()

What this error means
Gemini SDK genai.configure() was called without an API key, or GOOGLE_API_KEY environment variable is not set or accessible.

Stack trace

traceback
Traceback (most recent call last):
  File "your_script.py", line 12, in <module>
    genai.configure(api_key=api_key)
  File "/usr/local/lib/python3.11/site-packages/google/generativeai/client.py", line 45, in configure
    raise ValueError(
      'API key not provided. Pass the `api_key` argument, '
      'or set the `GOOGLE_API_KEY` environment variable.'
    )
ValueError: API key not provided. Pass the `api_key` argument, or set the `GOOGLE_API_KEY` environment variable.
QUICK FIX
Add `import os; from dotenv import load_dotenv; load_dotenv(); genai.configure(api_key=os.environ.get('GOOGLE_API_KEY'))` before calling genai.GenerativeModel().

Why it happens

The Gemini SDK requires authentication via API key before making any requests to Google's generative AI models. If you don't pass api_key to genai.configure() or set the GOOGLE_API_KEY environment variable, the SDK cannot authenticate your requests and raises this error immediately. This is a safety mechanism: the SDK refuses to proceed without valid credentials.

Detection

Check that GOOGLE_API_KEY is exported in your shell before running Python (echo $GOOGLE_API_KEY), and verify it starts with 'AIza...' (the correct prefix for Google API keys). Add print statements before genai.configure() to log the api_key value (masked).

Causes & fixes

1

GOOGLE_API_KEY environment variable is not set in your shell or deployment environment

✓ Fix

Export GOOGLE_API_KEY before running your script: `export GOOGLE_API_KEY='your_key_here'` in bash, or add it to .env and load with python-dotenv: `from dotenv import load_dotenv; load_dotenv()`

2

API key is set but genai.configure() is called without passing it, and the environment variable is not being read

✓ Fix

Explicitly pass the key to genai.configure(): `genai.configure(api_key=os.environ.get('GOOGLE_API_KEY'))`

3

API key contains special characters or spaces that break when passed as string literal

✓ Fix

Never hardcode keys in source code. Always use os.environ.get('GOOGLE_API_KEY') to read from environment, which handles special characters safely

4

Running in a containerized or serverless environment (Docker, Lambda, Cloud Functions) where environment variables are not inherited from your local shell

✓ Fix

Explicitly inject GOOGLE_API_KEY into the container/function via docker run -e GOOGLE_API_KEY=... or via platform secrets (Lambda env vars, Cloud Functions secret manager)

Code: broken vs fixed

Broken - triggers the error
python
import google.generativeai as genai

# BROKEN: No API key passed, GOOGLE_API_KEY not in environment
genai.configure()  # ← This line raises ValueError

model = genai.GenerativeModel('gemini-2.0-flash')
response = model.generate_content('What is AI?')
print(response.text)
Fixed - works correctly
python
import os
import google.generativeai as genai
from dotenv import load_dotenv

# FIXED: Load environment variables from .env file (optional, if using .env)
load_dotenv()

# Get API key from environment variable (required)
api_key = os.environ.get('GOOGLE_API_KEY')
if not api_key:
    raise ValueError('GOOGLE_API_KEY environment variable is not set')

# FIXED: Pass API key explicitly to genai.configure()
genai.configure(api_key=api_key)

model = genai.GenerativeModel('gemini-2.0-flash')
response = model.generate_content('What is AI?')
print(response.text)
Added explicit api_key retrieval from os.environ and passed it to genai.configure() with error handling to validate the key exists before proceeding.
⚠

Workaround

If you cannot set environment variables in your current environment, pass the API key as a hardcoded string directly to genai.configure(api_key='your_actual_key_here') for immediate testing (never use in production). For production, always use environment variables or a secrets manager (Google Cloud Secret Manager, AWS Secrets Manager, HashiCorp Vault) to inject keys safely at runtime.

✓

Prevention

Store all API keys in environment variables or a secrets management system, never in source code or version control. Use .env files locally (with python-dotenv) and platform-specific secrets injection in production (container orchestration, serverless platforms, CI/CD). Validate that GOOGLE_API_KEY is present at application startup with clear error messages, not buried in a 500ms SDK error later.

Python 3.9+ · google-generativeai >=0.7.0 · tested on 0.7.x, 0.8.x
Verified 2026-04 · gemini-2.0-flash, gemini-1.5-pro
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.