Skip to content

fix: guard /api/ai/roast against missing GEMINI_API_KEY - #3357

Open
vipul674 wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
vipul674:fix/3109-gemini-key-guard
Open

fix: guard /api/ai/roast against missing GEMINI_API_KEY#3357
vipul674 wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
vipul674:fix/3109-gemini-key-guard

Conversation

@vipul674

@vipul674 vipul674 commented Aug 3, 2026

Copy link
Copy Markdown

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

  • 🐛 Bug fix (non-breaking change that fixes an issue)

What Changed

  • Guard in src/app/api/ai/roast/route.ts returning 500 with a clear message when GEMINI_API_KEY is missing
  • GEMINI_API_KEY documented in .env.example

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
Copilot AI review requested due to automatic review settings August 3, 2026 20:31
@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix labels Aug 3, 2026
@github-actions github-actions Bot added type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts) labels Aug 3, 2026
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_KEY guard in src/app/api/ai/roast/route.ts to return a clear JSON error response.
  • Documents GEMINI_API_KEY in .env.example with usage context for the /api/ai/roast route.
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.

Comment on lines +4 to +7
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);
@vipul674

vipul674 commented Aug 3, 2026

Copy link
Copy Markdown
Author

👋 @Priyanshu-byte-coder — PR is ready for review. Please take a look when you get a chance. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants