Error Handling#
Build robust applications with Shadai's comprehensive error handling system that provides structured, actionable error information.Automatic Error Handling#
New in v0.1.30: Shadai now automatically displays clean, user-friendly error messages without requiring try-except blocks!Clean Error Messages by Default#
When an error occurs, Shadai automatically displays a formatted message instead of a verbose Python traceback:No more verbose tracebacks! The error handler automatically:✅ Shows the error code in brackets
✅ Displays a clear, concise message
✅ Includes actionable suggestions when available
✅ Hides internal stack traces from users
✅ Exits cleanly with appropriate error code
When to Use Manual Error Handling#
You can still use try-except blocks for programmatic error handling:Use manual error handling when:Building UIs that need to show custom error dialogs
Implementing retry logic or fallback mechanisms
Logging errors to external services
Need programmatic access to error details
Don't use manual error handling when:Building simple CLI scripts
Error messages are displayed directly to users
Default formatting is sufficient
Overview#
Shadai uses a hierarchical exception system with:Structured error messages - Human-readable and user-friendly
Machine-readable error codes - For programmatic handling
Contextual information - Debug details and affected resources
Retry flags - Indicates if operation can be retried
Suggestions - Actionable guidance for resolution
Automatic formatting - Clean output without verbose tracebacks
Exception Hierarchy#
ShadaiError (base)
├── ConnectionError
├── AuthenticationError
│ ├── InvalidAPIKeyError
│ └── MissingAccountSetupError
├── ResourceError
│ ├── ResourceNotFoundError
│ │ ├── SessionNotFoundError
│ │ ├── FileNotFoundError
│ │ └── AccountNotFoundError
│ └── ResourceAlreadyExistsError
│ └── SessionAlreadyExistsError
├── ValidationError
│ ├── InvalidFileTypeError
│ ├── InvalidParameterError
│ ├── MissingParameterError
│ ├── InvalidBase64Error
│ └── BatchSizeLimitExceededError
├── AuthorizationError
│ └── PlanLimitExceededError
│ ├── KnowledgePointsLimitExceededError
│ └── FileSizeLimitExceededError
├── ExternalServiceError
│ ├── LLMProviderError
│ ├── VectorStoreError
│ └── S3StorageError
├── ProcessingError
│ ├── FileParsingError
│ └── ChunkIngestionError
└── SystemError
├── ConfigurationError
├── DatabaseError
├── TimeoutError
└── ServerError
Import Exceptions#
Common Errors#
Authentication Errors#
Invalid API Key#
Cause: API key is missing or incorrectVerify API key in .env file
Regenerate API key if compromised
Ensure SHADAI_API_KEY environment variable is set
Missing Account Setup#
Cause: API key valid but account not configuredConfiguration Errors#
Missing LLM or Embedding Model#
Cause: Session or account lacks required AI modelsConfigure LLM model in session or account settings
Configure embedding model for document processing
Check that models are properly saved
config_key: "llm_model" or "embedding_model"
reason: Detailed explanation
Resource Errors#
Session Not Found#
Cause: Session was deleted or UUID is incorrectVerify session UUID is correct
Create new session if old one was deleted
Session Already Exists#
Cause: Attempting to create duplicate session nameUse session_get_or_create instead of session_create
Choose a different session name
Delete existing session if intended
Validation Errors#
Invalid File Type#
Cause: Uploading unsupported file formatSupported Types: .pdf, .jpg, .jpeg, .png, .webpInvalid Parameter#
Cause: Parameter value out of acceptable rangeAuthorization & Limit Errors#
Knowledge Points Limit Exceeded#
Cause: Monthly knowledge points quota exhaustedDelete unused files to free knowledge points
Upgrade to higher plan tier
Wait for monthly reset (1st of month)
plan_name: Current plan (e.g., "Free", "Pro")
current_value: Points used this month
max_allowed: Monthly quota
points_needed: Points required for operation
File Size Limit Exceeded#
Cause: File exceeds plan's maximum file sizeSplit large file into smaller parts
Compress or optimize file
Upgrade to plan with larger file limits
External Service Errors#
LLM Provider Error#
Cause: AI provider API failure or rate limitRetry after delay (exponential backoff)
Check if provider is experiencing outages
Switch to different model provider
Connection Errors#
Cause: Network issues or server unreachableCheck internet connectivity
Verify base_url is correct
Increase timeout: Shadai(..., timeout=60)
Check firewall/proxy settings
Comprehensive Error Handling#
Basic Pattern#
Detailed Pattern with Specific Handlers#
Retry Logic#
Smart Retry with Retriable Check#
Exponential Backoff with Jitter#
Graceful Degradation#
Feature Fallback Strategy#
User-Friendly Error Messages#
Production-Ready Error Handler#
Logging Errors#
Structured Logging with Context#
Best Practices#
✅ Do This#
❌ Don't Do This#
Testing Error Scenarios#
Unit Tests with pytest#
Error Monitoring#
Integration with Sentry#
Next Steps#
Pro Tip: Use error.context to provide specific, actionable feedback. The backend provides rich context to help you build great UX.Modified at 2025-10-21 05:35:25