Skip to main content

Error Handling

Learn how to handle errors, implement retry strategies, and build resilient applications with the Claro Python SDK.

Exception Hierarchy

The SDK provides a hierarchy of exceptions for different error types:
All SDK exceptions inherit from BaytAPIError, allowing you to catch all SDK errors with a single handler.

Exception Types

BaytAuthError

Authentication and authorization errors (HTTP 401, 403):
Common causes:
  • Invalid or expired API key
  • Insufficient permissions
  • Workspace access denied

BaytNotFoundError

Resource not found errors (HTTP 404):
Common causes:
  • Incorrect package name
  • Wrong version number
  • Prompt deleted or moved
  • No access to the workspace

BaytRateLimitError

Rate limit exceeded (HTTP 429):
Note: The SDK automatically retries on 429 with exponential backoff. This exception is only raised after all retries are exhausted.

BaytValidationError

Invalid request parameters (HTTP 400):
Common causes:
  • Invalid package name format
  • Out of range parameters (e.g., limit > 100)
  • Missing required fields

BaytAPIError

Base exception for all API errors:
Includes:
  • Network errors
  • Server errors (5xx)
  • Timeout errors
  • All specific exceptions above

Basic Error Handling

Catch Specific Errors

Handle different error types appropriately:

Catch All SDK Errors

Use the base exception to catch all SDK errors:

Retry Strategies

Built-in Retries

The SDK automatically retries on rate limits and server errors:
The SDK retries on:
  • 429 (Rate Limit) - Respects Retry-After header
  • 5xx (Server Errors) - Uses exponential backoff
The SDK does NOT retry on:
  • 4xx (Client Errors) - Except 429
  • Network Errors

Manual Retry Logic

Implement custom retry logic for specific operations:

Retry with Decorators

Use a decorator for reusable retry logic:

Graceful Degradation

Provide fallbacks when operations fail:

Logging Errors

Integrate with Python’s logging system:

Error Context

Provide helpful error messages to users:

Complete Example

Best Practices

Order exception handlers from most specific to most general:
Only retry on transient errors:
Always log errors with context:
Don’t expose technical errors to end users:

Next Steps

Advanced Features

Explore rate limiting and performance optimization

Client Configuration

Configure retries and timeouts

API Reference

Complete exception documentation

Quickstart

Review basic usage patterns