Answer
Goal: apply the same simplification pattern used in PR #643 (options extraction) to LLM exception handling, so llm.py stays minimal and readable.
Proposed design
• New submodule: openhands/sdk/llm/exceptions/
◦ types.py
▪︎ Defines SDK exception classes (LLMAuthenticationError, LLMRateLimitError, LLMTimeoutError, LLMServiceUnavailableError, LLMBadRequestError, LLMContextWindowExceedError, LLMNoResponseError).
◦ classifier.py
▪︎ Provider-agnostic detection helpers, e.g.:
◦ is_context_window_exceeded(exc)
◦ is_auth_error(exc)
◦ is_rate_limit(exc)
◦ is_timeout(exc)
◦ is_service_unavailable(exc)
◦ is_bad_request(exc)
▪︎ Encapsulates string-pattern heuristics for providers (e.g., “prompt too long”, 401/403 auth cues).
◦ mapping.py
▪︎ map_provider_exception(exc) -> Exception
▪︎ Uses classifier functions to translate provider/LiteLLM exceptions into SDK-typed exceptions.
▪︎ Returns the original exc if no mapping applies.
◦ retry_policy.py
▪︎ RETRYABLE_PROVIDER_EXCEPTIONS = (APIConnectionError, RateLimitError, ServiceUnavailableError, InternalServerError, Timeout)
▪︎ RETRYABLE_SDK_EXCEPTIONS = (LLMNoResponseError,)
▪︎ RETRYABLE_EXCEPTIONS = RETRYABLE_PROVIDER_EXCEPTIONS + RETRYABLE_SDK_EXCEPTIONS
▪︎ Optional should_retry(exc) for future flexibility.
◦
init.py
▪︎ Re-exports types, classifier entry points, map_provider_exception, and RETRYABLE_EXCEPTIONS.
Minimal changes to llm.py
• Import:
◦ from openhands.sdk.llm.exceptions import (
map_provider_exception,
is_context_window_exceeded,
RETRYABLE_EXCEPTIONS,
)
• Retries:
◦ Use @self.retry_decorator(retry_exceptions=RETRYABLE_EXCEPTIONS, …) for completion() and responses().
• Mapping:
◦ Surround the retried call with try/except and rethrow mapped exceptions:
▪︎ self._telemetry.on_error(e)
▪︎ raise map_provider_exception(e) from e
• Back-compat:
◦ Keep LLM.is_context_window_exceeded_exception as a thin wrapper around classifier.is_context_window_exceeded (deprecate later).
• Agent:
◦ Switch Agent to use classifier.is_context_window_exceeded (or the wrapper during migration).
Testing and docs
• Unit tests:
◦ Classifier: each is_* function with provider-specific patterns.
◦ Mapping: each underlying provider exception maps to the correct SDK exception; unknowns pass through.
◦ Retry policy: decorator retries the intended exceptions; LLMNoResponseError temperature bump behavior maintained.
◦ Integration: verify retry happens first, then final mapped exception is raised with
cause preserved.
• Docs:
◦ “Error handling & retries” section describing typed exceptions, mapping, retry semantics, and how clients can catch specific SDK errors.
This mirrors PR #643’s pattern: move provider-specific normalization/logic into dedicated modules, leaving llm.py as a clean orchestrator.
<sub>
View full conversation</sub>