fix(ai): defer env validation to request time to prevent import crash (#263)#311
fix(ai): defer env validation to request time to prevent import crash (#263)#311Shevilll wants to merge 2 commits into
Conversation
|
Warning Review limit reached
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 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
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! |
Fixes #263
What
Backend/app/routes/ai.pyread its environment variables, ran anif not all([...]): raise ValueError(...)check, and calledcreate_client(...)at module import time. Becauseapp/main.pyimports this module on startup (from app.routes import ai), a single missing env var (SUPABASE_URL,SUPABASE_KEY, orGEMINI_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 byget_youtube_channel_infoin 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.pypasses.create_clientis not invoked at import time.trending_niches()and the Gemini path return a clean503with a descriptive message when their config is missing.Scope
Single-file, minimal change to
Backend/app/routes/ai.py. No unrelated refactors.