3.4 KiB
SerpAPI Setup Guide
Overview
The Trading Agents system now supports SerpAPI for significantly faster news retrieval. This replaces the slow web scraping approach with a fast, reliable API service.
Performance Comparison
- Web Scraping: ~130 seconds for 300 articles
- SerpAPI: ~2-5 seconds for 300 articles
- Speed Improvement: 25-60x faster
Setup Instructions
1. Get SerpAPI Key
- Visit SerpAPI
- Sign up for a free account
- Get your API key from the dashboard
- Free tier includes 100 searches per month
2. Set Environment Variable
Add your SerpAPI key to your environment:
# Option 1: Export in terminal
export SERPAPI_API_KEY="your_serpapi_key_here"
# Option 2: Add to .env file
echo "SERPAPI_API_KEY=your_serpapi_key_here" >> .env
# Option 3: Add to shell profile (permanent)
echo 'export SERPAPI_API_KEY="your_serpapi_key_here"' >> ~/.bashrc
source ~/.bashrc
3. Verify Setup
Run the test to verify SerpAPI is working:
python test_serpapi_news.py
You should see output like:
✅ SerpAPI Key Found: 1234567890...
🚀 Testing SerpAPI for query: AAPL
✅ SerpAPI Results: 100 articles in 2.34s
⚡ Speed Improvement: 56.7x faster with SerpAPI
How It Works
The system automatically detects if a SerpAPI key is available:
- With SerpAPI key: Uses fast SerpAPI service
- Without SerpAPI key: Falls back to web scraping (slow but free)
Integration
The SerpAPI integration is automatically used in:
get_google_news()function- News Analyst agent
- All news-related tools
No code changes needed - just set the environment variable!
API Usage
The system uses the Google News search engine through SerpAPI:
# Automatic usage in get_google_news()
news_data = get_google_news("AAPL", "2025-07-05", 7)
# Direct usage (if needed)
from tradingagents.dataflows.serpapi_utils import getNewsDataSerpAPI
results = getNewsDataSerpAPI("AAPL", "2025-07-01", "2025-07-05")
Error Handling
The system includes robust error handling:
- Invalid API key: Falls back to web scraping
- Rate limiting: Respects API limits with delays
- Network errors: Graceful fallback to alternative methods
Cost Considerations
- Free tier: 100 searches/month
- Paid plans: Start at $50/month for 5,000 searches
- Usage: Each news query = 1 search
- Recommendation: Monitor usage in SerpAPI dashboard
Troubleshooting
Common Issues
-
"SerpAPI key not found"
- Check environment variable is set
- Restart terminal/IDE after setting variable
-
"SerpAPI Error: Invalid API key"
- Verify key is correct
- Check key hasn't expired
-
Still slow performance
- Verify SerpAPI key is being used (check logs)
- Test with
test_serpapi_news.py
Debug Commands
# Check if environment variable is set
echo $SERPAPI_API_KEY
# Test SerpAPI directly
python test_serpapi_news.py
# Test integrated news function
python test_news_integration.py
Benefits
✅ Speed: 25-60x faster news retrieval
✅ Reliability: Professional API service
✅ Fallback: Automatic fallback to web scraping
✅ Easy Setup: Just set environment variable
✅ Cost Effective: Free tier for testing
✅ No Code Changes: Drop-in replacement
Next Steps
- Sign up for SerpAPI account
- Set environment variable
- Test with provided scripts
- Enjoy faster news analysis!