File size: 12,622 Bytes
4d2a2da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""Tests for create_llm_with_fallback and the fallback chain runtime behaviour."""

from dataclasses import replace
from unittest.mock import patch

import pytest
from langchain_core.language_models.fake_chat_models import FakeListChatModel
from langchain_core.runnables import RunnableLambda

import src.provider as provider_module
from src.config import load_settings


def _base_settings():  # noqa: ANN202
    """Return a Settings instance with fallback fields overridable for tests."""
    return load_settings()


def test_fallback_disabled_returns_plain_llm() -> None:
    settings = replace(
        _base_settings(),
        llm_fallback_enabled=False,
        llm_fallback_providers=("openai",),
    )
    fake = FakeListChatModel(responses=["hello"])
    with patch.object(provider_module, "create_llm", return_value=fake) as m:
        result = provider_module.create_llm_with_fallback(settings)
    # Exactly one LLM constructed: the primary.
    assert m.call_count == 1
    # The returned object is the plain fake — no with_fallbacks wrapper.
    assert result is fake


def test_fallback_enabled_but_empty_list_returns_plain_llm() -> None:
    settings = replace(
        _base_settings(),
        llm_fallback_enabled=True,
        llm_fallback_providers=(),
    )
    fake = FakeListChatModel(responses=["hello"])
    with patch.object(provider_module, "create_llm", return_value=fake):
        result = provider_module.create_llm_with_fallback(settings)
    assert result is fake


def test_fallback_chain_invokes_fallback_on_transient_error(caplog) -> None:  # noqa: ANN001
    """Primary raises ConnectionError → fallback is used and success is returned."""
    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("fallback_stub",),
    )

    primary = RunnableLambda(lambda _x: (_ for _ in ()).throw(ConnectionError("down")))
    fallback = FakeListChatModel(responses=["rescued"])

    def fake_create(s):  # noqa: ANN001, ANN202
        if s.llm_provider == "primary_stub":
            return primary
        return fallback

    import logging
    caplog.set_level(logging.WARNING, logger="src.provider")
    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        chain = provider_module.create_llm_with_fallback(settings)

    # Startup warning about the chain must be emitted.
    assert any("LLM fallback chain is ACTIVE" in r.message for r in caplog.records)

    # Invoking the chain transparently recovers via the fallback.
    result = chain.invoke("hi")
    # FakeListChatModel returns an AIMessage whose content is the response.
    assert getattr(result, "content", result) == "rescued"

    # Trigger-time warning must have fired when the fallback was used.
    assert any("fallback activated" in r.message.lower() for r in caplog.records)


def test_broken_fallback_provider_is_skipped(caplog) -> None:  # noqa: ANN001
    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("broken", "good"),
    )
    primary = FakeListChatModel(responses=["primary"])
    good_fallback = FakeListChatModel(responses=["good"])

    def fake_create(s):  # noqa: ANN001, ANN202
        if s.llm_provider == "primary_stub":
            return primary
        if s.llm_provider == "broken":
            raise RuntimeError("cannot construct")
        return good_fallback

    import logging
    caplog.set_level(logging.ERROR, logger="src.provider")
    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        chain = provider_module.create_llm_with_fallback(settings)

    assert any("Skipping LLM fallback provider 'broken'" in r.message for r in caplog.records)
    # Chain should still be usable (wraps primary + good).
    assert chain is not primary  # with_fallbacks wrapped the result


def test_streaming_pre_stream_failure_engages_fallback() -> None:
    """When primary fails before yielding any tokens, fallback streams cleanly.

    This is the expected happy path: the streaming entry point (e.g. a
    connection refused at request time) raises before any token leaves the
    primary, so ``with_fallbacks`` transparently substitutes the fallback
    and the caller sees exactly one clean stream.
    """
    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("fallback_stub",),
    )

    # Primary throws on both invoke and stream — simulates full outage.
    primary = RunnableLambda(
        lambda _x: (_ for _ in ()).throw(ConnectionError("primary down"))
    )
    fallback = FakeListChatModel(responses=["rescued"])

    def fake_create(s):  # noqa: ANN001, ANN202
        return primary if s.llm_provider == "primary_stub" else fallback

    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        chain = provider_module.create_llm_with_fallback(settings)

    chunks = list(chain.stream("hi"))
    joined = "".join(getattr(c, "content", str(c)) for c in chunks)
    # The fallback's single response is streamed character-by-character by
    # FakeListChatModel, so the joined output must equal the response exactly
    # — no duplicated tokens from the primary.
    assert joined == "rescued"


def test_streaming_mid_stream_failure_is_not_caught_by_fallback() -> None:
    """Mid-stream failures propagate; fallback is not engaged.

    This documents a real limitation of ``RunnableWithFallbacks``: it only
    catches exceptions raised when the stream is OPENED, not exceptions
    raised DURING iteration. If the primary yields some tokens and then
    the connection dies, the caller receives those partial tokens followed
    by the original exception — NOT a seamless switch to the fallback.

    This guards against silently relying on fallback to cover mid-stream
    outages; it cannot.
    """
    from langchain_core.messages import AIMessageChunk
    from langchain_core.runnables import Runnable

    class PartialThenFail(Runnable):
        def invoke(self, input, config=None, **kwargs):  # noqa: ANN001, A002
            raise ConnectionError("primary has no invoke")

        def stream(self, input, config=None, **kwargs):  # noqa: ANN001, A002
            yield AIMessageChunk(content="partial-")
            raise ConnectionError("mid-stream outage")

    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("fallback_stub",),
    )
    primary = PartialThenFail()
    fallback = FakeListChatModel(responses=["rescued"])

    def fake_create(s):  # noqa: ANN001, ANN202
        return primary if s.llm_provider == "primary_stub" else fallback

    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        chain = provider_module.create_llm_with_fallback(settings)

    observed: list[str] = []
    with pytest.raises(ConnectionError):
        for chunk in chain.stream("hi"):
            observed.append(chunk.content)

    # Partial token was delivered to the caller before the failure bubbled up.
    assert observed == ["partial-"]


def test_streaming_integration_with_query_router_uses_fallback(monkeypatch) -> None:  # noqa: ANN001
    """End-to-end: QueryRouter.route_stream survives a primary LLM outage.

    Wires a fallback-wrapped LLM into a real QueryRouter and asserts the
    SSE event stream completes with a ``done`` event carrying the fallback's
    answer. This proves the fallback integrates with the generation node's
    downstream ``StrOutputParser`` and with the router's streaming path.
    """
    from langchain_core.messages import AIMessage
    from langchain_core.output_parsers import StrOutputParser

    from src.agent.router import QueryRouter
    from src.models import IntentType, QueryResult, DocumentChunk

    # Build the fallback-wrapped LLM.
    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("fallback_stub",),
    )
    primary = RunnableLambda(
        lambda _x: (_ for _ in ()).throw(ConnectionError("primary down"))
    )
    fallback = RunnableLambda(lambda _x: AIMessage(content="rescued answer"))

    def fake_create(s):  # noqa: ANN001, ANN202
        return primary if s.llm_provider == "primary_stub" else fallback

    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        llm = provider_module.create_llm_with_fallback(settings)

    # Stub the intent classifier so we don't need to drive language detection.
    class _StubClassifier:
        def classify(self, _query: str) -> IntentType:
            return IntentType.FACTUAL

    # Stub retriever + reranker so they return a single fake result.
    chunk = DocumentChunk(
        chunk_id="c1",
        document_id="doc1.pdf",
        text="Sample context.",
        metadata={"page_number": 1, "chunk_index": 0},
    )
    fake_qr = QueryResult(chunk=chunk, score=0.9, source="dense")

    class _StubHybridResult:
        def __init__(self):  # noqa: ANN204
            self.dense_results = [fake_qr]
            self.sparse_results = [fake_qr]
            self.fused_results = [fake_qr]

    class _StubHybrid:
        def __init__(self):  # noqa: ANN204
            self.vector_store = _StubVectorStore()
        def search_detailed(self, _q: str, top_k: int = 5) -> _StubHybridResult:
            return _StubHybridResult()

    class _StubVectorStore:
        def list_document_ids(self) -> list[str]:
            return []

    class _StubReranker:
        def rerank(self, _q: str, results, top_k: int = 5):  # noqa: ANN001, ANN201
            return list(results)[:top_k]

    router = QueryRouter(
        intent_classifier=_StubClassifier(),
        hybrid_retriever=_StubHybrid(),
        reranker=_StubReranker(),
        llm_chain=llm | StrOutputParser(),
        translate_query=False,
        document_languages=["Danish"],
    )

    events = list(router.route_stream("Hvor mange feriedage?", top_k=3))
    done = [e for e in events if e["step"] == "done"]
    assert done, f"expected a 'done' event, got steps={[e['step'] for e in events]}"
    assert done[0]["result"]["answer"] == "rescued answer"


def test_fallback_engages_on_sdk_style_exception() -> None:
    """Fallback must engage on arbitrary Exception subclasses.

    Real-world LLM SDK exceptions (openai.RateLimitError, httpx.ConnectError,
    etc.) do not inherit from stdlib ConnectionError / TimeoutError / OSError.
    Using a narrow exception tuple would silently make the fallback chain a
    no-op for these cases. This test simulates one of them with a custom
    Exception subclass that has no relation to ConnectionError.
    """

    class FakeRateLimitError(Exception):
        """Stand-in for openai.RateLimitError / anthropic.APIError."""

    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("fallback_stub",),
    )

    primary = RunnableLambda(
        lambda _x: (_ for _ in ()).throw(FakeRateLimitError("429 Too Many Requests"))
    )
    fallback = FakeListChatModel(responses=["rescued"])

    def fake_create(s):  # noqa: ANN001, ANN202
        return primary if s.llm_provider == "primary_stub" else fallback

    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        chain = provider_module.create_llm_with_fallback(settings)

    result = chain.invoke("hi")
    assert getattr(result, "content", result) == "rescued"


def test_all_fallbacks_broken_returns_primary_only(caplog) -> None:  # noqa: ANN001
    settings = replace(
        _base_settings(),
        llm_provider="primary_stub",
        llm_fallback_enabled=True,
        llm_fallback_providers=("broken",),
    )
    primary = FakeListChatModel(responses=["primary"])

    def fake_create(s):  # noqa: ANN001, ANN202
        if s.llm_provider == "primary_stub":
            return primary
        raise RuntimeError("nope")

    import logging
    caplog.set_level(logging.WARNING, logger="src.provider")
    with patch.object(provider_module, "create_llm", side_effect=fake_create):
        result = provider_module.create_llm_with_fallback(settings)

    assert result is primary
    assert any(
        "no fallback providers could be constructed" in r.message
        for r in caplog.records
    )