nikivdev/code · error
Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to
Error message
Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env)
What it means
The Gemini summarization path requires an API key and checks `GEMINI_API_KEY`/`GOOGLE_API_KEY`, including personal env lookups via `crate::env::get_personal_env_var`. If neither is set (or the value is whitespace-only), the function bails telling the user to set the env var or add it to their personal env file. No request is attempted without credentials.
Source
Thrown at src/ai.rs:14555
}
if let Ok(key) = std::env::var("GOOGLE_API_KEY") {
if !key.trim().is_empty() {
return Ok(key);
}
}
if let Ok(Some(key)) = crate::env::get_personal_env_var("GEMINI_API_KEY") {
if !key.trim().is_empty() {
return Ok(key);
}
}
if let Ok(Some(key)) = crate::env::get_personal_env_var("GOOGLE_API_KEY") {
if !key.trim().is_empty() {
return Ok(key);
}
}
bail!("Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env)")
}
fn truncate_for_summary(context: &str) -> String {
let max_chars = summary_max_chars();
if context.chars().count() <= max_chars {
return context.to_string();
}
let start = context.chars().count().saturating_sub(max_chars);
context.chars().skip(start).collect()
}
fn truncate_for_handoff(context: &str) -> String {
let max_chars = handoff_max_chars();
if context.chars().count() <= max_chars {
return context.to_string();
}
let start = context.chars().count().saturating_sub(max_chars);
context.chars().skip(start).collect()View on GitHub (pinned to a747e741ae)
Solutions
- Export the key: `export GEMINI_API_KEY=...` (or GOOGLE_API_KEY) in the shell before running the command
- Add the key to the tool's personal env file so it persists across shells/sessions
- In CI, pass the secret through as an environment variable to the process
- Verify with `echo ${GEMINI_API_KEY:+set}` that the variable is non-empty in the exact environment running the tool
Example fix
// before $ myapp ai summarize gemini Error: Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env) // after $ export GEMINI_API_KEY=AIza... $ myapp ai summarize gemini # succeeds
Defensive patterns
Strategy: validation
Validate before calling
fn ensure_gemini_key() -> Result<(), String> {
let ok = |v: &str| std::env::var(v).map(|k| !k.trim().is_empty()).unwrap_or(false);
if ok("GEMINI_API_KEY") || ok("GOOGLE_API_KEY") { Ok(()) }
else { Err("set GEMINI_API_KEY or GOOGLE_API_KEY before running".into()) }
}
ensure_gemini_key()?; Try / catch
match summarize_with_gemini(ctx) {
Err(e) if e.to_string().contains("Missing GEMINI_API_KEY") => {
eprintln!("{e}; export GEMINI_API_KEY or add it to your personal env file");
}
Err(e) => return Err(e),
Ok(s) => println!("{s}"),
} Prevention
- Add GEMINI_API_KEY to your personal env file once so every shell/session has it
- In CI, wire the secret explicitly into the job environment; never rely on rc files there
- Guard against whitespace-only values: `export GEMINI_API_KEY="$(cat key.txt | tr -d '[:space:]')"`
- Verify non-emptiness with `echo ${GEMINI_API_KEY:+set}` before launching long-running commands
When it happens
Trigger: Calling the Gemini summary command with neither GEMINI_API_KEY nor GOOGLE_API_KEY exported in the environment; key present but empty/whitespace-only; key defined only in a shell rc not loaded by the current process; personal env file missing the entry.
Common situations: Fresh checkout on a new machine without the key; CI secrets not propagated; using a shell wrapper (systemd, Docker, IDE task) that strips environment variables; typo in the variable name (e.g. GOOGLE_APIKEY).
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- OPENROUTER_API_KEY not set. Get one at https://openrouter.ai
- DATABASE_URL not found (set env, PLANETSCALE_DATABASE_URL, o
- Gemini API error {}: {}
- could not resolve commit report directory
- remote review URL not configured
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/f9e21e9ce5fc32ad.
Report an issue: GitHub.