Error Handling
This guide covers error handling when using the Amigo SDK in Python or TypeScript. Both SDKs include built-in retry logic for transient network failures and provide comprehensive error handling for all API operations.
Error Types
Both SDKs provide comprehensive typed error handling for different types of failures:
from amigo_sdk.errors import (
AmigoError, # Base error class
AuthenticationError, # Invalid credentials
NotFoundError, # Resource not found
BadRequestError, # Invalid request
ValidationError, # Request validation failed
ConflictError, # Resource conflict
NetworkError, # Network/connection issues
)
Basic Error Handling
from amigo_sdk import AmigoClient
from amigo_sdk.errors import (
AuthenticationError,
NotFoundError,
BadRequestError,
ValidationError,
ConflictError,
NetworkError,
AmigoError
)
def handle_errors_example():
try:
with AmigoClient(
api_key="your-api-key",
api_key_id="your-api-key-id",
user_id="your-user-id",
organization_id="your-org-id"
) as client:
# Your API calls here
org = client.organization.get()
print(f"Organization: {org.name}")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
# Handle invalid credentials - check API keys
except NotFoundError as e:
print(f"Resource not found: {e}")
# Handle missing resources - check IDs
except BadRequestError as e:
print(f"Bad request: {e}")
# Handle invalid request format
except ValidationError as e:
print(f"Validation failed: {e}")
# Handle request validation errors
except ConflictError as e:
print(f"Conflict: {e}")
# Handle resource conflicts (often recoverable)
except NetworkError as e:
print(f"Network error: {e}")
# Handle connection issues - SDKs handle retries automatically
except AmigoError as e:
print(f"Amigo API error: {e}")
# Handle any other Amigo-specific errors
except Exception as e:
print(f"Unexpected error: {e}")
# Handle unexpected errors
Stream Error Handling
When processing streaming responses, handle errors within the stream (see also Conversations: Interact):
def handle_stream_errors():
with AmigoClient() as client:
try:
events = client.conversation.create_conversation(request)
for event in events:
event_data = event.model_dump(mode="json")
# Check for error events in the stream
if event_data.get("type") == "error":
error_message = event_data.get("message", "Unknown error")
print(f"Stream error: {error_message}")
break
# Process normal events
print(f"Event: {event_data.get('type')}")
except Exception as e:
print(f"Stream processing error: {e}")
Built-in Retry Logic
Both SDKs include automatic retry logic for transient failures:
Network errors
Automatic exponential backoff
Rate limiting
Automatic retry with proper delay
Server errors (5xx)
Intelligent retry with backoff
Retry Flow Diagram
Best Practices
Handle specific error types — Don't just catch generic exceptions
Log errors with context — Include relevant IDs and operation details
Check for stream error events — Monitor the
error
event type in streamsTrust built-in retries — Let the SDK handle transient failures automatically
Graceful degradation — Provide fallback behavior when possible
Monitor error patterns — Track error rates and types in production
Next Steps
With proper error handling in place, you're ready to:
Work with conversations — Streaming and robust handlers
Manage users effectively — User management with error handling
Last updated
Was this helpful?