7.5 KiB
7.5 KiB
LinkedIn Writer Timeout and Connection Issues - Complete Fix
🐛 Issues Identified from Logs
Primary Issue: Gemini API Connection Timeout
ERROR|gemini_grounded_provider.py:99:generate_grounded_content| ❌ Error generating grounded content: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Secondary Issues:
- Frontend timeout: 60-second frontend timeout being hit
- No fallback mechanism: When Gemini fails, entire generation fails
- Research sources: 0 sources found because grounding failed
- Loading state issues: Fixed in previous session
✅ Comprehensive Fixes Implemented
1. Backend Fallback Mechanism
File: backend/services/linkedin/content_generator.py
Added robust fallback logic when Gemini grounded provider fails:
except Exception as e:
logger.error(f"Error generating grounded post content: {str(e)}")
logger.info("Attempting fallback to standard content generation...")
# Fallback to standard content generation without grounding
try:
if not self.fallback_provider:
raise Exception("No fallback provider available")
# Build a simpler prompt for fallback generation
prompt = PostPromptBuilder.build_post_prompt(request)
# Generate content using fallback provider
result = await self.fallback_provider.generate_content(
prompt=prompt,
temperature=0.7,
max_tokens=request.max_length
)
# Return result in the expected format
return {
'content': result.get('content', ''),
'sources': [],
'citations': [],
'grounding_enabled': False,
'fallback_used': True
}
except Exception as fallback_error:
logger.error(f"Fallback generation also failed: {str(fallback_error)}")
raise Exception(f"Failed to generate content: {str(e)}. Fallback also failed: {str(fallback_error)}")
2. Gemini Provider Timeout Configuration
File: backend/services/llm_providers/gemini_grounded_provider.py
Added timeout handling to prevent indefinite hanging:
# Initialize the Gemini client with timeout configuration
self.client = genai.Client(api_key=self.api_key)
self.timeout = 30 # 30 second timeout for API calls
# Make the request with native grounding and timeout
import asyncio
try:
response = await asyncio.wait_for(
self.client.models.generate_content(
model="gemini-2.5-flash",
contents=grounded_prompt,
config=config,
),
timeout=self.timeout
)
except asyncio.TimeoutError:
raise Exception(f"Gemini API request timed out after {self.timeout} seconds")
3. Frontend Timeout Extension
File: frontend/src/services/linkedInWriterApi.ts
Updated LinkedIn writer API calls to use aiApiClient with 3-minute timeout instead of 60-second timeout:
// Changed from apiClient (60s timeout) to aiApiClient (180s timeout)
async generatePost(request: LinkedInPostRequest): Promise<LinkedInPostResponse> {
const { data } = await aiApiClient.post('/api/linkedin/generate-post', request);
return data;
},
async generateArticle(request: LinkedInArticleRequest): Promise<LinkedInArticleResponse> {
const { data } = await aiApiClient.post('/api/linkedin/generate-article', request);
return data;
},
async generateCarousel(request: LinkedInCarouselRequest): Promise<LinkedInCarouselResponse> {
const { data } = await aiApiClient.post('/api/linkedin/generate-carousel', request);
return data;
},
async generateVideoScript(request: LinkedInVideoScriptRequest): Promise<LinkedInVideoScriptResponse> {
const { data } = await aiApiClient.post('/api/linkedin/generate-video-script', request);
return data;
},
4. Loading State Management (Previously Fixed)
File: frontend/src/components/LinkedInWriter/RegisterLinkedInActions.tsx
Added proper loading start/end events:
// Start loading state
window.dispatchEvent(new CustomEvent('linkedinwriter:loadingStart', {
detail: {
action: 'generateLinkedInPost',
message: 'Generating LinkedIn post with persona optimization...'
}
}));
// End loading state
window.dispatchEvent(new CustomEvent('linkedinwriter:loadingEnd'));
🔧 How the Fixes Work Together
Error Handling Flow
- Gemini API Call: Attempts to use Gemini with 30-second timeout
- Timeout/Connection Error: If Gemini fails, fallback is triggered
- Fallback Generation: Uses alternative LLM provider (OpenAI/Anthropic)
- Content Generation: Produces content without grounding but still functional
- Frontend Handling: 3-minute timeout allows for retry/fallback scenarios
- Loading States: Proper feedback throughout the process
Timeout Configuration
- Gemini API: 30 seconds (prevents indefinite hanging)
- Frontend API: 180 seconds (3 minutes for AI operations)
- Backend Processing: Graceful fallback within 30 seconds
🧪 Testing the Fixes
Expected Behavior
- Normal Operation: Gemini works → Grounded content with sources
- Gemini Failure: Fallback triggered → Content generated without grounding
- Network Issues: Timeout after 30 seconds → Fallback to alternative provider
- Frontend: No more 60-second timeouts, proper loading states
Debug Information
Check logs for these messages:
"Attempting fallback to standard content generation...""Gemini API request timed out after 30 seconds""Fallback generation also failed"(if both fail)
🚀 Benefits of the Fixes
1. Reliability
- Graceful degradation: System continues working even when Gemini fails
- Multiple fallbacks: Primary → Secondary → Error handling
- Timeout protection: No more indefinite hanging
2. User Experience
- Faster feedback: 30-second timeout instead of indefinite waiting
- Proper loading states: Users see progress throughout
- Content generation: Always produces content, even without grounding
3. System Stability
- Network resilience: Handles connection issues gracefully
- API reliability: Multiple provider options
- Error recovery: Automatic fallback mechanisms
📋 Verification Checklist
- Gemini API timeout after 30 seconds (not indefinite)
- Fallback content generation when Gemini fails
- Frontend timeout extended to 3 minutes
- Loading states work properly throughout
- Content generated even without grounding
- Error messages are informative
- System recovers from network issues
🔮 Future Improvements
- Health Checks: Monitor Gemini API availability
- Circuit Breaker: Temporarily disable Gemini if consistently failing
- Retry Logic: Automatic retry with exponential backoff
- Metrics: Track fallback usage and success rates
- User Notification: Inform users when fallback is used
🎯 Root Cause Resolution
The timeout issues were caused by:
- No timeout on Gemini API calls → Fixed with 30-second timeout
- No fallback mechanism → Fixed with automatic fallback
- Frontend timeout too short → Fixed with 3-minute timeout
- Poor error handling → Fixed with comprehensive error management
The system now handles network issues gracefully and provides a reliable content generation experience even when external APIs fail.