File size: 10,289 Bytes
7cfdc4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | """
Centralized error processing for AgentPress.
This module provides a unified way to handle, format, and propagate errors
throughout the agent execution pipeline using LiteLLM's standardized exceptions.
"""
from typing import Dict, Any, Optional, Union
from dataclasses import dataclass
from core.utils.logger import logger
# Import LiteLLM exceptions as documented at https://docs.litellm.ai/docs/exception_mapping
try:
from litellm import (
BadRequestError,
ContextWindowExceededError,
AuthenticationError,
RateLimitError,
ServiceUnavailableError,
APIConnectionError,
APIError,
InternalServerError,
NotFoundError,
ContentPolicyViolationError,
UnprocessableEntityError,
InvalidRequestError,
BudgetExceededError
)
LITELLM_IMPORTED = True
except ImportError:
logger.warning("Could not import LiteLLM exceptions, using generic exception handling")
# Fallback to generic exceptions if LiteLLM imports fail
BadRequestError = Exception
ContextWindowExceededError = Exception
AuthenticationError = Exception
RateLimitError = Exception
ServiceUnavailableError = Exception
APIConnectionError = Exception
APIError = Exception
InternalServerError = Exception
NotFoundError = Exception
ContentPolicyViolationError = Exception
UnprocessableEntityError = Exception
InvalidRequestError = Exception
BudgetExceededError = Exception
LITELLM_IMPORTED = False
@dataclass
class ProcessedError:
"""Standardized error representation."""
error_type: str
message: str
original_error: Optional[Exception] = None
context: Optional[Dict[str, Any]] = None
def to_stream_dict(self) -> Dict[str, Any]:
"""Convert to stream-compatible error dict."""
return {
"type": "status",
"status": "error",
"message": self.message,
"error_type": self.error_type
}
class ErrorProcessor:
"""Centralized error processing using LiteLLM's exception hierarchy."""
@staticmethod
def process_llm_error(error: Exception, context: Optional[Dict[str, Any]] = None) -> ProcessedError:
"""Process LLM-related errors using LiteLLM's exception types."""
error_message = ErrorProcessor.safe_error_to_string(error)
if isinstance(error, ContextWindowExceededError):
# Clean up the error message - remove fallback details for user-facing message
clean_message = error_message
if "Received Model Group=" in error_message:
# Extract just the core error before fallback attempts
parts = error_message.split(". Received Model Group=")
if parts:
clean_message = parts[0]
return ProcessedError(
error_type="context_window_exceeded",
message=f"Context window exceeded: The conversation is too long for this model. Please try starting a new conversation or use a model with a larger context window. {clean_message}",
original_error=error,
context=context
)
elif isinstance(error, AuthenticationError):
return ProcessedError(
error_type="authentication_error",
message=f"Authentication failed: Please check your API credentials. {error_message}",
original_error=error,
context=context
)
elif isinstance(error, RateLimitError):
return ProcessedError(
error_type="rate_limit_error",
message=f"Rate limit exceeded: Too many requests to the API. Please retry later. {error_message}",
original_error=error,
context=context
)
elif isinstance(error, InvalidRequestError):
return ProcessedError(
error_type="invalid_request_error",
message=f"Invalid request: {error_message}",
original_error=error,
context=context
)
elif isinstance(error, BudgetExceededError):
return ProcessedError(
error_type="budget_exceeded_error",
message=f"Budget exceeded: API usage limit reached. {error_message}",
original_error=error,
context=context
)
elif isinstance(error, ServiceUnavailableError):
return ProcessedError(
error_type="service_unavailable",
message=f"Service unavailable: The AI service is temporarily unavailable. {error_message}",
original_error=error,
context=context
)
elif isinstance(error, ContentPolicyViolationError):
return ProcessedError(
error_type="content_policy_violation",
message=f"Content policy violation: The request was rejected by content filters. {error_message}",
original_error=error,
context=context
)
elif isinstance(error, NotFoundError):
# Handle NotFoundError (e.g., model doesn't support image input)
# Clean up the error message - remove fallback details for user-facing message
clean_message = error_message
if "Received Model Group=" in error_message:
# Extract just the core error before fallback attempts
parts = error_message.split(". Received Model Group=")
if parts:
clean_message = parts[0]
# Check if it's an image input error
if "image" in error_message.lower() or "No endpoints found" in error_message:
return ProcessedError(
error_type="model_not_supported",
message=f"Model does not support image input. The system will automatically try an alternative model. {clean_message}",
original_error=error,
context=context
)
return ProcessedError(
error_type="not_found_error",
message=f"Model or endpoint not found: {clean_message}",
original_error=error,
context=context
)
elif isinstance(error, BadRequestError):
# Clean up the error message - extract just the core error, not fallback details
clean_message = error_message
# If message contains fallback details, extract just the first error
if "Received Model Group=" in error_message:
# Extract the actual error before fallback details
parts = error_message.split(". Received Model Group=")
if parts:
clean_message = parts[0]
# If it's a Bedrock content block error, make it user-friendly
if "ContentBlock" in clean_message and "is blank" in clean_message:
clean_message = "Message contains empty content blocks. This may be a data formatting issue. Please try starting a new conversation."
return ProcessedError(
error_type="bad_request",
message=f"Invalid request: {clean_message}",
original_error=error,
context=context
)
elif isinstance(error, (APIConnectionError, APIError, InternalServerError)):
return ProcessedError(
error_type="api_error",
message=f"API error: {error_message}",
original_error=error,
context=context
)
else:
# Fallback for unknown error types
return ProcessedError(
error_type="llm_error",
message=f"LLM error: {error_message}",
original_error=error,
context=context
)
@staticmethod
def process_tool_error(error: Exception, tool_name: str, context: Optional[Dict[str, Any]] = None) -> ProcessedError:
"""Process tool execution errors."""
return ProcessedError(
error_type="tool_execution_error",
message=f"Tool '{tool_name}' execution failed: {ErrorProcessor.safe_error_to_string(error)}",
original_error=error,
context=context
)
@staticmethod
def process_system_error(error: Exception, context: Optional[Dict[str, Any]] = None) -> ProcessedError:
"""Process general system errors."""
return ProcessedError(
error_type="system_error",
message=f"System error: {ErrorProcessor.safe_error_to_string(error)}",
original_error=error,
context=context
)
@staticmethod
def safe_error_to_string(error: Exception) -> str:
"""Safely convert an exception to a string - return whatever we get"""
try:
return str(error)
except Exception:
return f"{type(error).__name__}"
@staticmethod
def log_error(processed_error: ProcessedError, level: str = "error") -> None:
"""Log a processed error with appropriate level."""
log_func = getattr(logger, level, logger.error)
log_message = f"[{processed_error.error_type.upper()}] {processed_error.message}"
# NEVER pass exc_info to structlog - it causes concatenation errors with complex exceptions
# Instead, log the error details safely
if processed_error.original_error:
try:
error_details = f"Original error: {ErrorProcessor.safe_error_to_string(processed_error.original_error)}"
log_func(f"{log_message} | {error_details}")
except Exception:
# If even our safe conversion fails, just log the message
log_func(log_message)
else:
log_func(log_message)
if processed_error.context:
logger.debug(f"Error context: {processed_error.context}")
|