Skip to main content

Overview

The Google Gen AI SDK provides comprehensive error handling through the APIError 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 base APIError 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

  1. Catch Specific Errors: Use ClientError and ServerError for different handling strategies
  2. Inspect Error Details: Check code, status, and message for detailed error information
  3. Configure Retries: Set appropriate retry behavior for your use case
  4. Log Error Details: Store error.details for debugging and monitoring
  5. Implement Fallbacks: Have backup models or strategies when primary requests fail
  6. Handle Rate Limits: Implement exponential backoff for 429 errors
  7. Monitor Errors: Track error rates and types to identify issues early