Skip to main content

Overview

The Perplexity SDKs provide robust error handling with specific exception types for different error scenarios. This guide covers how to catch and handle common API errors gracefully.

Common Error Types

The SDKs provide specific exception types for different error scenarios:
  • APIConnectionError - Network connection issues
  • RateLimitError - API rate limit exceeded
  • APIStatusError - HTTP status errors (4xx, 5xx)
  • AuthenticationError - Invalid API key or authentication issues
  • ValidationError - Invalid request parameters

Basic Error Handling

Handle common API errors with try-catch blocks:
Common HTTP status codes: 400 (Bad Request), 401 (Authentication), 403 (Permission Denied), 404 (Not Found), 429 (Rate Limit), 500+ (Server Error).

Advanced Error Handling

Exponential Backoff for Rate Limits

Implement intelligent retry logic for rate limit errors:

Error Context and Debugging

Extract detailed error information for debugging:

Error Recovery Strategies

Graceful Degradation

Implement fallback mechanisms when API calls fail:

Best Practices

1

Always handle rate limits

Rate limiting is common with API usage. Always implement retry logic with exponential backoff.
Don’t implement aggressive retry loops without delays - this can worsen rate limiting.
2

Log errors for monitoring

Include proper logging to track error patterns and API health.
3

Set appropriate timeouts

Configure timeouts to prevent hanging requests.
4

Handle authentication errors

Check for invalid API keys and provide helpful error messages.

Configuration

Configure timeouts and retries

Best Practices

Environment variables and rate limiting