Overview
The Google Gen AI SDK provides comprehensive error handling through theAPIError class and its subclasses. You can catch specific error types, inspect error details, and configure automatic retry behavior.
APIError Class
All API errors inherit from the baseAPIError class, which provides access to error details:
Error Types
ClientError (4xx)
Raised when the request is invalid due to client-side issues:ServerError (5xx)
Raised when the API experiences internal server issues:Other Error Types
The SDK also defines these error types for specific situations:Error Handling Strategies
Basic Try-Catch
Granular Error Handling
Async Error Handling
Retry Configuration
Configure automatic retry behavior for transient errors:Basic Retry Configuration
Custom Retryable Status Codes
Specify which HTTP status codes should trigger retries:Disable Retries
Per-Request Retry Configuration
Override retry settings for individual requests:Retry Logic Details
The SDK uses exponential backoff with jitter for retries:Example Retry Delays
With default settings (initial_delay=1.0, exp_base=2.0, max_delay=60.0, jitter=1.0):
- Attempt 1: 0-2 seconds (1.0 * 2^0 * random)
- Attempt 2: 0-4 seconds (1.0 * 2^1 * random)
- Attempt 3: 0-8 seconds (1.0 * 2^2 * random)
- Attempt 4: 0-16 seconds (1.0 * 2^3 * random)
- Attempt 5: 0-32 seconds (1.0 * 2^4 * random)
Complete Error Handling Example
Best Practices
- Catch Specific Errors: Use
ClientErrorandServerErrorfor different handling strategies - Inspect Error Details: Check
code,status, andmessagefor detailed error information - Configure Retries: Set appropriate retry behavior for your use case
- Log Error Details: Store
error.detailsfor debugging and monitoring - Implement Fallbacks: Have backup models or strategies when primary requests fail
- Handle Rate Limits: Implement exponential backoff for 429 errors
- Monitor Errors: Track error rates and types to identify issues early