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(citation): fix broken think-tag test + harden coverage after #4334 (#4335)

LearningCircuiteb00b571c3ab
diff --git a/tests/citation_handlers/test_base_citation_handler.py b/tests/citation_handlers/test_base_citation_handler.py
index 6f2ec95b15..3993a64b24 100644
--- a/tests/citation_handlers/test_base_citation_handler.py
+++ b/tests/citation_handlers/test_base_citation_handler.py
@@ -70,6 +70,41 @@ class TestBaseCitationHandlerInit:
assert handler.settings_snapshot == {}
+class TestInvokeText:
+ """Tests for the _invoke_text helper (normalizes LLM responses)."""
+
+ @staticmethod
+ def _handler(return_value):
+ mock_llm = MagicMock()
+ mock_llm.invoke.return_value = return_value
+ return ConcreteCitationHandler(mock_llm)
+
+ def test_returns_string_response_unchanged(self):
+ """A raw string response is returned as-is."""
+ handler = self._handler("plain answer [1].")
+ assert handler._invoke_text("prompt") == "plain answer [1]."
+
+ def test_extracts_content_from_message_object(self):
+ """A message object's .content is extracted."""
+ handler = self._handler(MagicMock(content="message answer [1]."))
+ assert handler._invoke_text("prompt") == "message answer [1]."
+
+ def test_strips_think_tags(self):
+ """<think>...</think> reasoning blocks are removed."""
+ handler = self._handler(
+ MagicMock(content="<think>reasoning</think>final answer [1].")
+ )
+ result = handler._invoke_text("prompt")
+ assert "<think>" not in result
+ assert "reasoning" not in result
+ assert result == "final answer [1]."
+
+ def test_none_response_returns_empty_string(self):
+ """A None response normalizes to "" instead of raising."""
+ handler = self._handler(None)
+ assert handler._invoke_text("prompt") == ""
+
+
class TestGetSetting:
"""Tests for the get_setting method."""
diff --git a/tests/citation_handlers/test_forced_answer_citation_handler.py b/tests/citation_handlers/test_forced_answer_citation_handler.py
index 13cd3f343a..4d8a16d76d 100644
--- a/tests/citation_handlers/test_forced_answer_citation_handler.py
+++ b/tests/citation_handlers/test_forced_answer_citation_handler.py
@@ -195,6 +195,33 @@ class TestForcedAnswerExtractDirectAnswer:
assert "most likely answer" in result
assert "Original content" in result
+ 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(
+ content="<think>weighing candidates</think>John Smith"
+ )
+
+ result = handler._extract_direct_answer(
+ "Who is it?", "Original content", "Sources text"
+ )
+
+ assert "<think>" not in result
+ assert "weighing candidates" not in result
+ assert result.startswith("John Smith.")
+
+ def test_extract_direct_answer_handles_string_response(
+ self, handler, mock_llm
+ ):
+ """_extract_direct_answer handles a raw string LLM return."""
+ mock_llm.invoke.return_value = "John Smith"
+
+ result = handler._extract_direct_answer(
+ "Who is it?", "Original content", "Sources text"
+ )
+
+ assert result.startswith("John Smith.")
+ assert "Original content" in result
+
def test_extract_direct_answer_prompt_includes_query(
self, handler, mock_llm
):
@@ -243,6 +270,30 @@ class TestForcedAnswerAnalyzeInitial:
assert "documents" in result
assert len(result["documents"]) == 3
+ def test_analyze_initial_strips_think_tags(
+ self, mock_llm, sample_search_results
+ ):
+ """analyze_initial strips <think> reasoning (no extraction triggered)."""
+ from local_deep_research.citation_handlers.forced_answer_citation_handler import (
+ ForcedAnswerCitationHandler,
+ )
+
+ mock_llm.invoke.return_value = Mock(
+ content="<think>weighing options</think>The answer is X. According to [1], this is correct."
+ )
+
+ handler = ForcedAnswerCitationHandler(llm=mock_llm)
+
+ result = handler.analyze_initial(
+ "What is the answer?", sample_search_results
+ )
+
+ assert "<think>" not in result["content"]
+ assert "weighing options" not in result["content"]
+ assert result["content"].startswith("The answer is X.")
+ # Clean answer -> no forced extraction call
+ assert mock_llm.invoke.call_count == 1
+
def test_analyze_initial_prompt_includes_forced_instructions(
self, mock_llm, sample_search_results
):
diff --git a/tests/citation_handlers/test_precision_extraction_handler.py b/tests/citation_handlers/test_precision_extraction_handler.py
index 63f56d0d98..82b45d2ad7 100644
--- a/tests/citation_handlers/test_precision_extraction_handler.py
+++ b/tests/citation_handlers/test_precision_extraction_handler.py
@@ -635,6 +635,29 @@ class TestPrecisionExtractionAnalyzeInitial:
assert "content" in result
+ def test_analyze_initial_strips_think_tags(
+ self, mock_llm, sample_search_results
+ ):
+ """analyze_initial strips <think> reasoning (general question, no extraction)."""
+ from local_deep_research.citation_handlers.precision_extraction_handler import (
+ PrecisionExtractionHandler,
+ )
+
+ mock_llm.invoke.return_value = Mock(
+ content="<think>deciding</think>General answer with citation [1]."
+ )
+
+ handler = PrecisionExtractionHandler(llm=mock_llm)
+
+ # general-type question -> _apply_precision_extraction leaves content as-is
+ result = handler.analyze_initial(
+ "Tell me about the topic.", sample_search_results
+ )
+
+ assert "<think>" not in result["content"]
+ assert "deciding" not in result["content"]
+ assert result["content"] == "General answer with citation [1]."
+
class TestPrecisionExtractionAnalyzeFollowup:
"""Tests for analyze_followup method."""
diff --git a/tests/citation_handlers/test_standard_citation_handler.py b/tests/citation_handlers/test_standard_citation_handler.py
index 94eb085f74..42255186dc 100644
--- a/tests/citation_handlers/test_standard_citation_handler.py
+++ b/tests/citation_handlers/test_standard_citation_handler.py
@@ -206,6 +206,26 @@ class TestStandardCitationHandlerAnalyzeInitial:
assert result["content"] == "Test response with citation [1]."
+ def test_analyze_initial_strips_think_tags(
+ self, mock_llm, sample_search_results
+ ):
+ """Test analyze_initial strips <think> reasoning from the response."""
+ from local_deep_research.citation_handlers.standard_citation_handler import (
+ StandardCitationHandler,
+ )
+
+ mock_llm.invoke.return_value = Mock(
+ content="<think>internal reasoning</think>Answer with citation [1]."
+ )
+
+ handler = StandardCitationHandler(llm=mock_llm)
+
+ result = handler.analyze_initial("Test query", sample_search_results)
+
+ assert "<think>" not in result["content"]
+ assert "internal reasoning" not in result["content"]
+ assert result["content"] == "Answer with citation [1]."
+
def test_analyze_initial_with_output_instructions(
self, mock_llm, sample_search_results, settings_with_output_instructions
):
diff --git a/tests/test_report_generator_coverage.py b/tests/test_report_generator_coverage.py
index 1abc92a4c0..e2d0a16612 100644
--- a/tests/test_report_generator_coverage.py
+++ b/tests/test_report_generator_coverage.py
@@ -806,24 +806,40 @@ class TestDeprecatedGenerateSections:
class TestDetermineReportStructureThinkTags:
- """Verify that search_utilities.remove_think_tags is called on LLM response."""
+ """Verify <think> blocks are stripped from the LLM response before parsing.
- def test_remove_think_tags_called(self, generator, mock_model):
- """remove_think_tags is invoked on model output."""
+ The response is normalized via ``get_llm_response_text`` (which strips think
+ tags), so reasoning content cannot leak into the parsed structure.
+ """
+
+ def test_think_tags_stripped_before_parsing(self, generator, mock_model):
+ """Numbered lines inside <think> must not become bogus sections."""
mock_model.invoke.return_value = MagicMock(
- content="<think>internal</think>STRUCTURE\n1. Real\nEND_STRUCTURE"
+ content=(
+ "<think>\n"
+ "1. Bogus reasoning section\n"
+ "</think>\n"
+ "STRUCTURE\n1. Real\nEND_STRUCTURE"
+ )
)
- with patch(
- f"{MODULE}.search_utilities.remove_think_tags",
- wraps=lambda x: x.replace("<think>internal</think>", ""),
- ) as mock_rt:
- findings = {"current_knowledge": "knowledge"}
- structure = generator._determine_report_structure(findings, "q")
- mock_rt.assert_called_once()
- # Should still parse the section
- assert len(structure) == 1
- assert structure[0]["name"] == "Real"
+ findings = {"current_knowledge": "knowledge"}
+ structure = generator._determine_report_structure(findings, "q")
+
+ # Without stripping, "1. Bogus reasoning section" would parse as a
+ # section; with stripping only the real section remains.
+ assert len(structure) == 1
+ assert structure[0]["name"] == "Real"
+
+ def test_string_response_does_not_crash(self, generator, mock_model):
+ """A raw string model return (no .content) is handled gracefully."""
+ mock_model.invoke.return_value = "STRUCTURE\n1. Real\nEND_STRUCTURE"
+
+ findings = {"current_knowledge": "knowledge"}
+ structure = generator._determine_report_structure(findings, "q")
+
+ assert len(structure) == 1
+ assert structure[0]["name"] == "Real"
# ---------------------------------------------------------------------------