Files
DeepTutor/deeptutor/services/llm/error_mapping.py
evan188199-tech c72185aad6 fix(rag): improve local LlamaIndex pipeline compatibility (#864)
* fix(rag): improve local pipeline compatibility

* fix(rag): resolve tqdm progress at call time, not at import

`show_progress: bool = should_show_progress()` is a default argument, so
Python evaluated it once when the module was first imported and froze the
result for the process lifetime. That defeated the fix in both directions:
a server started from a terminal froze `True` and kept writing tqdm bars
into the pipe whose read end can close mid-indexing (the BrokenPipeError
this guards against), while a CLI launched with piped stdout froze `False`
and never showed progress again.

Take `show_progress: bool | None = None` and resolve inside the body of the
two functions that consume the value; the pure forwarders pass `None`
through so the decision is made once, at the point of use.

The accompanying test was green only because pytest captures stdout, so the
frozen default already read `False` — it never exercised the mechanism. It
now asserts both directions and fails under `pytest -s` if the default
argument comes back.

---------

Co-authored-by: XZH <cto@xzh.ai>
Co-authored-by: Frank <bingxizhao@novix.science>
2026-08-24 16:34:55 +08:00

167 lines
5.2 KiB
Python

"""
Error Mapping - Map provider-specific errors to unified exceptions.
"""
from __future__ import annotations
import asyncio
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timezone
from email.utils import parsedate_to_datetime
import logging
import math
# Import unified exceptions from exceptions.py
from .exceptions import (
LLMAPIError,
LLMAuthenticationError,
LLMError,
LLMRateLimitError,
LLMTimeoutError,
ProviderContextWindowError,
)
logger = logging.getLogger(__name__)
ErrorClassifier = Callable[[Exception], bool]
@dataclass(frozen=True)
class MappingRule:
classifier: ErrorClassifier
factory: Callable[[Exception, str | None], LLMError]
def _instance_of(*types: type[BaseException]) -> ErrorClassifier:
return lambda exc: isinstance(exc, types)
def _message_contains(*needles: str) -> ErrorClassifier:
def _classifier(exc: Exception) -> bool:
msg = str(exc).lower()
return any(needle in msg for needle in needles)
return _classifier
def _class_named(*names: str) -> ErrorClassifier:
"""Match optional SDK exceptions without importing the SDK at startup."""
expected = set(names)
def _classifier(exc: Exception) -> bool:
return any(cls.__name__ in expected for cls in type(exc).__mro__)
return _classifier
def retry_after_seconds(
exc: Exception,
*,
now: datetime | None = None,
) -> float | None:
"""Extract a numeric or HTTP-date Retry-After value from an exception."""
value = getattr(exc, "retry_after", None)
if value is None:
response = getattr(exc, "response", None)
headers = getattr(response, "headers", None)
get_header = getattr(headers, "get", None)
if callable(get_header):
value = get_header("Retry-After")
if value is None:
value = get_header("retry-after")
if isinstance(value, bool) or value is None:
return None
if isinstance(value, (int, float)):
seconds = float(value)
elif isinstance(value, str):
normalized = value.strip()
if not normalized:
return None
try:
seconds = float(normalized)
except ValueError:
try:
retry_at = parsedate_to_datetime(normalized)
except (TypeError, ValueError, OverflowError):
return None
if retry_at.tzinfo is None:
retry_at = retry_at.replace(tzinfo=timezone.utc)
current_time = now or datetime.now(timezone.utc)
if current_time.tzinfo is None:
current_time = current_time.replace(tzinfo=timezone.utc)
return max(0.0, (retry_at - current_time).total_seconds())
else:
return None
if not math.isfinite(seconds) or seconds < 0:
return None
return seconds
def _rate_limit_error(exc: Exception, provider: str | None) -> LLMRateLimitError:
"""Map rate-limit errors without discarding their server retry hint."""
return LLMRateLimitError(
str(exc),
retry_after=retry_after_seconds(exc),
provider=provider,
)
_GLOBAL_RULES: list[MappingRule] = [
MappingRule(
classifier=_instance_of(asyncio.TimeoutError, TimeoutError),
factory=lambda exc, provider: LLMTimeoutError(
str(exc) or "Request timed out", provider=provider
),
),
MappingRule(
classifier=_class_named("AuthenticationError", "AuthenticationStatusError"),
factory=lambda exc, provider: LLMAuthenticationError(str(exc), provider=provider),
),
MappingRule(
classifier=_class_named("RateLimitError"),
factory=_rate_limit_error,
),
MappingRule(
classifier=_message_contains("rate limit", "429", "quota"),
factory=_rate_limit_error,
),
MappingRule(
classifier=_message_contains("context length", "maximum context"),
factory=lambda exc, provider: ProviderContextWindowError(str(exc), provider=provider),
),
]
def map_error(exc: Exception, provider: str | None = None) -> LLMError:
"""Map provider-specific errors to unified internal exceptions.
An exception that is already an ``LLMError`` is returned *itself*, not a
replacement: re-running the rules would re-classify a precise error into a
vaguer one (an ``LLMTimeoutError`` fell out as a bare ``LLMAPIError``), and
callers such as the LightRAG adapter re-raise the terminal error and
compare identity. Filling in ``provider`` when the raiser did not know it is
therefore an in-place write on the caller's exception — deliberate, and the
only field this function mutates.
"""
if isinstance(exc, LLMError):
if exc.provider is None:
exc.provider = provider
return exc
# Heuristic check for status codes before rules
status_code = getattr(exc, "status_code", None)
if status_code == 401:
return LLMAuthenticationError(str(exc), provider=provider)
if status_code == 429:
return _rate_limit_error(exc, provider)
for rule in _GLOBAL_RULES:
if rule.classifier(exc):
return rule.factory(exc, provider)
return LLMAPIError(str(exc), status_code=status_code, provider=provider)