Skip to main content
Error handling documentation is being expanded. Core patterns are shown below.

Overview

The Shannon Python SDK provides comprehensive error handling to help you build robust applications. All SDK exceptions inherit from ShannonError.

Exception Hierarchy

Budget and Task Failure Handling: Budget exceeded and task failures are not exceptions. For failures, check status.status. For token usage and cost totals, use list_tasks() and read total_token_usage from the returned task summaries.

Basic Error Handling

Try-Catch Pattern

Specific Error Types

Connection Errors

Handle network and connection issues:

Check Cost and Failures

Monitor failures from status and get usage totals via list_tasks():

Timeout Errors

Handle long-running operations:

Rate Limiting

Handle API rate limits gracefully:
Import clarity: from shannon import ConnectionError refers to the SDK’s exception (not Python’s built‑in ConnectionError). Use RateLimitError for rate limiting and ConnectionError for network issues. Advanced patterns such as backoff and circuit breakers are reference implementations — validate in your environment.

Validation Errors

Handle invalid parameters:

Task Failure Handling

Handle task execution failures:

Logging Errors

Implement comprehensive error logging:

Circuit Breaker Pattern

Implement circuit breaker for resilience:

Best Practices

  1. Always catch specific exceptions before generic ones
  2. Implement retry logic with exponential backoff
  3. Log errors for debugging and monitoring
  4. Provide fallback options for critical operations
  5. Set reasonable timeouts to avoid hanging
  6. Validate inputs before submission
  7. Use circuit breakers for external dependencies

Next Steps

Examples

See error handling in action

Async Usage

Async error patterns

Complete Retry Example

To run:
When to use retry logic:
  • Network instability
  • Transient service issues
  • Production systems requiring resilience
When NOT to retry:
  • Timeouts (task is too complex)
  • API errors (invalid parameters)
  • Authentication failures