AI Bot Router Import Documentation
Overview
This document explains the conditional import logic inmain.py for the AI bot router and how to troubleshoot router import failures.
Router Import Logic
The AI bot uses a conditional import pattern inmain.py (lines 80-159):
Why This Pattern Exists
- Graceful Degradation: If the router import fails, the service can still start without crashing
- Explicit Failure: Without a fallback endpoint, import failures are immediately apparent
- Debugging: Clear logging shows whether the router loaded successfully
Common Import Failure Causes
1. Missing Dependencies
- PyJWT: Required by
api/auth.pyfor JWT token handling - Vertex AI: Required for Gemini model integration
- BigQuery: Required for data queries
requirements.txt:
2. Import Path Issues
The router uses fallback imports for Cloud Run environment:sys.path.insert(0, '/app') is used in fallback imports.
3. Environment Variables
Missing environment variables cause silent failures:GCP_PROJECT_ID(should be “payroll-bi-gauntlet”)VERTEX_MODEL(for Gemini model)GCP_REGION(should be “us-central1”)
Troubleshooting Steps
1. Check Cloud Run Logs
Look for these log messages: Success Indicators:- ”🔍 Attempting to import AI query router…”
- ”✅ AI query router imported successfully”
- ”🚀 ai_query router successfully imported and active”
- ”❌ AI query router import failed”
- ”❌ CRITICAL: AI router import failed”
ModuleNotFoundErrororImportErrortracebacks
2. Test Endpoint Directly
- 404 Not Found (no endpoint available)
- 500 Internal Server Error (import failure)
3. Force Container Rebuild
If dependencies were added but container wasn’t rebuilt:--no-cache flag ensures the container is rebuilt with latest requirements.txt.
Deployment Checklist
Before deploying AI bot changes:- Dependencies: Verify all required packages are in
requirements.txt - Import Paths: Check fallback imports use
sys.path.insert(0, '/app') - Environment Variables: Confirm all required env vars are set in Cloud Run
- No Fallback Endpoint: Ensure inline fallback endpoint is removed
- Force Rebuild: Use
--no-cacheflag if dependencies changed - Test Endpoint: Verify
/api/v1/vertex/queryreturns business data, not generic responses
Historical Context
The Fallback Endpoint Problem
Previously, when the router import failed,main.py would create an inline fallback endpoint:
- Router defines
/api/v1/vertex/query(with full business logic) - Fallback also defines
/api/v1/vertex/query(with generic responses) - FastAPI would use whichever was registered last
Related Files
main.py(lines 80-159): Router import logicapi/routes/ai_query.py: The AI router implementationrequirements.txt: Dependencies including PyJWTapi/bigquery/business_queries.py: Business-level query functionsapi/bigquery/business_matcher.py: Business name fuzzy matching
Monitoring
To monitor router health in production:- Startup Logs: Check for ”🚀 ai_query router successfully imported and active”
- Endpoint Testing: Regular tests of business queries like “how many employees did nug have in aug”
- Error Alerts: Monitor for ”❌ CRITICAL: AI router import failed” messages