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.

5.4 KB
Raw Blame
"""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,
]


@pytest.mark.parametrize("error_type", TIMEOUT_TYPES)
@pytest.mark.parametrize(
    "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


@pytest.mark.asyncio
@pytest.mark.parametrize("surface", ["invoke", "ainvoke"])
@pytest.mark.parametrize("rate_limiting", [False, True])
@pytest.mark.parametrize("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()


@pytest.mark.asyncio
@pytest.mark.parametrize("surface", ["stream", "astream"])
@pytest.mark.parametrize("after_first_chunk", [False, True])
@pytest.mark.parametrize("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]


@pytest.mark.asyncio
@pytest.mark.parametrize("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,
    )