Error Handling Guide¶
This guide explains the comprehensive error handling implemented in the YT Music API and how to troubleshoot common issues.
Overview¶
The YT Music API implements robust error handling to gracefully manage:
- YouTube Music API structure changes
- Network connectivity issues
- Authentication requirements
- Rate limiting
- Invalid parameters
Common Error Types¶
KeyError: "Unable to find 'header'"¶
This is the most frequent error, occurring when YouTube Music changes their internal API structure.
Symptoms:
{
"error": "API structure error",
"message": "YouTube Music API structure has changed, search temporarily unavailable",
"technical_details": "KeyError: Unable to find 'header'..."
}
Solutions:
- Use simplified search parameters (lower limits)
- Check the health endpoint:
/search/health - Update ytmusicapi:
pip install --upgrade ytmusicapi - Try different search filters
Authentication Errors (401)¶
Some endpoints require YouTube Music authentication.
Response:
{
"error": "Authentication required",
"message": "Authentication required to access library playlists"
}
Solution: Set up authentication following the ytmusicapi authentication guide.
Rate Limiting (429)¶
Response:
{
"error": "Rate limit exceeded",
"message": "API rate limit exceeded. Please try again later.",
"retry_after": "60"
}
Solution: Implement exponential backoff in your client code.
Network Issues (503/504)¶
Responses:
Solution: Check internet connectivity and retry the request.
Error Response Structure¶
All errors follow a consistent structure:
{
"error": "error_category",
"message": "Human readable message",
"operation": "operation_name",
"identifier": "resource_id",
"technical_details": "detailed_error_info"
}
Health Monitoring¶
Monitor API health using these endpoints:
- Global Status:
/api/status - Search Health:
/search/health
Health Check Responses¶
Operational:
Degraded:
{
"status": "degraded",
"message": "YouTube Music API structure issues detected",
"recommendation": "Use simplified search parameters"
}
Error:
{
"status": "error",
"message": "API connectivity issues",
"recommendation": "Check internet connection and try again"
}
Fallback Mechanisms¶
The API implements automatic fallbacks:
- Simplified Parameters: Reduces complexity when structure errors occur
- Alternative Endpoints: Uses different ytmusicapi methods when available
- Graceful Degradation: Returns partial results when possible
- Retry Logic: Automatic retries with exponential backoff
Best Practices¶
For Client Applications¶
- Always check health endpoints before making requests
- Implement retry logic with exponential backoff
- Handle rate limiting gracefully
- Parse error responses to provide user-friendly messages
- Monitor API status for proactive issue detection
Example Client Code¶
import requests
import time
import random
def safe_api_call(url, max_retries=3):
for attempt in range(max_retries):
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
elif response.status_code == 429:
# Rate limited - wait and retry
wait_time = 2 ** attempt + random.uniform(0, 1)
time.sleep(wait_time)
continue
elif response.status_code == 503:
# Service degraded - check health
health = requests.get("/search/health").json()
if health["status"] != "operational":
print(f"API Issue: {health['message']}")
return None
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
if attempt < max_retries - 1:
time.sleep(2 ** attempt)
continue
return None
Troubleshooting Steps¶
- Check API Status
- Test Basic Search
-
Check Logs Review
ytmusic_api.logfor detailed error information -
Update Dependencies
- Verify Network Connectivity
Getting Help¶
If issues persist:
- Check the GitHub Issues
- Review the ytmusicapi documentation
- Create a detailed bug report with:
- Error messages
- Request parameters
- Health check results
- Log excerpts