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.

9.2 KB
Raw Blame
"""``llm.request_timeout`` / ``llm.max_retries`` also bound embeddings.

The setting documents itself as covering "LLM inference and embedding
requests ... applied to every supported provider". Before this, both
embedding clients were built with ``Timeout(timeout=None)`` — langchain
passes an explicit ``None`` that overrides the SDK's own default — so a
stalled embedding call held an indexing worker forever, and the OpenAI
model-discovery client had no timeout or retry cap at all.
"""

from unittest.mock import MagicMock, patch

import openai
import pytest

from local_deep_research.embeddings.providers.implementations.ollama import (
    OllamaEmbeddingsProvider,
)
from local_deep_research.embeddings.providers.implementations.openai import (
    OpenAIEmbeddingsProvider,
)
from local_deep_research.llm.providers._helpers import (
    MODEL_DISCOVERY_TIMEOUT_SECONDS,
)
from local_deep_research.settings.exceptions import SettingsValueError

_OLLAMA_MODULE = (
    "local_deep_research.embeddings.providers.implementations.ollama"
)
_OPENAI_MODULE = (
    "local_deep_research.embeddings.providers.implementations.openai"
)


def _settings(overrides=None):
    values = {
        "embeddings.ollama.model": "nomic-embed-text",
        "embeddings.ollama.num_ctx": 8192,
        "embeddings.openai.model": "text-embedding-3-small",
        "embeddings.openai.api_key": "test-key",
    }
    values.update(overrides or {})
    return values


def _side_effect(values):
    def _get(key, default=None, *args, **kwargs):
        return values.get(key, default)

    return _get


class TestOllamaEmbeddingsTimeout:
    @pytest.mark.parametrize(
        ("configured", "expected"),
        # Missing uses the default; the 1s floor is valid; 10**9 is
        # honoured, since the upper bound was removed deliberately.
        [(None, 1800.0), (1, 1.0), (37, 37.0), (10**9, 1.0e9)],
    )
    def test_client_kwargs_carry_a_bounded_timeout(self, configured, expected):
        # Given
        values = _settings(
            {} if configured is None else {"llm.request_timeout": configured}
        )

        # When
        with patch(
            f"{_OLLAMA_MODULE}.get_setting_from_snapshot",
            side_effect=_side_effect(values),
        ):
            with patch(
                f"{_OLLAMA_MODULE}.get_ollama_base_url",
                return_value="http://localhost:11434",
            ):
                with patch(f"{_OLLAMA_MODULE}.OllamaEmbeddings") as mock_cls:
                    OllamaEmbeddingsProvider.create_embeddings(
                        settings_snapshot=values
                    )

        # Then
        timeout = mock_cls.call_args[1]["client_kwargs"]["timeout"]
        assert timeout.read == expected
        assert timeout.write == expected
        assert timeout.pool == expected
        # Literal, not the imported constant: raising CONNECT_TIMEOUT_SECONDS
        # is a behaviour change and must break a test, not ride along.
        assert timeout.connect == min(5.0, expected)

    def test_zero_timeout_is_rejected_before_client_construction(self):
        values = _settings({"llm.request_timeout": 0})

        with patch(
            f"{_OLLAMA_MODULE}.get_setting_from_snapshot",
            side_effect=_side_effect(values),
        ):
            with patch(
                f"{_OLLAMA_MODULE}.get_ollama_base_url",
                return_value="http://localhost:11434",
            ):
                with patch(f"{_OLLAMA_MODULE}.OllamaEmbeddings") as mock_cls:
                    with pytest.raises(SettingsValueError) as raised:
                        OllamaEmbeddingsProvider.create_embeddings(
                            settings_snapshot=values
                        )

        assert raised.value.key == "llm.request_timeout"
        assert raised.value.source == "snapshot"
        mock_cls.assert_not_called()


