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
- Do not set
GEMINI_API_KEY in .env.local
- Start the dev server
- Send a POST request to
/api/ai/roast with { mode: "roast", stats: { commits: 10, languages: ["TypeScript"], mergedPRs: 3, failedGoals: 1 } }
- 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:
- Move
genAI initialization into the POST handler (lazy initialization)
- Check for
GEMINI_API_KEY before creating the client
- Return a clear
400 or 503 error like "Gemini API key is not configured. Set GEMINI_API_KEY in your environment."
- Add
GEMINI_API_KEY to .env.example with a comment explaining its purpose
Bug Description
The
/api/ai/roastroute initializes the Google Generative AI client at module level withprocess.env.GEMINI_API_KEY || ''. WhenGEMINI_API_KEYis not set in the environment, the client is created with an empty string. CallinggenAI.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 withif (process.env.GROQ_API_KEY)before making API calls, falls back gracefullysrc/app/api/ai-insights/route.ts— checksprocess.env.GROQ_API_KEYbefore proceedingsrc/lib/cv/cv-ai-generator.ts— extracts key early, returns""immediately if absentsrc/app/api/project-tutor/route.ts— returns"AI insights unavailable — GROQ_API_KEY not configured."if key missingAdditionally,
GEMINI_API_KEYis not documented in.env.example, so self-hosters and new contributors have no way to know this variable exists.Steps to Reproduce
GEMINI_API_KEYin.env.local/api/ai/roastwith{ mode: "roast", stats: { commits: 10, languages: ["TypeScript"], mergedPRs: 3, failedGoals: 1 } }Expected behavior
The route should fail gracefully with a clear error message like
"Gemini API key is not configured"whenGEMINI_API_KEYis 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. WhengenerateContent()is called, Google's API returns a cryptic error that bubbles up as a 500 withFailed 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: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:
genAIinitialization into thePOSThandler (lazy initialization)GEMINI_API_KEYbefore creating the client400or503error like"Gemini API key is not configured. Set GEMINI_API_KEY in your environment."GEMINI_API_KEYto.env.examplewith a comment explaining its purpose