From 8bc9ebf2aa689ad11a13f2bd795ba46252c8b1da Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 7 Nov 2024 12:45:27 -0800 Subject: [PATCH] feat: add LiteLLM exception handling with ExInfo dataclass --- aider/exceptions.py | 62 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/aider/exceptions.py b/aider/exceptions.py index fe88fc9a9..e3346916f 100644 --- a/aider/exceptions.py +++ b/aider/exceptions.py @@ -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)