Skip to content

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

@Muneerali199

Description

@Muneerali199

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

📌 Issue Overview

Importing Backend/app/routes/ai.py currently raises ValueError at module import if SUPABASE_URL, SUPABASE_KEY, or GEMINI_API_KEY are unset. This aborts FastAPI startup, so the entire API is down when any of those env vars are missing—blocking unrelated routes.

🔍 Steps to Reproduce

  1. Unset SUPABASE_URL/SUPABASE_KEY/GEMINI_API_KEY in the backend environment.
  2. Start the FastAPI app (e.g., uvicorn app.main:app --reload).
  3. Observe startup failure.

🎯 Expected Behavior

The app should start; only the AI endpoints should fail gracefully per-request, returning a clear 500 explaining missing config.

🚨 Actual Behavior

FastAPI fails at import time with ValueError("Missing required environment variables: SUPABASE_URL, SUPABASE_KEY, GEMINI_API_KEY"), preventing any route from serving.

📷 Screenshot

N/A (startup crash)

💡 Suggested Improvements

  • Move env validation from module import to within the AI route handlers.
  • On missing keys, return 500 JSON with a clear message; log a startup warning instead of raising.
  • Add a lightweight check/test to cover the missing-env path.

Record

  • I agree to follow this project's Code of Conduct
  • I want to work on this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions