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.

test(error_handling): pin load-bearing branches in openai_compat_errors (#4074)

LearningCircuit6e37c248e407
diff --git a/src/local_deep_research/error_handling/openai_compat_errors.py b/src/local_deep_research/error_handling/openai_compat_errors.py
index d6aefdfa77..b27588cb38 100644
--- a/src/local_deep_research/error_handling/openai_compat_errors.py
+++ b/src/local_deep_research/error_handling/openai_compat_errors.py
@@ -37,6 +37,13 @@ def _strip_credentials(base_url: str | None) -> str:
if not parsed.netloc:
return base_url
host = parsed.hostname or ""
+ # urlparse exposes IPv6 hostnames without their surrounding brackets;
+ # re-add them when reassembling the netloc, or the rebuilt URL is
+ # not parseable by downstream HTTP libraries (e.g. ``http://::1:8080/``
+ # is ambiguous: is the host ``::`` and the port ``1:8080``?). IPv4
+ # never contains ``:`` so this heuristic is safe.
+ if ":" in host:
+ host = f"[{host}]"
if parsed.port:
host = f"{host}:{parsed.port}"
return urlunparse(parsed._replace(netloc=host)) or "<unknown>"
diff --git a/tests/error_handling/test_openai_compat_errors.py b/tests/error_handling/test_openai_compat_errors.py
index a010dcc14d..c30d9bef6d 100644
--- a/tests/error_handling/test_openai_compat_errors.py
+++ b/tests/error_handling/test_openai_compat_errors.py
@@ -316,3 +316,120 @@ class TestAdditionalDispatch:
)
assert "Error type: openai_unknown" in msg
assert "Details: just a value error" in msg
+
+
+# ---------------------------------------------------------------------------
+# Edge cases pinned by this PR
+# ---------------------------------------------------------------------------
+
+
+class TestDispatchOrderingTimeoutBeforeConnection:
+ """``openai.APITimeoutError`` subclasses ``APIConnectionError`` in
+ openai>=1.x, so the dispatch table at openai_compat_errors.py:87 must
+ check the timeout branch BEFORE the connection branch — otherwise
+ every timeout would be mislabelled as ``openai_connection_refused``.
+
+ The comment at lines 85-86 documents this constraint; this test pins it.
+
+ Mutation: swap the two ``if`` blocks (lines 87-92 and 95-100) so the
+ connection branch runs first, and ``APITimeoutError`` instances get the
+ wrong token.
+ """
+
+ def test_timeout_subclass_dispatches_to_timeout_branch_not_connection(
+ self,
+ ):
+ # Sanity: confirm the subclassing assumption that motivates the
+ # ordering. If openai breaks this hierarchy in a future major
+ # release, this assertion fails first and the test author can
+ # decide whether the ordering rule still applies.
+ assert issubclass(APITimeoutError, APIConnectionError)
+
+ exc = APITimeoutError(request=_req())
+ msg = friendly_openai_compatible_error(
+ exc,
+ provider="lmstudio",
+ base_url="http://localhost:1234/v1",
+ model="m",
+ )
+ assert "Error type: openai_timeout" in msg
+ assert "Error type: openai_connection_refused" not in msg
+
+
+class TestWalkCauseChainPreference:
+ """``_walk_cause`` traverses ``cur.__cause__ or cur.__context__`` —
+ i.e. explicit ``raise X from Y`` chains win over implicit
+ ``__context__`` chains. Pins the preference.
+
+ Mutation: swap to ``cur.__context__ or cur.__cause__`` at
+ openai_compat_errors.py:60 — the test would point at the wrong root.
+ """
+
+ def test_cause_preferred_over_context_when_both_set(self):
+ explicit_root = APIConnectionError(message="conn", request=_req())
+ implicit_root = RuntimeError("implicit context")
+
+ wrapper = RuntimeError("wrapper")
+ wrapper.__cause__ = explicit_root
+ wrapper.__context__ = implicit_root
+
+ assert _walk_cause(wrapper) is explicit_root
+
+
+class TestStripCredentialsEdgeCases:
+ def test_ipv6_host_brackets_preserved(self):
+ # ``urlparse`` returns ``hostname`` without brackets; the
+ # implementation reassembles ``netloc`` and must re-add the
+ # brackets, or the resulting URL is unparseable downstream —
+ # ``http://::1:8080/`` is ambiguous about where the host ends
+ # and the port begins.
+ result = _strip_credentials("http://[::1]:8080/v1")
+ assert result == "http://[::1]:8080/v1"
+
+ def test_userinfo_stripped_with_ipv6_host(self):
+ # Combine userinfo and IPv6 host. The key must be removed AND
+ # the brackets must survive.
+ result = _strip_credentials(
+ "http://user:secret-key-12345@[::1]:8080/v1"
+ )
+ assert "secret-key-12345" not in result
+ assert "user:" not in result
+ assert result == "http://[::1]:8080/v1"
+
+ def test_url_with_no_netloc_passed_through(self):
+ # A bare path (no scheme/netloc) is returned as-is. The function
+ # short-circuits at ``if not parsed.netloc:``. Worth pinning so
+ # someone "fixing" the path-only case doesn't break it.
+ assert _strip_credentials("/v1/chat/completions") == (
+ "/v1/chat/completions"
+ )
+
+
+class TestFriendlyErrorNoneArgs:
+ """``friendly_openai_compatible_error`` uses ``provider or
+ "<unknown provider>"`` and ``model or "<unspecified>"`` to surface a
+ legible message even when the caller doesn't know the values. Pin
+ both placeholders so future refactors don't drop them.
+ """
+
+ def test_none_provider_replaced_with_placeholder(self):
+ msg = friendly_openai_compatible_error(
+ APIConnectionError(message="conn", request=_req()),
+ provider=None,
+ base_url="http://localhost:9999/v1",
+ model="m",
+ )
+ assert "<unknown provider>" in msg
+
+ def test_none_model_replaced_with_placeholder(self):
+ msg = friendly_openai_compatible_error(
+ NotFoundError(
+ message="missing",
+ response=_resp(404),
+ body=None,
+ ),
+ provider="lmstudio",
+ base_url="http://localhost:1234/v1",
+ model=None,
+ )
+ assert "<unspecified>" in msg