Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: trust explicit HTTP status in retry classification

OrdoAbChao76c04a51a7ead
diff --git a/deeptutor/services/llm/provider_core/base.py b/deeptutor/services/llm/provider_core/base.py
index 964e90e76..490ba5309 100644
--- a/deeptutor/services/llm/provider_core/base.py
+++ b/deeptutor/services/llm/provider_core/base.py
@@ -87,6 +87,23 @@ class LLMProvider(ABC):
"server error",
"temporarily unavailable",
)
+ _RETRYABLE_HTTP_STATUS_CODES = frozenset({408, 429, 500, 502, 503, 504})
+ _HTTP_STATUS_PATTERNS = (
+ re.compile(r"\bHTTP(?:/\d+(?:\.\d+)?)?\s+(?P<code>[1-5]\d{2})\b", re.IGNORECASE),
+ re.compile(
+ r"\b(?:error\s+code|http_status|status(?:\s+code)?|status_code)\s*[:=]\s*['\"]?"
+ r"(?P<code>[1-5]\d{2})\b|"
+ r"['\"](?:code|status|status_code|http_status)['\"]\s*:\s*['\"]?"
+ r"(?P<json_code>[1-5]\d{2})\b",
+ re.IGNORECASE,
+ ),
+ re.compile(
+ r"^\s*(?:Error(?: calling LLM)?[: ]\s*)?(?P<code>[1-5]\d{2})\s+"
+ r"(?:Bad Request|Unauthorized|Forbidden|Not Found|Too Many Requests|"
+ r"Internal Server Error|Bad Gateway|Service Unavailable|Gateway Timeout)\b",
+ re.IGNORECASE,
+ ),
+ )
_SENTINEL = object()
@staticmethod
@@ -267,13 +284,52 @@ class LLMProvider(ABC):
return response
@classmethod
- def _is_transient_error(cls, content: str | None) -> bool:
+ def _status_code_from_exception(cls, exc: Exception) -> int | None:
+ """Return an HTTP status carried by the exception or its response."""
+ response = getattr(exc, "response", None)
+ for value in (getattr(exc, "status_code", None), getattr(response, "status_code", None)):
+ if isinstance(value, bool):
+ continue
+ if isinstance(value, int):
+ status_code = value
+ elif isinstance(value, str) and value.strip().isdigit():
+ status_code = int(value.strip())
+ else:
+ continue
+ if 100 <= status_code <= 599:
+ return status_code
+ return None
+
+ @classmethod
+ def _status_code_from_message(cls, content: str) -> int | None:
+ """Parse only explicit HTTP status fields and conventional status lines."""
+ matches: list[tuple[int, int]] = []
+ for pattern in cls._HTTP_STATUS_PATTERNS:
+ match = pattern.search(content)
+ if match is None:
+ continue
+ raw_code = match.groupdict().get("code") or match.groupdict().get("json_code")
+ if raw_code is not None:
+ matches.append((match.start(), int(raw_code)))
+ return min(matches)[1] if matches else None
+
+ @classmethod
+ def _is_transient_error(
+ cls,
+ content: str | None,
+ *,
+ status_code: int | None = None,
+ exception: Exception | None = None,
+ ) -> bool:
+ if status_code is not None:
+ return status_code in cls._RETRYABLE_HTTP_STATUS_CODES
err = (content or "").lower()
- # Gateway errors often embed opaque request IDs. Matching bare status
- # substrings misclassifies permanent errors when an ID contains "500".
- return bool(re.search(r"\b(?:429|500|502|503|504)\b", err)) or any(
- marker in err for marker in cls._TRANSIENT_ERROR_MARKERS
- )
+ message_status = cls._status_code_from_message(err)
+ if message_status is not None:
+ return message_status in cls._RETRYABLE_HTTP_STATUS_CODES
+ if isinstance(exception, (TimeoutError, ConnectionError)):
+ return True
+ return any(marker in err for marker in cls._TRANSIENT_ERROR_MARKERS)
async def _call_with_retry(
self,
@@ -312,6 +368,8 @@ class LLMProvider(ABC):
while True:
attempt += 1
+ status_code = None
+ caught_exception = None
try:
response = await measured_call(
messages=messages,
@@ -326,6 +384,8 @@ class LLMProvider(ABC):
except asyncio.CancelledError:
raise
except Exception as exc:
+ status_code = self._status_code_from_exception(exc)
+ caught_exception = exc
response = LLMResponse(
content=f"Error calling LLM: {exc}",
finish_reason="error",
@@ -334,7 +394,11 @@ class LLMProvider(ABC):
if response.finish_reason != "error":
return response
- if not self._is_transient_error(response.content):
+ if not self._is_transient_error(
+ response.content,
+ status_code=status_code,
+ exception=caught_exception,
+ ):
# Stage-2 vision fallback: only degrade to text-only when the
# caller opted in (model is *not* in the known-vision
# allowlist). For allowlisted models we trust the images and
diff --git a/tests/services/llm/test_provider_retry_classification.py b/tests/services/llm/test_provider_retry_classification.py
index 39fa38f55..85fa9bc96 100644
--- a/tests/services/llm/test_provider_retry_classification.py
+++ b/tests/services/llm/test_provider_retry_classification.py
@@ -10,13 +10,15 @@ from deeptutor.services.llm.provider_core.base import LLMProvider, LLMResponse
class _FailingProvider(LLMProvider):
- def __init__(self, error: str) -> None:
+ def __init__(self, error: str | Exception) -> None:
super().__init__()
self.error = error
self.calls = 0
async def chat(self, messages: list[dict[str, Any]], **kwargs: Any) -> LLMResponse:
self.calls += 1
+ if isinstance(self.error, Exception):
+ raise self.error
return LLMResponse(content=self.error, finish_reason="error")
def get_default_model(self) -> str:
@@ -32,6 +34,8 @@ class _FailingProvider(LLMProvider):
"invalid model (request id: 1502)",
"invalid parameter (request id: trace503end)",
"invalid request (request id: 1504)",
+ "HTTP 400: invalid request (request id: req-500-abcd)",
+ "HTTP 401: invalid API key (request id: req-503-abcd)",
],
)
async def test_permanent_error_with_status_digits_is_not_retried(error: str) -> None:
@@ -55,12 +59,39 @@ async def test_permanent_error_with_status_digits_is_not_retried(error: str) ->
"request timed out",
"connection reset by peer",
"stream stalled",
+ TimeoutError(),
+ ConnectionResetError("peer reset"),
],
)
-async def test_transient_errors_still_retry(error: str) -> None:
+async def test_transient_errors_still_retry(error: str | Exception) -> None:
provider = _FailingProvider(error)
response = await provider.chat_with_retry(
messages=[{"role": "user", "content": "hello"}], retry_delays=(0.001,)
)
- assert response.content == error
+ expected_content = error if isinstance(error, str) else f"Error calling LLM: {error}"
+ assert response.content == expected_content
assert provider.calls == 2
+
+
+ ("status_code", "message", "expected_calls"),
+ [
+ (400, "HTTP 503: temporary gateway error (request id: req-503-abcd)", 1),
+ (503, "HTTP 400: invalid request", 2),
+ ],
+)
+async def test_structured_status_overrides_message_status(
+ status_code: int, message: str, expected_calls: int
+) -> None:
+ class StructuredError(Exception):
+ pass
+
+ error = StructuredError(message)
+ error.status_code = status_code
+ provider = _FailingProvider(error)
+ response = await provider.chat_with_retry(
+ messages=[{"role": "user", "content": "hello"}], retry_delays=(0.001,)
+ )
+ assert response.finish_reason == "error"
+ assert provider.calls == expected_calls