Exceptions
The Lunar SDK provides a comprehensive exception hierarchy for error handling. Exceptions are categorized into two groups based on whether fallback should be attempted.Exception Hierarchy
Non-Fallback Errors
These errors indicate problems with your request. Fallback to another model won’t help.BadRequestError (400)
Invalid request parameters.AuthenticationError (401)
Invalid or missing API key/JWT.PermissionDeniedError (403)
Access denied to the resource.NotFoundError (404)
Model or resource not found.UnprocessableEntityError (422)
Request validation failed.ChatNotSupportedError
Model doesn’t support chat completions.Fallback-Triggering Errors
These errors indicate temporary issues. The SDK will try fallback models if configured.RateLimitError (429)
Rate limit exceeded.ServerError (5xx)
Internal server error.ServiceUnavailableError (503)
Service temporarily unavailable.GatewayTimeoutError (504)
Gateway timeout.ConnectionError
Network connection failed.TimeoutError
Request timed out.Error Handling Patterns
Catch All API Errors
Catch All Lunar Errors
Comprehensive Error Handling
With Fallbacks
When fallbacks are configured, fallback-triggering errors are handled automatically:Exception Properties
APIError Properties
| Property | Type | Description |
|---|---|---|
message | str | Error message |
status_code | int | HTTP status code |
response_body | dict | Full response body |
RateLimitError Properties
| Property | Type | Description |
|---|---|---|
retry_after | float | Seconds to wait |
ChatNotSupportedError Properties
| Property | Type | Description |
|---|---|---|
model | str | The model that doesn’t support chat |