Production Errors Fixed - VettCode AI Chat System
Date: August 14, 2026
Issues Identified from Production Logs
1. ❌ "No AI providers available" Error
Error:
Error: No AI providers available
at l.generateChat (/var/task/.next/server/chunks/121.js:80:7113)
Root Cause:
- The
OPENROUTER_API_KEYand/orGROQ_API_KEYenvironment variables are not set in the Vercel production environment - The
AIProviderRegistryregisters 0 providers when API keys are missing - When
getAvailableProviders()returns an empty array, the router throws "No AI providers available"
Fix Applied:
- ✅ Added detailed logging to
AIProviderRegistry.registerProviders()to show which providers are available - ✅ Added environment variable validation logging at startup
- ✅ Enhanced error messages in
ai-chat.tsto handle "No AI providers" gracefully - ✅ Falls back to template responses when no providers are available
User Impact:
- Before: Users saw technical error or app crash
- After: Users see template-based overview seamlessly (no error visible to user)
Action Required:
- Set
OPENROUTER_API_KEYin Vercel environment variables - Get key from: https://openrouter.ai/
2. ❌ Invalid MongoDB ObjectId Error
Error:
Failed to get user plan: BSONError: input must be a 24 character hex string, 12 byte Uint8Array, or an integer
at new ObjectId (/var/task/node_modules/bson/lib/bson.cjs:2538:23)
at n.findById (/var/task/.next/server/chunks/230.js:599:579)
Root Cause:
getUserPlan()insubscription.tswas callingUserModel.findById(userId)without validating the userId format- MongoDB ObjectIds must be exactly 24 hexadecimal characters
- Invalid userId formats (like session IDs or malformed strings) caused BSON errors
Fix Applied:
- ✅ Added ObjectId format validation using regex:
/^[0-9a-fA-F]{24}$/ - ✅ Falls back to free plan if userId format is invalid
- ✅ Added detailed logging to track invalid userIds
- ✅ Graceful error handling that doesn't crash the request
Code:
// Validate ObjectId format before querying MongoDB const objectIdRegex = /^[0-9a-fA-F]{24}$/; if (!objectIdRegex.test(userId)) { console.warn( `[SUBSCRIPTION] Invalid ObjectId format: ${userId}. Falling back to free plan.`, ); return SUBSCRIPTION_PLANS.free; }
User Impact:
- Before: API requests failed with 500 error
- After: System gracefully handles invalid userIds and uses free plan
3. ✅ User-Friendly Error Messages
Improvement: Replaced technical error messages with user-friendly ones:
| Error Type | Technical Message | User-Friendly Message |
|---|---|---|
| No providers | "No AI providers available" | "VettCode Coach is temporarily offline for maintenance. Our templates can still help!" |
| Rate limit | "429 Too Many Requests" | "VettCode Coach is experiencing high demand. Please try again in a few moments." |
| Invalid response | "AI_PROVIDER_INVALID_RESPONSE" | "VettCode Coach had trouble processing that request. Could you rephrase your question?" |
| Generic error | Stack trace | "I'm having trouble responding right now. Please try again in a moment." |
Code Location: lib/ai-chat.ts - generateChatResponse() catch block
4. ✅ Enhanced Logging for Debugging
Added comprehensive logging throughout the AI system:
Provider Registry:
[AI-PROVIDER-REGISTRY] Registering AI providers...
[AI-PROVIDER-REGISTRY] ✓ OpenRouter provider registered
[AI-PROVIDER-REGISTRY] ✗ Groq provider NOT available (missing GROQ_API_KEY)
[AI-PROVIDER-REGISTRY] Total providers registered: 1
Subscription System:
[SUBSCRIPTION] Invalid ObjectId format: abc123. Falling back to free plan.
[SUBSCRIPTION] User not found: 6a7c5c64c5b99e9b18e74414. Falling back to free plan.
AI Chat:
[AI-CHAT][req_123456] CRITICAL: No AI providers configured!
[AI-CHAT][req_123456] Chat response generation failed: No AI providers available
Verification Checklist
✅ Completed
- ObjectId validation in
getUserPlan() - User-friendly error messages in chat
- Provider registry logging
- Graceful fallback to templates
- Error classification by type
⚠️ Action Required (Deployment)
- Set
OPENROUTER_API_KEYin Vercel environment variables - Optionally set
GROQ_API_KEYfor backup provider - Deploy changes to production
- Monitor logs for "[AI-PROVIDER-REGISTRY]" messages
- Verify AI chat works end-to-end
Testing Instructions
Local Testing
# Test with no API keys (should use templates) unset OPENROUTER_API_KEY unset GROQ_API_KEY npm run dev # Visit /dashboard/scans/[scanId]/ai # Should see template overview (not error)
Production Testing
- Deploy to Vercel
- Check build logs for provider registration messages
- Open AI chat page
- Verify either:
- AI responses work (if keys are set)
- Template responses work (if keys are missing)
- No crashes or 500 errors in either case
Files Modified
-
lib/subscription.ts
- Added ObjectId validation
- Enhanced error logging
- Graceful fallback handling
-
lib/ai-chat.ts
- User-friendly error messages
- Error type classification
- Better logging throughout
-
lib/ai-providers.ts
- Provider registry logging
- Environment variable diagnostics
- Startup validation
Environment Variables Required
# Required for AI chat to work OPENROUTER_API_KEY=sk-or-v1-xxxxxxxxxxxxx # Optional backup provider GROQ_API_KEY=gsk_xxxxxxxxxxxxx # MongoDB connection (ensure it's valid) MONGODB_URI=mongodb+srv://user:pass@cluster.mongodb.net/vettcode # NextAuth (for user sessions) NEXTAUTH_SECRET=your_secret_min_32_chars NEXTAUTH_URL=https://vettedcodewe.vercel.app
Next Steps
-
Immediate:
- Set OPENROUTER_API_KEY in Vercel project settings
- Redeploy to production
- Test AI chat functionality
-
Monitoring:
- Watch production logs for "[AI-PROVIDER-REGISTRY]" on startup
- Monitor "[SUBSCRIPTION]" logs for invalid ObjectIds
- Track error rates in AI chat
-
Future Improvements:
- Add server-side request deduplication (prevent duplicate AI calls)
- Implement retry button UI for failed AI requests
- Add usage analytics dashboard
Related Documentation
docs/AI_CHAT_FIXES_SUMMARY.md- Complete implementation historydocs/AI_IMPLEMENTATION_GUIDE.md- AI system architecture.env.example- All required environment variables