~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(llm): log streaming failure types (#7289)
LearningCircuita36f8f05c43c
diff --git a/changelog.d/+llm-stream-diagnostics.bugfix.md b/changelog.d/+llm-stream-diagnostics.bugfix.mdnew file mode 100644index 0000000000..5e05ef0469--- /dev/null+++ b/changelog.d/+llm-stream-diagnostics.bugfix.md@@ -0,0 +1 @@+Log the exception type when a synchronous or asynchronous LLM stream fails, while preserving the original error and stream cleanup.diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.pyindex 44e1e3eb7f..d04522d83d 100644--- a/src/local_deep_research/config/llm_config.py+++ b/src/local_deep_research/config/llm_config.py@@ -6,6 +6,7 @@ from loguru import loggerfrom ..llm import get_llm_from_registry, is_llm_registeredfrom ..security.log_sanitizer import sanitize_for_log, scrub_error+from ..security.secure_logging import log_exception_typefrom ..utilities.search_utilities import remove_think_tags# Import providers module to trigger auto-discovery. get_llm() has no@@ -1024,12 +1025,18 @@ class ProcessingLLMWrapper:research_id = self._effective_research_id()if research_id:self._check_terminated(research_id)- gen = self.base_llm.stream(*args, **kwargs)+ gen = Nonetry:+ gen = self.base_llm.stream(*args, **kwargs)for chunk in gen:if research_id:self._check_terminated(research_id)yield chunk+ except Exception as error:+ log_exception_type(+ "LLM Request - Failed with error in stream()", error+ )+ raisefinally:try:close = getattr(gen, "close", None)@@ -1045,12 +1052,18 @@ class ProcessingLLMWrapper:research_id = self._effective_research_id()if research_id:self._check_terminated(research_id)- agen = self.base_llm.astream(*args, **kwargs)+ agen = Nonetry:+ agen = self.base_llm.astream(*args, **kwargs)async for chunk in agen:if research_id:self._check_terminated(research_id)yield chunk+ except Exception as error:+ log_exception_type(+ "LLM Request - Failed with error in astream()", error+ )+ raisefinally:try:aclose = getattr(agen, "aclose", None)diff --git a/tests/llm/test_processing_stream_diagnostics.py b/tests/llm/test_processing_stream_diagnostics.pynew file mode 100644index 0000000000..b8c96fe7ed--- /dev/null+++ b/tests/llm/test_processing_stream_diagnostics.py@@ -0,0 +1,213 @@+"""Stream failure diagnostics must preserve provider and cleanup contracts."""++import asyncio+from types import SimpleNamespace++from loguru import logger+import pytest++from local_deep_research.config.llm_config import ProcessingLLMWrapper+++class UnrenderableProviderError(Exception):+ def __str__(self):+ raise AssertionError("Provider error text must not be rendered")+++class ProbeStream:+ def __init__(self, *, error=None, after_chunk=False, close_error=False):+ self.error = error+ self.after_chunk = after_chunk+ self.close_error = close_error+ self.chunk = object()+ self.yielded = False+ self.closed = 0++ def __iter__(self):+ return self++ def __next__(self):+ if not self.yielded and (self.error is None or self.after_chunk):+ self.yielded = True+ return self.chunk+ if self.error is not None:+ raise self.error+ raise StopIteration++ def __aiter__(self):+ return self++ async def __anext__(self):+ try:+ return next(self)+ except StopIteration:+ raise StopAsyncIteration from None++ def close(self):+ self.closed += 1+ if self.close_error:+ raise RuntimeError("cleanup detail sentinel")++ async def aclose(self):+ self.close()+++def stream_events(loguru_caplog_full):+ events = []+ sink = logger.add(+ events.append,+ level="DEBUG",+ format="{message}\n{exception}",+ filter=lambda record: (+ record["name"] == "local_deep_research.config.llm_config"+ ),+ )+ try:+ yield events+ finally:+ logger.remove(sink)+++def make_wrapper(probe, *, construction_error=None):+ calls = []++ def start(*args, **kwargs):+ calls.append((args, kwargs))+ if construction_error is not None:+ raise construction_error+ return probe++ return ProcessingLLMWrapper(+ SimpleNamespace(stream=start, astream=start)+ ), calls+++async def consume(wrapper, mode, chunks, config):+ if mode == "sync":+ for chunk in wrapper.stream("prompt", config=config):+ chunks.append(chunk)+ else:+ async for chunk in wrapper.astream("prompt", config=config):+ chunks.append(chunk)++[email protected]("mode", ["sync", "async"])+ "phase", ["construction", "before_chunk", "after_chunk"]+)[email protected]("diagnose", ["0", "1"])+async def test_failure_logs_type_and_preserves_original_error(+ mode, phase, diagnose, monkeypatch, stream_events+):+ monkeypatch.setenv("LDR_APP_DEBUG", diagnose)+ monkeypatch.setenv("LDR_LOGURU_DIAGNOSE", diagnose)+ error = UnrenderableProviderError("provider detail sentinel")+ error.response = SimpleNamespace(status_code=429)+ error.add_note("provider note sentinel")+ error.__cause__ = RuntimeError("provider cause sentinel")+ probe = ProbeStream(error=error, after_chunk=phase == "after_chunk")+ wrapper, calls = make_wrapper(+ probe, construction_error=error if phase == "construction" else None+ )+ chunks = []+ config = {"tags": ["caller-tag"]}++ with pytest.raises(UnrenderableProviderError) as caught:+ await consume(wrapper, mode, chunks, config)++ assert caught.value is error+ assert caught.value.response.status_code == 429+ assert chunks == ([probe.chunk] if phase == "after_chunk" else [])+ assert probe.closed == (0 if phase == "construction" else 1)+ assert calls == [(("prompt",), {"config": config})]+ assert calls[0][1]["config"] is config+ assert len(stream_events) == 1+ record = stream_events[0].record+ assert record["level"].name == "ERROR"+ assert record["function"] == ("stream" if mode == "sync" else "astream")+ assert "LLM Request - Failed with error" in record["message"]+ assert "UnrenderableProviderError" in record["message"]+ assert "sentinel" not in str(stream_events[0])+ assert record["exception"] is None+ assert all(value is not error for value in record["extra"].values())++[email protected]("mode", ["sync", "async"])+async def test_cleanup_failure_does_not_replace_provider_failure(+ mode, stream_events+):+ error = UnrenderableProviderError("provider detail sentinel")+ probe = ProbeStream(error=error, after_chunk=True, close_error=True)+ wrapper, _ = make_wrapper(probe)+ chunks = []++ with pytest.raises(UnrenderableProviderError) as caught:+ await consume(wrapper, mode, chunks, {})++ assert caught.value is error+ assert chunks == [probe.chunk]+ assert probe.closed == 1+ failures = [e for e in stream_events if e.record["level"].name == "ERROR"]+ assert len(failures) == 1+ assert all(e.record["exception"] is None for e in stream_events)+ assert all("sentinel" not in str(e) for e in stream_events)++[email protected]("mode", ["sync", "async"])[email protected]("abort_type", [GeneratorExit, asyncio.CancelledError])+async def test_abort_propagates_and_closes_without_failure_event(+ mode, abort_type, stream_events+):+ abort = abort_type("abort detail sentinel")+ probe = ProbeStream(error=abort, after_chunk=True)+ wrapper, _ = make_wrapper(probe)+ chunks = []++ with pytest.raises(abort_type) as caught:+ await consume(wrapper, mode, chunks, {})++ assert caught.value is abort+ assert chunks == [probe.chunk]+ assert probe.closed == 1+ assert stream_events == []++[email protected]("mode", ["sync", "async"])+async def test_success_forwards_chunks_and_closes_without_failure_event(+ mode, stream_events+):+ probe = ProbeStream()+ wrapper, _ = make_wrapper(probe)+ chunks = []++ await consume(wrapper, mode, chunks, {})++ assert chunks == [probe.chunk]+ assert probe.closed == 1+ assert stream_events == []++[email protected]("mode", ["sync", "async"])+async def test_consumer_close_cleans_up_without_failure_event(+ mode, stream_events+):+ probe = ProbeStream(error=UnrenderableProviderError(), after_chunk=True)+ wrapper, _ = make_wrapper(probe)++ if mode == "sync":+ stream = wrapper.stream("prompt")+ assert next(stream) is probe.chunk+ stream.close()+ else:+ stream = wrapper.astream("prompt")+ assert await anext(stream) is probe.chunk+ await stream.aclose()++ assert probe.closed == 1+ assert stream_events == []diff --git a/tests/llm/test_provider_adapter_contracts.py b/tests/llm/test_provider_adapter_contracts.pyindex b4e3880c49..3743a4da03 100644--- a/tests/llm/test_provider_adapter_contracts.py+++ b/tests/llm/test_provider_adapter_contracts.py@@ -418,16 +418,6 @@ class TestCredentialsAtCallTime:assert excinfo.value is plantedassert excinfo.value.response.status_code == 429- @pytest.mark.xfail(- strict=True,- reason=(- "DEFECT: ProcessingLLMWrapper defines invoke/ainvoke but not "- "stream/astream, so a streamed call's exception falls through "- "__getattr__ to the base LLM: no scrubbed log line at all. "- "Streaming and non-streaming disagree on error handling, and "- "the streaming half is the one with no scrubbing."- ),- )def test_stream_failure_is_scrub_logged_like_invoke(self, captured_logs):planted = _Boom(f"502 from gateway: Authorization: Bearer {FAKE_KEY}")wrapper = _wrapper_over(planted)