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(synthesis): strip <think> in synthesize_findings + knowledge gen; guard empty extractor answers (#4336)

LearningCircuitb6a9ba026a58
diff --git a/changelog.d/+synthesis-think-tag-leak-and-empty-answer.bugfix.md b/changelog.d/+synthesis-think-tag-leak-and-empty-answer.bugfix.md
new file mode 100644
index 0000000000..21063dca07
--- /dev/null
+++ b/changelog.d/+synthesis-think-tag-leak-and-empty-answer.bugfix.md
@@ -0,0 +1 @@
+Strip `<think>` reasoning blocks from the main synthesis path (`synthesize_findings` and the standard knowledge generator), not just the citation handlers — reasoning-model output no longer leaks `<think>…</think>` into final answers. Also fixes precision/forced answer extractors emitting a stray `". <content>"` when the model returns an empty (or think-only) response.
diff --git a/src/local_deep_research/advanced_search_system/findings/repository.py b/src/local_deep_research/advanced_search_system/findings/repository.py
index 5812069304..96e4db6caf 100644
--- a/src/local_deep_research/advanced_search_system/findings/repository.py
+++ b/src/local_deep_research/advanced_search_system/findings/repository.py
@@ -8,6 +8,7 @@ from typing import Dict, List, Union
from langchain_core.documents import Document
from langchain_core.language_models import BaseLanguageModel
+from ...utilities.json_utils import get_llm_response_text
from ...utilities.search_utilities import format_findings
from .base_findings import BaseFindingsRepository
@@ -362,12 +363,8 @@ Use IEEE style citations [1], [2], etc. Never make up your own citations.
120, self.model.invoke, prompt
)
- # Handle different response types (string or object with content attribute)
- if hasattr(response, "content"):
- synthesized_content = response.content
- else:
- # Handle string responses
- synthesized_content = str(response)
+ # Normalize response (handles str/.content, strips <think> tags)
+ synthesized_content = get_llm_response_text(response)
logger.info(
f"Successfully synthesized final answer for query: '{query}'"
@@ -402,12 +399,10 @@ Use IEEE style citations [1], [2], etc. Never make up your own citations.
):
response = self.model.invoke(prompt)
- # Handle different response types (string or object with content attribute)
- if hasattr(response, "content"):
- synthesized_content = response.content
- else:
- # Handle string responses
- synthesized_content = str(response)
+ # Normalize response (handles str/.content, strips <think> tags)
+ synthesized_content = get_llm_response_text(
+ response
+ )
logger.info(
f"Successfully synthesized final answer for query: '{query}'"
diff --git a/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py b/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
index 4bc67123ea..bcb996faf8 100644
--- a/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
+++ b/src/local_deep_research/advanced_search_system/knowledge/standard_knowledge.py
@@ -6,6 +6,7 @@ from loguru import logger
from datetime import datetime, UTC
from typing import List
+from ...utilities.json_utils import get_llm_response_text
from .base_knowledge import BaseKnowledgeGenerator
@@ -58,8 +59,7 @@ Generate detailed knowledge that:
Format your response as a well-structured paragraph."""
- response = self.model.invoke(prompt)
- knowledge = response.content
+ knowledge = get_llm_response_text(self.model.invoke(prompt))
logger.info("Generated knowledge successfully")
return knowledge
@@ -90,8 +90,7 @@ Generate detailed knowledge that:
Format your response as a well-structured paragraph."""
try:
- response = self.model.invoke(prompt)
- return response.content
+ return get_llm_response_text(self.model.invoke(prompt))
except Exception:
logger.exception("Error generating sub-knowledge")
return ""
@@ -128,8 +127,9 @@ Accumulated Knowledge:
Compressed Knowledge:"""
try:
- response = self.model.invoke(prompt)
- compressed_knowledge = response.content
+ compressed_knowledge = get_llm_response_text(
+ self.model.invoke(prompt)
+ )
logger.info(
f"Compressed knowledge length: {len(compressed_knowledge)}"
)
diff --git a/src/local_deep_research/citation_handlers/forced_answer_citation_handler.py b/src/local_deep_research/citation_handlers/forced_answer_citation_handler.py
index 47294570fe..bffe9d7cb1 100644
--- a/src/local_deep_research/citation_handlers/forced_answer_citation_handler.py
+++ b/src/local_deep_research/citation_handlers/forced_answer_citation_handler.py
@@ -183,6 +183,8 @@ Answer:"""
try:
answer = self._invoke_text(extraction_prompt)
+ if not answer:
+ return content
# Format as a proper response
return f"{answer}. Based on the available sources, this appears to be the most likely answer. {content}"
diff --git a/src/local_deep_research/citation_handlers/precision_extraction_handler.py b/src/local_deep_research/citation_handlers/precision_extraction_handler.py
index 063f9c049d..00add888b1 100644
--- a/src/local_deep_research/citation_handlers/precision_extraction_handler.py
+++ b/src/local_deep_research/citation_handlers/precision_extraction_handler.py
@@ -304,6 +304,8 @@ The single answer is:"""
try:
answer = self._invoke_text(extraction_prompt)
+ if not answer:
+ return content
# Clean up the answer
answer = answer.split(",")[
@@ -482,6 +484,8 @@ The answer is:"""
try:
answer = self._invoke_text(extraction_prompt)
+ if not answer:
+ return f"{scores[0][0]}-{scores[0][1]}. {content}"
return f"{answer}. {content}"
except Exception:
# Return first score found if LLM extraction fails
@@ -506,6 +510,8 @@ The answer is:"""
try:
answer = self._invoke_text(extraction_prompt)
+ if not answer:
+ return f"{years[0]}. {content}"
# Clean to just the year/date
year_match = self.answer_patterns["year"].search(answer)
if year_match:
@@ -535,6 +541,8 @@ The answer is:"""
try:
answer = self._invoke_text(extraction_prompt)
+ if not answer:
+ return f"{numbers[0]}. {content}"
return f"{answer}. {content}"
except Exception:
# Fallback to first found number if LLM extraction fails
diff --git a/tests/advanced_search_system/findings/test_findings_repository.py b/tests/advanced_search_system/findings/test_findings_repository.py
index 6b06c2bb87..5f356b7838 100644
--- a/tests/advanced_search_system/findings/test_findings_repository.py
+++ b/tests/advanced_search_system/findings/test_findings_repository.py
@@ -129,6 +129,44 @@ class TestSynthesizeFindings:
assert result == "Synthesized answer"
+ def test_strips_think_tags_from_synthesis(self):
+ """synthesize_findings strips <think> reasoning from the final answer."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(
+ content="<think>weighing the evidence</think>The final answer is X [1]."
+ )
+
+ repo = FindingsRepository(mock_model)
+ result = repo.synthesize_findings(
+ query="Test query",
+ sub_queries=[],
+ findings=[{"content": "Finding 1"}],
+ )
+
+ assert "<think>" not in result
+ assert "weighing the evidence" not in result
+ assert result == "The final answer is X [1]."
+
+ def test_synthesizes_string_response(self):
+ """synthesize_findings handles a raw string model return (no .content)."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = "Raw string answer [1]."
+
+ repo = FindingsRepository(mock_model)
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=["Finding 1"]
+ )
+
+ assert result == "Raw string answer [1]."
+
def test_synthesizes_string_findings(self):
"""Test synthesizing string findings."""
from local_deep_research.advanced_search_system.findings.repository import (
diff --git a/tests/advanced_search_system/knowledge/test_standard_knowledge.py b/tests/advanced_search_system/knowledge/test_standard_knowledge.py
index 8f82712c7f..fa7bea1052 100644
--- a/tests/advanced_search_system/knowledge/test_standard_knowledge.py
+++ b/tests/advanced_search_system/knowledge/test_standard_knowledge.py
@@ -59,6 +59,43 @@ class TestGenerateKnowledge:
assert result == "Generated knowledge about topic"
mock_model.invoke.assert_called_once()
+ def test_strips_think_tags_from_knowledge(self):
+ """generate_knowledge strips <think> reasoning from the output."""
+ from local_deep_research.advanced_search_system.knowledge.standard_knowledge import (
+ StandardKnowledge,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(
+ content="<think>planning the answer</think>Quantum computing uses qubits."
+ )
+
+ knowledge = StandardKnowledge(mock_model)
+ result = knowledge.generate_knowledge("What is quantum computing?")
+
+ assert "<think>" not in result
+ assert "planning the answer" not in result
+ assert result == "Quantum computing uses qubits."
+
+ def test_compress_knowledge_strips_think_tags(self):
+ """compress_knowledge strips <think> reasoning from the output."""
+ from local_deep_research.advanced_search_system.knowledge.standard_knowledge import (
+ StandardKnowledge,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(
+ content="<think>condensing</think>Compressed facts [1]."
+ )
+
+ knowledge = StandardKnowledge(mock_model)
+ result = knowledge.compress_knowledge(
+ "Long accumulated knowledge", "query", []
+ )
+
+ assert "<think>" not in result
+ assert result == "Compressed facts [1]."
+
def test_includes_query_in_prompt(self):
"""Test that query is included in prompt."""
from local_deep_research.advanced_search_system.knowledge.standard_knowledge import (
diff --git a/tests/citation_handlers/test_forced_answer_citation_handler.py b/tests/citation_handlers/test_forced_answer_citation_handler.py
index 4d8a16d76d..5a83833dd1 100644
--- a/tests/citation_handlers/test_forced_answer_citation_handler.py
+++ b/tests/citation_handlers/test_forced_answer_citation_handler.py
@@ -195,6 +195,19 @@ class TestForcedAnswerExtractDirectAnswer:
assert "most likely answer" in result
assert "Original content" in result
+ def test_extract_direct_answer_empty_returns_content(
+ self, handler, mock_llm
+ ):
+ """An empty LLM answer (None) returns content unchanged, not '. <content>'."""
+ mock_llm.invoke.return_value = None
+
+ result = handler._extract_direct_answer(
+ "Who is it?", "Original content", "Sources text"
+ )
+
+ assert result == "Original content"
+ assert not result.startswith(". ")
+
def test_extract_direct_answer_strips_think_tags(self, handler, mock_llm):
"""_extract_direct_answer strips <think> reasoning from the answer."""
mock_llm.invoke.return_value = Mock(
diff --git a/tests/citation_handlers/test_precision_extraction_error_paths.py b/tests/citation_handlers/test_precision_extraction_error_paths.py
index f129a1ef52..a472d511ae 100644
--- a/tests/citation_handlers/test_precision_extraction_error_paths.py
+++ b/tests/citation_handlers/test_precision_extraction_error_paths.py
@@ -54,12 +54,12 @@ class TestExtractSingleAnswerError:
)
assert result == content
- def test_empty_answer_on_none_response(self, mock_llm):
- """A None LLM response is normalized to "" (no crash) and content is preserved.
+ def test_empty_answer_returns_content_unchanged(self, mock_llm):
+ """An empty LLM answer returns the original content unchanged (no ". " prefix).
- Responses flow through ``get_llm_response_text``, which returns "" for
- None instead of raising AttributeError. The extracted answer is therefore
- empty, but the original content is still appended.
+ A None response (or a think-only response that strips to "") normalizes to
+ "" via ``get_llm_response_text``. The extractor must then fall back to the
+ original content rather than prepending an empty answer (". content").
"""
from local_deep_research.citation_handlers.precision_extraction_handler import (
PrecisionExtractionHandler,
@@ -70,8 +70,7 @@ class TestExtractSingleAnswerError:
content = "Test content"
result = handler._extract_single_answer(content, "test query?", "")
- assert result.endswith(content)
- assert result == ". Test content"
+ assert result == "Test content"
def test_successful_extraction_prepends_answer(self, mock_llm):
"""Successful extraction prepends the answer to content."""
@@ -201,6 +200,21 @@ class TestExtractScoreError:
assert result.startswith("3-2.")
assert content in result
+ def test_returns_first_score_on_empty_answer(self, mock_llm):
+ """An empty LLM answer (None) falls back to the first score, not '. content'."""
+ from local_deep_research.citation_handlers.precision_extraction_handler import (
+ PrecisionExtractionHandler,
+ )
+
+ mock_llm.invoke.return_value = None
+ handler = PrecisionExtractionHandler(llm=mock_llm)
+ content = "The final score was 3-2. The halftime score was 1-1."
+ result = handler._extract_score(
+ content, "What was the final score?", ""
+ )
+ assert result.startswith("3-2.")
+ assert not result.startswith(". ")
+
def test_returns_content_when_no_scores_found(self, handler_with_error_llm):
"""When no score pattern is found, returns content unchanged."""
content = "It was an exciting game with many plays."
@@ -256,6 +270,21 @@ class TestExtractTemporalError:
assert result.startswith("1998.")
assert content in result
+ def test_returns_first_year_on_empty_answer(self, mock_llm):
+ """An empty LLM answer (None) falls back to the first year, not '. content'."""
+ from local_deep_research.citation_handlers.precision_extraction_handler import (
+ PrecisionExtractionHandler,
+ )
+
+ mock_llm.invoke.return_value = None
+ handler = PrecisionExtractionHandler(llm=mock_llm)
+ content = "Founded in 1998. Went public in 2004. Acquired in 2015."
+ result = handler._extract_temporal(
+ content, "When was the company founded?", ""
+ )
+ assert result.startswith("1998.")
+ assert not result.startswith(". ")
+
def test_returns_content_when_no_years_found(self, handler_with_error_llm):
"""When no year pattern is found, returns content unchanged."""
content = "The event happened many years ago."
@@ -328,6 +357,19 @@ class TestExtractNumberError:
assert result.startswith("8.3.")
assert content in result
+ def test_returns_first_number_on_empty_answer(self, mock_llm):
+ """An empty LLM answer (None) falls back to the first number, not '. content'."""
+ from local_deep_research.citation_handlers.precision_extraction_handler import (
+ PrecisionExtractionHandler,
+ )
+
+ mock_llm.invoke.return_value = None
+ handler = PrecisionExtractionHandler(llm=mock_llm)
+ content = "The population is 8.3 million. The area is 302 sq km."
+ result = handler._extract_number(content, "What is the population?", "")
+ assert result.startswith("8.3.")
+ assert not result.startswith(". ")
+
def test_returns_content_when_no_numbers_found(
self, handler_with_error_llm
):