Skip to content

fix(ai): defer env validation to request time to prevent import crash (#263)#311

Open
Shevilll wants to merge 2 commits into
AOSSIE-Org:mainfrom
Shevilll:fix/ai-routes-lazy-env
Open

fix(ai): defer env validation to request time to prevent import crash (#263)#311
Shevilll wants to merge 2 commits into
AOSSIE-Org:mainfrom
Shevilll:fix/ai-routes-lazy-env

Conversation

@Shevilll

Copy link
Copy Markdown

Fixes #263

What

Backend/app/routes/ai.py read its environment variables, ran an if not all([...]): raise ValueError(...) check, and called create_client(...) at module import time. Because app/main.py imports this module on startup (from app.routes import ai), a single missing env var (SUPABASE_URL, SUPABASE_KEY, or GEMINI_API_KEY) crashed the entire app import — taking down unrelated routes (post, chat, match) along with the AI endpoints.

This PR moves the env reads and client construction out of module scope into lazy, call-time helpers:

  • get_supabase_client() — lazily builds and caches the Supabase client.
  • get_gemini_api_key() — reads the Gemini key at call time.

When configuration is missing, the AI handlers now raise HTTPException(status_code=503, detail=...) with a clear message, instead of crashing import. This mirrors the convention already used by get_youtube_channel_info in the same file.

Why

Importing the AI router should never be able to take down the whole FastAPI app. With this change the app starts and all unrelated endpoints work even when AI config is absent; only the AI endpoints return a clean 503.

Verification

  • python -m py_compile Backend/app/routes/ai.py passes.
  • Importing the module with all env vars unset no longer raises, and create_client is not invoked at import time.
  • trending_niches() and the Gemini path return a clean 503 with a descriptive message when their config is missing.
  • No behavior change when the env vars are present.

Scope

Single-file, minimal change to Backend/app/routes/ai.py. No unrelated refactors.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@Shevilll, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 59 minutes and 32 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0b0e189f-c8ce-41f8-a2ae-8a55d52f2556

📥 Commits

Reviewing files that changed from the base of the PR and between a61c801 and ec954d8.

📒 Files selected for processing (1)
  • Backend/app/routes/ai.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Shevilll

Copy link
Copy Markdown
Author

Hi team! I've verified that all local checks are green and the module import crash issue is resolved. Please let me know if there's any feedback or if we are good to merge this! Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Prevent ai.py import crash when env vars are missing

1 participant