fix: guard /api/ai/roast against missing GEMINI_API_KEY - #3357
Conversation
The route created the GoogleGenerativeAI client at module level with an empty key, so calls threw a cryptic Google SDK error as a 500. Return a clear 'Gemini API key is not configured' error when the key is absent, matching the guard pattern used by project-tutor and cv-ai-generator. Also document GEMINI_API_KEY in .env.example. Closes Priyanshu-byte-coder#3109
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
There was a problem hiding this comment.
🟡 Not ready to approve
The Gemini client is still constructed at module load time with an empty-string key, which can fail before the request-level guard executes and should be refactored to lazy initialization after validating GEMINI_API_KEY.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR aims to make the /api/ai/roast endpoint fail gracefully when GEMINI_API_KEY is not set, instead of surfacing a cryptic upstream Google SDK error, and documents the required env var for contributors/self-hosters.
Changes:
- Adds a missing-
GEMINI_API_KEYguard insrc/app/api/ai/roast/route.tsto return a clear JSON error response. - Documents
GEMINI_API_KEYin.env.examplewith usage context for the/api/ai/roastroute.
File summaries
| File | Description |
|---|---|
| src/app/api/ai/roast/route.ts | Adds an env-var guard to prevent calling Gemini without credentials and returns a clearer error response. |
| .env.example | Adds documentation for GEMINI_API_KEY so the roast route can be configured correctly. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
| const GEMINI_API_KEY = process.env.GEMINI_API_KEY || ''; | ||
|
|
||
| // Initialize the Google Generative AI SDK | ||
| const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || ''); | ||
| const genAI = new GoogleGenerativeAI(GEMINI_API_KEY); |
|
👋 @Priyanshu-byte-coder — PR is ready for review. Please take a look when you get a chance. Thanks! |
Summary
The /api/ai/roast route created the GoogleGenerativeAI client at module level with an empty key, so calls threw a cryptic Google SDK error as a 500. Guard the key and return a clear error, and document GEMINI_API_KEY in .env.example.
Closes #3109
Type of Change
What Changed