nikivdev/code · error

Gemini API error {}: {}

Error message

Gemini API error {}: {}

What it means

After sending a request to the Gemini generateContent API via reqwest, the code checks `resp.status().is_success()`. On any non-2xx response it bails with the HTTP status and the raw response body text. This surfaces server-side rejections: invalid keys, quota limits, malformed requests, or model availability problems.

Source

Thrown at src/ai.rs:14628

            }
        ],
        "generationConfig": {
            "temperature": 0.2,
            "maxOutputTokens": 700,
            "responseMimeType": "application/json"
        }
    });

    let resp = client
        .post(&url)
        .json(&payload)
        .send()
        .context("failed to call Gemini API")?;

    if !resp.status().is_success() {
        let status = resp.status();
        let text = resp.text().unwrap_or_default();
        bail!("Gemini API error {}: {}", status, text);
    }

    let parsed: GeminiResponse = resp.json().context("failed to parse Gemini response")?;
    let content = parsed
        .candidates
        .get(0)
        .and_then(|c| c.content.parts.get(0))
        .and_then(|p| p.text.as_deref())
        .unwrap_or("")
        .trim();

    if content.is_empty() {
        bail!("Gemini returned empty summary");
    }

    let summary_payload = parse_summary_response(content)?;

    Ok(SessionSummary {

View on GitHub (pinned to a747e741ae)

Solutions

  1. Read the status and body in the message: 400 → fix request payload; 401/403 → fix/rotate API key and enable the Generative Language API for the project; 429 → back off and retry after quota window; 5xx → retry with backoff
  2. Verify the key with curl against `https://generativelanguage.googleapis.com/v1beta/models?key=$GEMINI_API_KEY`
  3. Check quota/limits in Google AI Studio / Cloud Console and enable billing if needed
  4. Confirm the model name matches an available Gemini model and trim oversized context before sending

Example fix

// before
$ myapp ai summarize gemini
Error: Gemini API error 429 Too Many Requests: {"error":{"code":429,...RESOURCE_EXHAUSTED...}}
// after
$ sleep 60 && myapp ai summarize gemini   # after quota window; or rotate key for 401/403
Defensive patterns

Strategy: retry

Validate before calling

fn preflight_gemini(key: &str) -> Result<(), String> {
    let url = format!("https://generativelanguage.googleapis.com/v1beta/models?key={key}");
    match reqwest::blocking::get(&url) {
        Ok(r) if r.status().is_success() => Ok(()),
        Ok(r) => Err(format!("preflight failed: {}", r.status())),
        Err(e) => Err(format!("network error: {e}")),
    }
}
preflight_gemini(&key)?;

Try / catch

match summarize_with_gemini(ctx) {
    Err(e) if e.to_string().starts_with("Gemini API error 429") => {
        std::thread::sleep(Duration::from_secs(60));
        retry_with_backoff(3, || summarize_with_gemini(ctx))?;
    }
    Err(e) if e.to_string().starts_with("Gemini API error 5") => {
        retry_with_backoff(3, || summarize_with_gemini(ctx))?;
    }
    Err(e) if e.to_string().starts_with("Gemini API error 40") => {
        eprintln!("{e}; check API key and enabled APIs in Google AI Studio");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling the Gemini summarize API with an invalid/expired/revoked API key (401/403); exceeding quota or rate limits (429); a request body that fails validation (400); requesting a model name that doesn't exist or isn't enabled for the key (404); transient Google-side 5xx errors.

Common situations: Key created without Generative Language API enabled; key restricted to different APIs/referrers; large prompts exceeding token limits; shared key hitting project quota during team use; network proxies injecting error pages.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/83155b6fb0e38864. Report an issue: GitHub.