Pass litellm error types to user and update error message (#7344)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan
2025-03-19 10:44:30 -04:00
committed by GitHub
parent 6824d14ed8
commit cbc26a5e40

View File

@@ -4,12 +4,19 @@ import os
import traceback
from typing import Callable, ClassVar, Type
import litellm
from litellm.exceptions import (
import litellm # noqa
from litellm.exceptions import ( # noqa
APIConnectionError,
APIError,
AuthenticationError,
BadRequestError,
ContextWindowExceededError,
InternalServerError,
NotFoundError,
OpenAIError,
RateLimitError,
ServiceUnavailableError,
Timeout,
)
from openhands.controller.agent import Agent
@@ -223,20 +230,20 @@ class AgentController:
await self.set_agent_state_to(AgentState.ERROR)
if self.status_callback is not None:
err_id = ''
if isinstance(e, litellm.AuthenticationError):
if isinstance(e, AuthenticationError):
err_id = 'STATUS$ERROR_LLM_AUTHENTICATION'
elif isinstance(
e,
(
litellm.ServiceUnavailableError,
litellm.APIConnectionError,
litellm.APIError,
ServiceUnavailableError,
APIConnectionError,
APIError,
),
):
err_id = 'STATUS$ERROR_LLM_SERVICE_UNAVAILABLE'
elif isinstance(e, litellm.InternalServerError):
elif isinstance(e, InternalServerError):
err_id = 'STATUS$ERROR_LLM_INTERNAL_SERVER_ERROR'
elif isinstance(e, litellm.BadRequestError) and 'ExceededBudget' in str(e):
elif isinstance(e, BadRequestError) and 'ExceededBudget' in str(e):
err_id = 'STATUS$ERROR_LLM_OUT_OF_CREDITS'
elif isinstance(e, RateLimitError):
await self.set_agent_state_to(AgentState.RATE_LIMITED)
@@ -256,18 +263,24 @@ class AgentController:
f'Traceback: {traceback.format_exc()}',
)
reported = RuntimeError(
'There was an unexpected error while running the agent. Please '
'report this error to the developers by opening an issue at '
'https://github.com/All-Hands-AI/OpenHands. Your session ID is '
f' {self.id}. Error type: {e.__class__.__name__}'
f'There was an unexpected error while running the agent: {e.__class__.__name__}. You can refresh the page or ask the agent to try again.'
)
if (
isinstance(e, litellm.AuthenticationError)
or isinstance(e, litellm.BadRequestError)
isinstance(e, Timeout)
or isinstance(e, APIError)
or isinstance(e, BadRequestError)
or isinstance(e, NotFoundError)
or isinstance(e, InternalServerError)
or isinstance(e, AuthenticationError)
or isinstance(e, RateLimitError)
or isinstance(e, LLMContextWindowExceedError)
):
reported = e
else:
self.log(
'warning',
f'Unknown exception type while running the agent: {type(e).__name__}.',
)
await self._react_to_exception(reported)
def should_step(self, event: Event) -> bool: