~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
"""The #3878 friendly-error rewrite, driven through its real call site.
``run_research_process``'s ``except Exception as search_error`` block
rewrites OpenAI-compatible client failures into a message naming the
provider, base URL and model -- but only for providers in
``_OPENAI_COMPAT_RUNTIME_ERROR_PROVIDERS``, and with the base URL resolved
by ``_resolve_error_rewrite_base_url``. The unit tests in
``tests/error_handling/test_openai_compat_errors.py`` pin the set and the
resolver in isolation; this module pins that the worker actually wires them
together.
The rewritten text is server-side detail: the outer handler replaces it
with a scrubbed category message before anything reaches the client
(CWE-209), so the observable output is the ``Research failed: ...`` log
line. It is captured with a loguru sink.
What breaks if this regresses: a QwenCloud (or DeepSeek, Requesty) outage
is logged as a generic "Connection error" with no provider/endpoint, or --
when chat/follow-up pass ``llm.openai_endpoint.url`` (default: the
OpenRouter URL) as ``custom_endpoint`` -- it is logged against an endpoint
the client never contacted.
Network: ``get_llm`` and ``AdvancedSearchSystem`` are mocked, and the
connection error is constructed locally, so nothing is contacted.
"""
from contextlib import ExitStack
from unittest.mock import MagicMock, patch
import httpx
import pytest
from loguru import logger
from openai import (
APIConnectionError,
APITimeoutError,
AuthenticationError,
BadRequestError,
NotFoundError,
PermissionDeniedError,
)
from local_deep_research.web.services import research_service
from tests.web.services.helpers import (
MODULE,
QUEUE_PROC_MOD,
_base_run_patches,
_egress_and_search_patches,
_get_raw_run_research_process,
)
QWEN_URL = "https://maas.qwencloudapi.com/compatible-mode/v1"
OPENROUTER_URL = "https://openrouter.ai/api/v1"
def _connection_error():
return APIConnectionError(
message="Connection error.",
request=httpx.Request("POST", f"{QWEN_URL}/chat/completions"),
)
def _status_error(cls, status):
request = httpx.Request("POST", f"{QWEN_URL}/chat/completions")
return cls(
message=f"upstream said {status}",
response=httpx.Response(status, request=request),
body=None,
)
_OMIT = object()
def _run_worker_logging(
*,
provider,
model,
custom_endpoint,
error=None,
all_messages=None,
get_llm=None,
):
"""Drive the real worker with ``analyze_topic`` raising an
OpenAI-compatible error (default: a connection error); return the
``Research failed`` log lines and the metadata queued for the client.
``all_messages``, when a list, receives every captured log line."""
system = MagicMock()
system.analyze_topic.side_effect = (
error if error is not None else _connection_error()
)
patches = _base_run_patches()
patches[f"{MODULE}.get_llm"] = get_llm or MagicMock(
return_value=MagicMock()
)
patches[f"{MODULE}.AdvancedSearchSystem"] = MagicMock(return_value=system)
snapshot = {
"llm.provider": provider,
"llm.model": model,
"search.tool": "searxng",
# The stale value chat/follow-up used to forward for every provider.
"llm.openai_endpoint.url": OPENROUTER_URL,
}
messages = [] if all_messages is None else all_messages
# The package disables its loguru logging on import (see
# ``local_deep_research/__init__.py``); re-enable it for the capture and
# restore the default afterwards, as the ``loguru_caplog`` fixture does.
logger.enable("local_deep_research")
sink_id = logger.add(
lambda m: messages.append(m.record["message"]), level="DEBUG"
)
try:
with ExitStack() as stack:
for cm in _egress_and_search_patches():
stack.enter_context(cm)
for target, mock_obj in patches.items():
stack.enter_context(patch(target, mock_obj))
_get_raw_run_research_process()(
1,
"test query",
"quick",
username="user1",
settings_snapshot=snapshot,
search_engine="searxng",
model_provider=provider,
model=model,
**(
{}
if custom_endpoint is _OMIT
else {"custom_endpoint": custom_endpoint}
),
)
finally:
logger.remove(sink_id)
logger.disable("local_deep_research")
assert system.analyze_topic.called, (
"the worker never reached analyze_topic -- the search-error path "
"did not run, so nothing below is being tested"
)
failed = [m for m in messages if m.startswith("Research failed:")]
assert failed, "the outer handler never logged the failure"
queue = patches[f"{QUEUE_PROC_MOD}.queue_processor"]
assert queue.queue_error_update.called, "no error update was queued"
return failed, queue.queue_error_update.call_args.kwargs["metadata"]
@pytest.mark.parametrize(
"custom_endpoint",
[None, OPENROUTER_URL],
ids=["no-custom-endpoint", "stale-openai-endpoint-url"],
)
def test_qwen_connection_error_is_rewritten_with_its_real_endpoint(
custom_endpoint,
):
"""QwenCloud's failure is rewritten (it is in the provider set) and
names its own QwenCloud endpoint -- not ``<unknown>`` and not the
OpenRouter URL that ``llm.openai_endpoint.url`` defaults to."""
# Premise: the patch targets below resolve against the module that
# holds the worker.
assert research_service.__name__ == MODULE
failed, _metadata = _run_worker_logging(
provider="qwen", model="qwen3.8-max", custom_endpoint=custom_endpoint
)
text = "\n".join(failed)
assert "Error type: openai_connection_refused" in text, text
# The connection-refused wording names provider and endpoint (the model
# only appears for model-specific failures such as a 404).
assert "Cannot reach qwen at " + QWEN_URL in text, text
assert "<unknown>" not in text
assert "openrouter.ai" not in text
def test_client_gets_provider_neutral_category_message_and_hint():
"""The client never sees the rewritten detail (CWE-209); it gets the
category message and ``solution``. Those now reach cloud providers
(QwenCloud, DeepSeek, OpenRouter, ...) too, so they name the configured
endpoint and only mention local servers as an example."""
_failed, metadata = _run_worker_logging(
provider="qwen", model="qwen3.8-max", custom_endpoint=None
)
assert metadata["error"] == (
"Could not connect to the configured LLM endpoint."
)
solution = metadata["solution"]
assert "configured LLM endpoint is reachable" in solution
assert not solution.startswith("Start your LLM server")
# CWE-209: neither the endpoint nor the raw detail reaches the client.
assert QWEN_URL not in str(metadata)
assert "Details:" not in str(metadata)
def test_legacy_uppercase_provider_still_gets_the_rewrite():
"""A provider stored before normalisation (``"QWEN"``) must still be
matched against the lower-case provider set and rewritten."""
failed, _metadata = _run_worker_logging(
provider="QWEN", model="qwen3.8-max", custom_endpoint=None
)
text = "\n".join(failed)
assert "Error type: openai_connection_refused" in text, text
assert "Cannot reach qwen at " + QWEN_URL in text, text
def test_research_parameters_log_redacts_the_custom_endpoint():
"""The INFO "Research parameters" line logs the per-run endpoint; a
credential embedded in it (userinfo, ``?api_key=``) must not reach the
log."""
endpoint = "https://user:[email protected]/v1?api_key=sk-leak123&v=1"
messages = []
_run_worker_logging(
provider="openai_endpoint",
model="m",
custom_endpoint=endpoint,
all_messages=messages,
)
params = [m for m in messages if m.startswith("Research parameters:")]
assert params, "the worker never logged its parameters"
# The worker preserves main's stricter policy: retain the origin only,
# so arbitrary path/query values never reach this diagnostic.
assert "custom_endpoint=https://gw.example," in params[0], params[0]
assert "/v1" not in params[0], params[0]
assert "api_key=" not in params[0], params[0]
for message in messages:
assert "sk-leak123" not in message
assert "pw-leak" not in message
def test_research_parameters_log_keeps_absent_endpoint_as_none():
messages = []
_run_worker_logging(
provider="qwen",
model="qwen3.8-max",
custom_endpoint=None,
all_messages=messages,
)
params = [m for m in messages if m.startswith("Research parameters:")]
assert "custom_endpoint=None," in params[0], params[0]
@pytest.mark.parametrize(
("error", "expected_message"),
[
(
APITimeoutError(
request=httpx.Request("POST", f"{QWEN_URL}/chat/completions")
),
"The configured LLM endpoint timed out.",
),
(
_status_error(AuthenticationError, 401),
"Authentication with the configured LLM provider failed.",
),
(
_status_error(PermissionDeniedError, 403),
"The configured LLM provider denied access to the model.",
),
(
_status_error(NotFoundError, 404),
"The configured model was not found at the LLM endpoint.",
),
(
_status_error(BadRequestError, 400),
"The LLM endpoint rejected the request.",
),
],
ids=[
"timeout",
"auth",
"permission_denied",
"model_not_found",
"bad_request",
],
)
def test_other_categories_are_provider_neutral(error, expected_message):
"""The other five categories, as a cloud provider's client sees them:
timeout, model-not-found and bad-request were reworded to name the
configured endpoint; auth and permission-denied already named the
configured provider and only their hints changed. None of them
mentions a local or LLM server."""
_failed, metadata = _run_worker_logging(
provider="qwen",
model="qwen3.8-max",
custom_endpoint=None,
error=error,
)
assert metadata["error"] == expected_message
client_text = f"{metadata['error']} {metadata['solution']}".lower()
assert "local server" not in client_text
assert "llm server" not in client_text
assert "loaded on this server" not in client_text
assert "account/server" not in client_text
assert QWEN_URL not in str(metadata)
def test_settings_fallback_endpoint_matches_legacy_uppercase_provider():
"""With no per-run ``custom_endpoint`` kwarg, the worker falls back to
``llm.openai_endpoint.url`` for the openai_endpoint provider and hands
it to ``get_llm`` -- also when the provider is stored in legacy upper
case."""
get_llm = MagicMock(return_value=MagicMock())
_run_worker_logging(
provider="OPENAI_ENDPOINT",
model="m",
custom_endpoint=_OMIT,
get_llm=get_llm,
)
assert get_llm.called
assert get_llm.call_args.kwargs["openai_endpoint_url"] == OPENROUTER_URL