Skip to content

[BUG] ai/roast route crashes when GEMINI_API_KEY is not configured — missing graceful fallback #3109

Description

@vipul674

Bug Description

The /api/ai/roast route initializes the Google Generative AI client at module level with process.env.GEMINI_API_KEY || ''. When GEMINI_API_KEY is not set in the environment, the client is created with an empty string. Calling genAI.getGenerativeModel(...) with this empty key throws a confusing runtime error from Google's SDK instead of a graceful, user-friendly message.

This is inconsistent with every other AI route in the codebase:

  • src/app/api/personality/route.ts — guards with if (process.env.GROQ_API_KEY) before making API calls, falls back gracefully
  • src/app/api/ai-insights/route.ts — checks process.env.GROQ_API_KEY before proceeding
  • src/lib/cv/cv-ai-generator.ts — extracts key early, returns "" immediately if absent
  • src/app/api/project-tutor/route.ts — returns "AI insights unavailable — GROQ_API_KEY not configured." if key missing

Additionally, GEMINI_API_KEY is not documented in .env.example, so self-hosters and new contributors have no way to know this variable exists.

Steps to Reproduce

  1. Do not set GEMINI_API_KEY in .env.local
  2. Start the dev server
  3. Send a POST request to /api/ai/roast with { mode: "roast", stats: { commits: 10, languages: ["TypeScript"], mergedPRs: 3, failedGoals: 1 } }
  4. Observe the 500 error response with a confusing Google SDK error instead of a clear "API key not configured" message

Expected behavior

The route should fail gracefully with a clear error message like "Gemini API key is not configured" when GEMINI_API_KEY is missing, similar to how other AI routes handle missing keys (project-tutor/route.ts, cv-ai-generator.ts).

Actual behavior

The Google SDK client is created with '' at module load time. When generateContent() is called, Google's API returns a cryptic error that bubbles up as a 500 with Failed to generate response. Please try again.

Screenshots

No response

Browser & OS

All environments

Environment

Both

Additional Context

The root cause is at src/app/api/ai/roast/route.ts:5:

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || '');

The || '' fallback masks the absence of the API key and delegates error handling to Google's SDK, which produces a confusing error message.

All other AI routes in the codebase properly guard against missing credentials — this route should follow the same pattern.

Suggested fix:

  1. Move genAI initialization into the POST handler (lazy initialization)
  2. Check for GEMINI_API_KEY before creating the client
  3. Return a clear 400 or 503 error like "Gemini API key is not configured. Set GEMINI_API_KEY in your environment."
  4. Add GEMINI_API_KEY to .env.example with a comment explaining its purpose

Metadata

Metadata

Assignees

Labels

gssoc:assignedGSSoC: Issue assigned to a contributor

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions