Skip to content

Conversation

@starryendymion
Copy link

Description

This PR introduces a Startup Environment Validator to the backend. Previously, the application would start even if critical environment variables were missing, leading to runtime crashes when the AI agent or vector search was invoked. This change ensures the application "fails fast" with a clear error message if the environment is not correctly configured.

The Issue: Silent Configuration Failures

The backend relies on several sensitive and infrastructure-specific variables (e.g., GOOGLE_API_KEY, GCP_PROJECT_ID). Without validation:

  • The server starts in an unstable state.
  • Developers only discover missing variables through 500 errors during active chat sessions.
  • Debugging is difficult for new contributors who may miss a step in the .env.template setup.

The Fix: Pydantic-Driven Validation

I implemented a central configuration schema using pydantic-settings.

  • Mandatory Checks: The app will now exit immediately if critical keys are missing.
  • Explicit Error Messages: Instead of generic stack traces, the console will explicitly list which environment variable is missing or invalid.
  • Centralized Configuration: All environment variables are now managed through a single Settings object, improving code maintainability.

Changes Made

  • backend/config.py: Created a new utility using BaseSettings to define and validate the required environment schema (LLM keys, GCP project details, and Vector Search IDs).
  • backend/main.py: Integrated the validate_config() call at the entry point of the application to intercept the startup process if the environment is invalid.

Verification

  1. Missing Variable Test: Removed GOOGLE_API_KEY from .env. The app successfully exited with a ValidationError listing the missing key.
  2. Valid Startup Test: With all variables present, the app logged ✅ Environment variables validated successfully and initialized the FastAPI server normally.

@starryendymion starryendymion changed the title feat: add startup environment validator to ensure critical secrets ar… feat: add startup environment validator to ensure critical secrets are present Jan 30, 2026
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.

1 participant