Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep searching the web past a lone Wikipedia result (#17242)

Nilay100352c7ecba
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index d57c969c275..ef48f97e849 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -164,7 +164,7 @@ _DDGS_CONNECT_TIMEOUT_MARKERS = ("connection timed out", "connecttimeout")
_DDGS_CONNECT_TIMEOUT_LOCK = threading.Lock()
_DDGS_PENDING = threading.local()
-# Tier 2 runs only if tier 1 found nothing; engines in neither tier are never contacted.
+# Tier 2 runs unless tier 1 found more than Wikipedia; engines in neither tier are never contacted.
_SEARCH_ENGINE_TIERS = (
("wikipedia", "brave", "duckduckgo", "mojeek", "startpage"),
("grokipedia", "google", "yahoo"),
@@ -17087,6 +17087,11 @@ def _wikipedia_search(query, max_results, timeout, deadline, cancel_event, websi
]
+def _is_wikipedia_result(result) -> bool:
+ host = urllib.parse.urlsplit(str(result.get("href") or "").strip()).hostname or ""
+ return host == "wikipedia.org" or host.endswith(".wikipedia.org")
+
+
def _usable_search_results(results, website_policy):
from .web_access_policy import check_url_access
return [
@@ -17189,12 +17194,14 @@ def _web_search(
client = DDGS(timeout = remaining)
try:
candidates = _ddgs_text(client, effective_query, wanted, backend)
- results = _usable_search_results(candidates, website_policy)
- rejected_results = rejected_results or bool(candidates and not results)
+ found = _usable_search_results(candidates, website_policy)
+ rejected_results = rejected_results or bool(candidates and not found)
except Exception as exc: # noqa: BLE001 - try the next tier before classifying the failure
last_error = exc
continue
- if results:
+ seen = {r.get("href") for r in results}
+ results += [r for r in found if r.get("href") not in seen]
+ if not all(_is_wikipedia_result(r) for r in results):
break
except Exception as exc:
last_error = exc
diff --git a/studio/backend/tests/test_web_search_tiers.py b/studio/backend/tests/test_web_search_tiers.py
index b195e019732..14318e7b63d 100644
--- a/studio/backend/tests/test_web_search_tiers.py
+++ b/studio/backend/tests/test_web_search_tiers.py
@@ -97,6 +97,29 @@ def test_tier_two_runs_only_after_tier_one_finds_nothing(engine_calls):
assert "URL: https://" in result
+def test_a_lone_wikipedia_hit_still_reaches_tier_two(monkeypatch, engine_calls):
+ _require_two_tiers()
+ engine_calls.install(lambda name: "empty" if name in TIER1 else "results")
+
+ def wikipedia(engine, query, **kwargs):
+ engine_calls.calls.append(("wikipedia", query))
+ return [
+ TextResult(
+ title = "The Bitcoin President",
+ href = "https://en.wikipedia.org/wiki/The_Bitcoin_President",
+ body = "A 2021 documentary film.",
+ )
+ ]
+
+ monkeypatch.setattr(ENGINES["text"]["wikipedia"], "search", wikipedia)
+
+ result = tools.execute_tool("web_search", {"query": "Bitcoin price"})
+
+ assert set(_names(engine_calls)) & set(TIER2), "tier 2 was never reached"
+ assert "https://en.wikipedia.org/wiki/The_Bitcoin_President" in result
+ assert any(f"https://{name}.example/source" in result for name in TIER2)
+
+
def test_a_tier_one_refusal_falls_through_to_tier_two(engine_calls):
_require_two_tiers()
engine_calls.install(lambda name: "raise" if name in TIER1 else "results")