class TestOpenAIEmbeddingsTimeout:
    def test_request_timeout_and_retries_are_bounded(self):
        # Given
        values = _settings({"llm.request_timeout": 45, "llm.max_retries": 1})

        # When
        with patch(
            f"{_OPENAI_MODULE}.get_setting_from_snapshot",
            side_effect=_side_effect(values),
        ):
            with patch(
                "langchain_openai.OpenAIEmbeddings", return_value=MagicMock()
            ) as mock_cls:
                OpenAIEmbeddingsProvider.create_embeddings(
                    settings_snapshot=values
                )

        # Then
        kwargs = mock_cls.call_args[1]
        # An SDK ``Timeout`` object, not the chat path's hashable tuple:
        # ``OpenAIEmbeddings`` never consults langchain's ``@lru_cache``'d
        # ``_get_default_httpx_client``, so each instance owns its httpx
        # clients whatever the timeout's type, and the object keeps the
        # SDK's ``x-stainless-read-timeout`` header well formed.
        # ``openai.Timeout`` is the httpx flavour the openai SDK ships
        # (httpx2). Reverting this call site to ``build_timeout`` fails
        # the isinstance assertion. See ``_helpers.build_httpx_timeout``.
        timeout = kwargs["request_timeout"]
        assert isinstance(timeout, openai.Timeout)
        assert (timeout.connect, timeout.read, timeout.write, timeout.pool) == (
            5.0,
            45,
            45,
            45,
        )
        assert kwargs["max_retries"] == 1

    def test_model_discovery_is_bounded_without_sdk_retries(self):
        # Given
        values = _settings({"embeddings.openai.base_url": None})
        fake_client = MagicMock()
        fake_client.models.list.return_value = MagicMock(data=[])

        # When
        with patch(
            f"{_OPENAI_MODULE}.get_setting_from_snapshot",
            side_effect=_side_effect(values),
        ):
            with patch(
                "openai.OpenAI", return_value=fake_client
            ) as mock_openai:
                OpenAIEmbeddingsProvider.get_available_models(
                    settings_snapshot=values
                )

        # Then
        kwargs = mock_openai.call_args.kwargs
        # Fresh client per call, outside langchain's cached factory, so
        # the SDK's own ``Timeout`` object rather than the chat path's
        # tuple. Literals, not the imported constant: changing the
        # discovery budget is a behaviour change and must break a test
        # rather than ride along on the import.
        timeout = kwargs["timeout"]
        assert isinstance(timeout, openai.Timeout)
        assert (timeout.connect, timeout.read, timeout.write, timeout.pool) == (
            5.0,
            30,
            30,
            30,
        )
        assert MODEL_DISCOVERY_TIMEOUT_SECONDS == 30
        assert kwargs["max_retries"] == 0


class TestOllamaEmbeddingsAuthHeaders:
    """The chat path routes ``llm.ollama.api_key`` through client_kwargs.

    Embeddings talk to the same authenticated Ollama instance through the
    same single key setting, so indexing used to 401 while chat worked.
    ``OllamaEmbeddings`` has no ``headers`` field either, so client_kwargs
    is the only route. Dropping the ``build_bearer_header`` block in
    ``embeddings/providers/implementations/ollama.py`` fails both tests
    below.
    """

    def _create(self, values, *, patch_cls):
        """``build_bearer_header`` reads the snapshot dict directly."""
        with patch(
            f"{_OLLAMA_MODULE}.get_setting_from_snapshot",
            side_effect=_side_effect(values),
        ):
            with patch(
                f"{_OLLAMA_MODULE}.get_ollama_base_url",
                return_value="http://localhost:11434",
            ):
                if not patch_cls:
                    return OllamaEmbeddingsProvider.create_embeddings(
                        settings_snapshot=values
                    )
                with patch(f"{_OLLAMA_MODULE}.OllamaEmbeddings") as mock_cls:
                    OllamaEmbeddingsProvider.create_embeddings(
                        settings_snapshot=values
                    )
                    return mock_cls.call_args[1]

    def test_the_configured_key_becomes_a_bearer_header(self):
        # Given
        values = _settings({"llm.ollama.api_key": "secret-token"})

        # When
        kwargs = self._create(values, patch_cls=True)

        # Then
        assert kwargs["client_kwargs"]["headers"] == {
            "Authorization": "Bearer secret-token"
        }
        assert "headers" not in kwargs

    def test_no_key_configured_adds_no_header(self):
        # Given — bare local Ollama needs no auth
        values = _settings()

        # When
        kwargs = self._create(values, patch_cls=True)

        # Then
        assert "headers" not in kwargs["client_kwargs"]

    def test_the_header_reaches_both_real_httpx_clients(self):
        """End-to-end against the real OllamaEmbeddings, no network."""
        # Given
        values = _settings({"llm.ollama.api_key": "secret-token"})

        # When
        instance = self._create(values, patch_cls=False)

        # Then
        for client in (instance._client, instance._async_client):
            assert (
                client._client.headers.get("authorization")
                == "Bearer secret-token"
            )
            assert client._client.timeout.read == 1800
            assert client._client.timeout.connect == 5.0