feat: add LiteLLM exception handling with ExInfo dataclass

This commit is contained in:
Paul Gauthier 2024-11-07 12:45:27 -08:00 committed by Paul Gauthier (aider)
parent dad335b8b6
commit 8bc9ebf2aa

View file

@ -1,4 +1,4 @@
from dataclasses import dataclass
def retry_exceptions():
import httpx
@ -31,4 +31,64 @@ def retry_exceptions():
)
@dataclass
class ExInfo:
name: str
retry: bool
description: str
EXCEPTIONS = [
ExInfo("APIConnectionError", True, None),
ExInfo("APIError", True, None),
ExInfo("APIResponseValidationError", True, None),
ExInfo("AuthenticationError", True, None),
ExInfo("AzureOpenAIError", True, None),
ExInfo("BadRequestError", True, None),
ExInfo("BudgetExceededError", True, None),
ExInfo("ContentPolicyViolationError", True, None),
ExInfo("ContextWindowExceededError", True, None),
ExInfo("InternalServerError", True, None),
ExInfo("InvalidRequestError", True, None),
ExInfo("JSONSchemaValidationError", True, None),
ExInfo("NotFoundError", True, None),
ExInfo("OpenAIError", True, None),
ExInfo("RateLimitError", True, None),
ExInfo("RouterRateLimitError", True, None),
ExInfo("ServiceUnavailableError", True, None),
ExInfo("UnprocessableEntityError", True, None),
ExInfo("UnsupportedParamsError", True, None),
]
class LiteLLMExceptions:
exceptions = dict()
def __init__(self):
self._load()
def _load(self, strict=False):
import litellm
for var in dir(litellm):
if not var.endswith("Error"):
continue
ex_info = None
for exi in EXCEPTIONS:
if var == exi.name:
ex_info = exi
break
if strict and not ex_info:
raise ValueError(f"{var} is in litellm but not in aider's exceptions list")
ex = getattr(litellm, var)
self.exceptions[ex] = ex_info
def exceptions_tuple(self):
return tuple(self.exceptions)
litellm_ex = LiteLLMExceptions()
litellm_ex._load(strict=True)