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.

6.0 KB
Raw Blame
"""Contract tests for ``invoke_llm_sync`` / ``ainvoke_llm`` (#5854).

The two helpers exist so the research pipeline has ONE place that decides
which client an LLM call goes through. What each one must do is a
behavioural contract, not an implementation detail:

* ``invoke_llm_sync`` must stay on the synchronous ``.invoke`` client and
  must not create or run an event loop. Bridging a sync caller onto
  ``ainvoke`` with a throwaway loop is what #6293 documents as
  double-sending (or hard-failing) from the second call onward, because
  langchain caches one ``httpx.AsyncClient`` per process and its
  keep-alive connections are bound to the loop that opened them.
* ``ainvoke_llm`` must await ``.ainvoke`` and must never quietly degrade
  to the sync client, so a misconfigured wrapper is a loud error rather
  than a silent change of transport.
"""

import asyncio
import inspect

import pytest

from local_deep_research.utilities import llm_utils
from local_deep_research.utilities.llm_utils import ainvoke_llm, invoke_llm_sync


class _LoopTripwire(BaseException):
    """Raised if the helper touches the event loop machinery.

    Derived from ``BaseException`` rather than ``Exception`` so the broad
    ``except Exception`` handlers around production call sites cannot
    swallow it if this stub is ever reused there.
    """


class _RecordingLLM:
    """Stub model recording both transports separately."""

    def __init__(self, result="response"):
        self.result = result
        self.invoke_calls = []
        self.ainvoke_calls = []

    def invoke(self, prompt):
        self.invoke_calls.append(prompt)
        return self.result

    async def ainvoke(self, prompt):
        self.ainvoke_calls.append(prompt)
        return self.result


class _SyncOnlyLLM:
    """A wrapped object that never got an ``ainvoke`` — a config error."""

    def __init__(self):
        self.invoke_calls = []

    def invoke(self, prompt):
        self.invoke_calls.append(prompt)
        return "sync"


@pytest.fixture
def no_event_loop(monkeypatch):
    """Trip if anything starts or creates an event loop."""

    def _tripwire(*args, **kwargs):
        raise _LoopTripwire("the sync helper started an event loop")

    monkeypatch.setattr(asyncio, "run", _tripwire)
    monkeypatch.setattr(asyncio, "new_event_loop", _tripwire)
    monkeypatch.setattr(asyncio.events, "new_event_loop", _tripwire)
    return _tripwire


def test_invoke_llm_sync_uses_the_sync_client_only(no_event_loop):
    """It calls ``.invoke``, leaves ``.ainvoke`` untouched, starts no loop."""
    llm = _RecordingLLM(result="answer")

    assert invoke_llm_sync(llm, "prompt") == "answer"

    assert llm.invoke_calls == ["prompt"]
    assert llm.ainvoke_calls == [], (
        "invoke_llm_sync reached the async client; that is the #6293 "
        "loop-bound-client hazard"
    )


def test_invoke_llm_sync_second_consecutive_call_succeeds(no_event_loop):
    """The failure mode #6293 describes only shows up on call #2.

    A per-call ``asyncio.run`` bridge passes a single-call test and then
    raises ``RuntimeError: Event loop is closed`` on the next one, so the
    second call is the assertion that matters.
    """
    llm = _RecordingLLM(result="answer")

    assert invoke_llm_sync(llm, "first") == "answer"
    assert invoke_llm_sync(llm, "second") == "answer"

    assert llm.invoke_calls == ["first", "second"]
    assert llm.ainvoke_calls == []


def test_invoke_llm_sync_returns_the_raw_response(no_event_loop):
    """No unwrapping: callers do their own ``.content`` / JSON parsing."""
    sentinel = object()
    llm = _RecordingLLM(result=sentinel)

    assert invoke_llm_sync(llm, "prompt") is sentinel


def test_invoke_llm_sync_propagates_model_errors(no_event_loop):
    """Errors reach the caller's own except-block unchanged."""

    class _Boom:
        def invoke(self, prompt):
            raise ValueError("model exploded")

    with pytest.raises(ValueError, match="model exploded"):
        invoke_llm_sync(_Boom(), "prompt")


def test_invoke_llm_sync_works_inside_a_running_loop():
    """Callable from a coroutine (``news_strategy.analyze_findings``).

    A sync call is legal there; ``asyncio.run`` inside a running loop is
    not, which is the second reason the helper stays on ``.invoke``.
    """
    llm = _RecordingLLM(result="answer")

    async def _driver():
        return invoke_llm_sync(llm, "prompt")

    assert asyncio.run(_driver()) == "answer"
    assert llm.invoke_calls == ["prompt"]
    assert llm.ainvoke_calls == []


def test_invoke_llm_sync_is_not_a_coroutine_function():
    """Guards against the helper being turned back into an async bridge."""
    assert not inspect.iscoroutinefunction(invoke_llm_sync)


@pytest.mark.asyncio
async def test_ainvoke_llm_awaits_the_async_client():
    """The async core awaits ``.ainvoke`` and never touches ``.invoke``."""
    llm = _RecordingLLM(result="answer")

    assert await ainvoke_llm(llm, "prompt") == "answer"

    assert llm.ainvoke_calls == ["prompt"]
    assert llm.invoke_calls == [], (
        "ainvoke_llm fell back to the sync client; a transport change "
        "must not happen silently"
    )


@pytest.mark.asyncio
async def test_ainvoke_llm_returns_the_raw_response():
    sentinel = object()
    llm = _RecordingLLM(result=sentinel)

    assert await ainvoke_llm(llm, "prompt") is sentinel


@pytest.mark.asyncio
async def test_ainvoke_llm_raises_when_the_model_has_no_ainvoke():
    """A sync-only object is a configuration error, not a fallback.

    Every production model is a ``ProcessingLLMWrapper``, which always
    defines ``ainvoke``. Falling back to ``.invoke`` here would block the
    awaiting loop and hide the misconfiguration.
    """
    llm = _SyncOnlyLLM()

    with pytest.raises(AttributeError):
        await ainvoke_llm(llm, "prompt")

    assert llm.invoke_calls == []


def test_helpers_are_exported():
    """Both names are part of the module's public surface."""
    assert "invoke_llm_sync" in llm_utils.__all__
    assert "ainvoke_llm" in llm_utils.__all__