Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(llm): preserve transport timeout errors (#7287)

LearningCircuitdb719b91adbe
diff --git a/changelog.d/+llm-timeout-classification.bugfix.md b/changelog.d/+llm-timeout-classification.bugfix.md
new file mode 100644
index 0000000000..e72881a046
--- /dev/null
+++ b/changelog.d/+llm-timeout-classification.bugfix.md
@@ -0,0 +1 @@
+Preserve transport timeout errors when their messages contain retry advice, so LLM calls do not misreport them as rate limits or retry them as throttled requests.
diff --git a/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py b/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
index 48ad2d41a0..9025a1c180 100644
--- a/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
+++ b/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
@@ -2,6 +2,9 @@
LLM-specific rate limit error detection.
"""
+from httpx import TimeoutException as HTTPXTimeout
+from requests.exceptions import Timeout as RequestsTimeout
+
from ....security.log_sanitizer import scrub_error
from ....security.secure_logging import logger
@@ -27,6 +30,13 @@ def is_llm_rate_limit_error(error: BaseException) -> bool:
)
return True
+ # A transport timeout is not a provider throttling response. Its text
+ # can contain retry advice, a duration such as 429 ms, or rate-limit
+ # wording from the operation that timed out. Preserve the concrete
+ # failure type before applying the fallback message heuristics.
+ if isinstance(error, (TimeoutError, HTTPXTimeout, RequestsTimeout)):
+ return False
+
# Check for common rate limit error messages
rate_limit_indicators = [
"rate limit",
diff --git a/tests/llm/test_provider_adapter_contracts.py b/tests/llm/test_provider_adapter_contracts.py
index 9bcf9c46b8..b4e3880c49 100644
--- a/tests/llm/test_provider_adapter_contracts.py
+++ b/tests/llm/test_provider_adapter_contracts.py
@@ -830,17 +830,6 @@ class TestErrorTaxonomy:
assert is_llm_rate_limit_error(error) is is_rate_limit
- @pytest.mark.xfail(
- strict=True,
- reason=(
- "DEFECT (low severity): is_llm_rate_limit_error matches the "
- "substring 'try again later' anywhere in the message, so a "
- "plain connection timeout is classified as a rate limit — it "
- "is then retried on the provider's adaptive backoff and "
- "recorded as a rate-limit event, distorting the tracker's "
- "pacing for that provider."
- ),
- )
def test_a_timeout_that_says_try_again_is_still_a_timeout(self):
from local_deep_research.web_search_engines.rate_limiting.llm.detection import ( # noqa: E501
is_llm_rate_limit_error,
diff --git a/tests/web_search_engines/rate_limiting/test_llm_timeout_classification.py b/tests/web_search_engines/rate_limiting/test_llm_timeout_classification.py
new file mode 100644
index 0000000000..3491be9c76
--- /dev/null
+++ b/tests/web_search_engines/rate_limiting/test_llm_timeout_classification.py
@@ -0,0 +1,173 @@
+"""Transport timeouts retain their identity through the real LLM wrapper."""
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, Mock
+
+import httpx
+import pytest
+import requests
+
+from local_deep_research.web_search_engines.rate_limiting.llm.detection import (
+ is_llm_rate_limit_error,
+)
+from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
+ create_rate_limited_llm_wrapper,
+)
+
+
+TIMEOUT_TYPES = [
+ TimeoutError,
+ httpx.TimeoutException,
+ httpx.ConnectTimeout,
+ httpx.ReadTimeout,
+ httpx.WriteTimeout,
+ httpx.PoolTimeout,
+ requests.exceptions.Timeout,
+ requests.exceptions.ConnectTimeout,
+ requests.exceptions.ReadTimeout,
+]
+
+WRAPPER_TIMEOUT_TYPES = [
+ TimeoutError,
+ httpx.ReadTimeout,
+ requests.exceptions.ReadTimeout,
+]
+
+
[email protected]("error_type", TIMEOUT_TYPES)
+ "message",
+ [
+ "Read timed out, please try again later",
+ "Read timed out after 429 ms",
+ "Timed out waiting for the rate limit status response",
+ ],
+)
+def test_timeout_type_takes_precedence_over_message_hints(error_type, message):
+ assert is_llm_rate_limit_error(error_type(message)) is False
+
+
+def test_explicit_429_response_still_takes_precedence():
+ # Some adapters attach a response to a more general transport exception.
+ error = requests.exceptions.Timeout("try again later")
+ error.response = requests.Response()
+ error.response.status_code = 429
+
+ assert is_llm_rate_limit_error(error) is True
+
+
[email protected]("surface", ["invoke", "ainvoke"])
[email protected]("rate_limiting", [False, True])
[email protected]("error_type", WRAPPER_TIMEOUT_TYPES)
+async def test_timeout_is_not_wrapped_retried_or_recorded_as_throttling(
+ surface, rate_limiting, error_type
+):
+ error = error_type("Read timed out, please try again later")
+ backend = SimpleNamespace(
+ invoke=Mock(side_effect=error),
+ ainvoke=AsyncMock(side_effect=error),
+ model_name="test-model",
+ )
+ tracker = Mock()
+ tracker.get_wait_time.return_value = 0
+ wrapper = create_rate_limited_llm_wrapper(backend, provider="openai")
+ assert wrapper.rate_limiter is None
+ if rate_limiting:
+ wrapper.rate_limiter = tracker
+
+ with pytest.raises(error_type) as caught:
+ if surface == "invoke":
+ wrapper.invoke("hello")
+ else:
+ await wrapper.ainvoke("hello")
+
+ assert caught.value is error
+ if surface == "invoke":
+ backend.invoke.assert_called_once_with("hello")
+ backend.ainvoke.assert_not_called()
+ else:
+ backend.ainvoke.assert_awaited_once_with("hello")
+ backend.invoke.assert_not_called()
+ tracker.get_wait_time.assert_not_called()
+ tracker.record_outcome.assert_not_called()
+
+
[email protected]("surface", ["stream", "astream"])
[email protected]("after_first_chunk", [False, True])
[email protected]("error_type", WRAPPER_TIMEOUT_TYPES)
+async def test_streaming_timeout_preserves_the_exception_and_closes_source(
+ surface, after_first_chunk, error_type
+):
+ error = error_type("Read timed out, please try again later")
+ closed = []
+
+ def stream(_prompt):
+ try:
+ if after_first_chunk:
+ yield "first chunk"
+ raise error
+ finally:
+ closed.append("stream")
+
+ async def astream(_prompt):
+ try:
+ if after_first_chunk:
+ yield "first chunk"
+ raise error
+ finally:
+ closed.append("astream")
+
+ backend = SimpleNamespace(stream=stream, astream=astream)
+ wrapper = create_rate_limited_llm_wrapper(backend, provider="openai")
+ received = []
+
+ with pytest.raises(error_type) as caught:
+ if surface == "stream":
+ for chunk in wrapper.stream("hello"):
+ received.append(chunk)
+ else:
+ async for chunk in wrapper.astream("hello"):
+ received.append(chunk)
+
+ assert caught.value is error
+ assert received == (["first chunk"] if after_first_chunk else [])
+ assert closed == [surface]
+
+
[email protected]("surface", ["invoke", "ainvoke"])
+async def test_real_http_429_is_retried_and_success_is_recorded(surface):
+ request = httpx.Request("POST", "https://example.invalid/v1/chat")
+ response = httpx.Response(429, request=request)
+ error = httpx.HTTPStatusError(
+ "Please try again later", request=request, response=response
+ )
+ backend = SimpleNamespace(
+ invoke=Mock(side_effect=[error, "answer"]),
+ ainvoke=AsyncMock(side_effect=[error, "answer"]),
+ model_name="test-model",
+ )
+ tracker = Mock()
+ tracker.get_wait_time.return_value = 0
+ wrapper = create_rate_limited_llm_wrapper(backend, provider="openai")
+ wrapper.rate_limiter = tracker
+
+ if surface == "invoke":
+ result = wrapper.invoke("hello")
+ assert backend.invoke.call_count == 2
+ backend.ainvoke.assert_not_called()
+ else:
+ result = await wrapper.ainvoke("hello")
+ assert backend.ainvoke.await_count == 2
+ backend.invoke.assert_not_called()
+
+ assert result == "answer"
+ tracker.get_wait_time.assert_called_once()
+ tracker.record_outcome.assert_called_once_with(
+ engine_type="openai-unknown-test-model",
+ wait_time=0,
+ success=True,
+ retry_count=0,
+ )