feat: add startup environment validator to ensure critical secrets are present #49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:.env.templatesetup.The Fix: Pydantic-Driven Validation
I implemented a central configuration schema using
pydantic-settings.Settingsobject, improving code maintainability.Changes Made
backend/config.py: Created a new utility usingBaseSettingsto define and validate the required environment schema (LLM keys, GCP project details, and Vector Search IDs).backend/main.py: Integrated thevalidate_config()call at the entry point of the application to intercept the startup process if the environment is invalid.Verification
GOOGLE_API_KEYfrom.env. The app successfully exited with aValidationErrorlisting the missing key.✅ Environment variables validated successfullyand initialized the FastAPI server normally.