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.

fix(search-engine-searxng): preserve word boundaries in parsed titles (#5022)

S70cee77764eb
diff --git a/changelog.d/4970.bugfix.md b/changelog.d/4970.bugfix.md
new file mode 100644
index 0000000000..352c9f5932
--- /dev/null
+++ b/changelog.d/4970.bugfix.md
@@ -0,0 +1 @@
+Fix collapsed-whitespace bug in SearXNG-sourced titles. The HTML parser was calling `BeautifulSoup.Tag.get_text(strip=True)` to read result titles, which strips leading/trailing whitespace *and* collapses every internal whitespace run to nothing — so multi-word titles like "Word One Word Two Word Three" rendered as "WordOneWordTwoWordThree" in the `## Sources` block. Swapped to `get_text(" ", strip=True)` (the documented BeautifulSoup idiom for "strip edges, replace internal runs with a single space") so word boundaries survive. The same fix was applied to result snippets. Title preservation flows through to the LLM relevance filter and `BaseCandidateExplorer` / `ProgressiveExplorer` candidate-phrase extraction, both of which previously saw single-token titles from SearXNG-sourced collections.
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py b/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
index 48c46278fa..e27e959afa 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
@@ -352,7 +352,7 @@ class SearXNGSearchEngine(BaseSearchEngine):
)
title = (
- title_element.get_text(strip=True)
+ title_element.get_text(" ", strip=True)
if title_element
else ""
)
@@ -364,7 +364,7 @@ class SearXNGSearchEngine(BaseSearchEngine):
url = url_element.get_text(strip=True)
content = (
- content_element.get_text(strip=True)
+ content_element.get_text(" ", strip=True)
if content_element
else ""
)
diff --git a/tests/web_search_engines/engines/test_search_engine_searxng.py b/tests/web_search_engines/engines/test_search_engine_searxng.py
index de91f3076b..a5c6a4d522 100644
--- a/tests/web_search_engines/engines/test_search_engine_searxng.py
+++ b/tests/web_search_engines/engines/test_search_engine_searxng.py
@@ -507,6 +507,89 @@ class TestGetSearchResults:
assert results[0]["title"] == "Result 1"
assert results[0]["url"] == "https://example.com/page1"
+ def test_title_preserves_internal_whitespace(self):
+ """Multi-word titles keep word boundaries (issue #4970).
+
+ ``Tag.get_text(strip=True)`` collapses every internal whitespace
+ run to nothing, so titles like ``Word One Word Two Word Three``
+ render as ``WordOneWordTwoWordThree``. ``get_text(" ", strip=True)``
+ replaces internal runs with a single space instead.
+ """
+ from local_deep_research.web_search_engines.engines.search_engine_searxng import (
+ SearXNGSearchEngine,
+ )
+
+ # Real SearXNG output often splits a multi-word title across
+ # several child nodes (e.g. spans holding separate words), so
+ # the parse path concatenates their text. Leading/trailing
+ # whitespace inside the anchor also exercises edge stripping.
+ html_content = """
+ <html>
+ <body>
+ <article class="result">
+ <h3 class="result-title">
+ <a href="https://example.com/word-one">
+ <em>Word One</em> of
+ <em>Word Two Word Three</em>
+ at ExampleSite
+ </a>
+ </h3>
+ <p class="content">
+ <em>A</em> <em>multi-line</em>
+ <em>snippet</em> <em>with</em>
+ <em>extra</em> spaces.
+ </p>
+ </article>
+ </body>
+ </html>
+ """
+
+ with patch(
+ "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
+ ) as mock_get:
+ mock_response_init = Mock()
+ mock_response_init.status_code = 200
+ mock_response_init.cookies = {}
+
+ mock_response_search = Mock()
+ mock_response_search.status_code = 200
+ mock_response_search.text = html_content
+ mock_response_search.cookies = {}
+
+ mock_get.side_effect = [
+ mock_response_init,
+ mock_response_init,
+ mock_response_search,
+ ]
+
+ engine = SearXNGSearchEngine(
+ instance_url="http://localhost:8080",
+ )
+
+ results = engine._get_search_results("word one two three")
+
+ assert len(results) == 1
+ title = results[0]["title"]
+ # The whitespace-collapsed bug would produce
+ # "WordOneofWordTwoWordThreeatExampleSite" with no spaces.
+ assert title == (
+ "Word One of Word Two Word Three at ExampleSite"
+ ), f"Title should keep internal spaces, got {title!r}"
+ assert " " not in title, (
+ "Internal runs must collapse to a single space"
+ )
+ assert title == title.strip(), "Edges must be stripped"
+
+ content = results[0]["content"]
+ assert content.startswith("A multi-line"), content
+ assert content.rstrip().endswith("extra spaces."), content
+ # Word boundary across the newline must survive — the
+ # old ``get_text(strip=True)`` joined "multi-line" with
+ # "snippet" into "multi-linesnippet".
+ assert "A multi-line snippet" in content, (
+ f"Cross-fragment word boundary lost: {content!r}"
+ )
+
class TestGetPreviews:
"""Tests for _get_previews method."""