~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
refactor(citation): route LLM responses through get_llm_response_text (#4334)
LearningCircuitc91b36264721
diff --git a/changelog.d/+citation-handler-think-tag-normalization.bugfix.md b/changelog.d/+citation-handler-think-tag-normalization.bugfix.mdnew file mode 100644index 0000000000..fedf918257--- /dev/null+++ b/changelog.d/+citation-handler-think-tag-normalization.bugfix.md@@ -0,0 +1 @@+Citation handlers and the report structure generator now route LLM responses through `get_llm_response_text`, stripping `<think>` reasoning blocks from synthesized answers and reports (previously leaked verbatim with reasoning models) and normalizing string/message responses in one place.diff --git a/src/local_deep_research/citation_handlers/base_citation_handler.py b/src/local_deep_research/citation_handlers/base_citation_handler.pyindex 89618e2afe..a358618bff 100644--- a/src/local_deep_research/citation_handlers/base_citation_handler.py+++ b/src/local_deep_research/citation_handlers/base_citation_handler.py@@ -8,6 +8,8 @@ from typing import Any, Dict, List, Unionfrom langchain_core.documents import Documentfrom loguru import logger+from ..utilities.json_utils import get_llm_response_text+class BaseCitationHandler(ABC):"""Abstract base class for citation handlers."""@@ -17,6 +19,14 @@ class BaseCitationHandler(ABC):self.settings_snapshot = settings_snapshot or {}self._fact_checking_logged = False+ def _invoke_text(self, prompt: str) -> str:+ """Invoke the LLM and return normalized text.++ Handles both message objects (``.content``) and raw string responses,+ and strips ``<think>`` reasoning blocks via ``get_llm_response_text``.+ """+ return get_llm_response_text(self.llm.invoke(prompt))+def get_setting(self, key: str, default=None):"""Get a setting value from the snapshot."""if key in self.settings_snapshot: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.pyindex f461b4b94f..47294570fe 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@@ -46,9 +46,7 @@ Example response format:"[Direct Answer]. According to [1], this is supported by...""""- response = self.llm.invoke(prompt)- if not isinstance(response, str):- response = response.content+ response = self._invoke_text(prompt)# If the response still doesn't have a direct answer, extract oneif self._needs_answer_extraction(response, query):@@ -86,9 +84,7 @@ New Sources:{formatted_sources}Return the most likely answer based on evidence consistency."""- fact_check_response = self.llm.invoke(fact_check_prompt)- if not isinstance(fact_check_response, str):- fact_check_response = fact_check_response.content+ fact_check_response = self._invoke_text(fact_check_prompt)current_timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M"@@ -121,11 +117,7 @@ CRITICAL INSTRUCTIONS:Remember: A wrong answer is better than no answer for this task."""- response = self.llm.invoke(prompt)- if not isinstance(response, str):- content = response.content- else:- content = response+ content = self._invoke_text(prompt)# Final check - if still no direct answer, force extractionif self._needs_answer_extraction(content, question):@@ -190,10 +182,7 @@ RULES:Answer:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)# Format as a proper responsereturn 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.pyindex 52c1f46211..063f9c049d 100644--- a/src/local_deep_research/citation_handlers/precision_extraction_handler.py+++ b/src/local_deep_research/citation_handlers/precision_extraction_handler.py@@ -76,9 +76,7 @@ PRECISION INSTRUCTIONS:Format: Start with the direct, precise answer, then explain with citations."""- response = self.llm.invoke(prompt)- if not isinstance(response, str):- response = response.content+ response = self._invoke_text(prompt)# Apply precision extraction if neededresponse = self._apply_precision_extraction(@@ -133,11 +131,7 @@ PRECISION REQUIREMENTS:Provide the precise answer with citations. Do not create the bibliography, it will be provided automatically."""- response = self.llm.invoke(prompt)- if not isinstance(response, str):- content = response.content- else:- content = response+ content = self._invoke_text(prompt)# Apply precision extractioncontent = self._apply_precision_extraction(@@ -245,9 +239,7 @@ List all name variations found:Which is the FULL name (including middle name if present)?"""try:- extraction = self.llm.invoke(extraction_prompt)- if not isinstance(extraction, str):- extraction = extraction.content+ extraction = self._invoke_text(extraction_prompt)extraction_lower = extraction.lower()# Extract the identified full name@@ -311,10 +303,7 @@ Rules:The single answer is:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)# Clean up the answeranswer = answer.split(",")[@@ -378,10 +367,7 @@ Rules:The exact {dimension_type or "dimension"} is:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)# Clean and validate the answerimport re@@ -495,10 +481,7 @@ Context: {content[:1000]}The answer is:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)return f"{answer}. {content}"except Exception:# Return first score found if LLM extraction fails@@ -522,10 +505,7 @@ Context: {content[:1000]}The answer is:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)# Clean to just the year/dateyear_match = self.answer_patterns["year"].search(answer)if year_match:@@ -554,10 +534,7 @@ Context: {content[:1000]}The answer is:"""try:- answer = self.llm.invoke(extraction_prompt)- if not isinstance(answer, str):- answer = answer.content- answer = answer.strip()+ answer = self._invoke_text(extraction_prompt)return f"{answer}. {content}"except Exception:# Fallback to first found number if LLM extraction fails@@ -595,12 +572,8 @@ The answer is:"""List the most important facts (names, numbers, dates) found:"""try:- response = self.llm.invoke(extraction_prompt)- if not isinstance(response, str):- facts = response.content- else:- facts = response- return str(facts)[:500]+ facts = self._invoke_text(extraction_prompt)+ return facts[:500]except Exception:# Fallback to truncated previous knowledge if LLM extraction failsreturn previous_knowledge[:500]diff --git a/src/local_deep_research/citation_handlers/standard_citation_handler.py b/src/local_deep_research/citation_handlers/standard_citation_handler.pyindex c0a6632bd2..170b817c36 100644--- a/src/local_deep_research/citation_handlers/standard_citation_handler.py+++ b/src/local_deep_research/citation_handlers/standard_citation_handler.py@@ -34,9 +34,7 @@ Current time is {current_timestamp} UTC for verifying temporal references in souProvide a detailed analysis with citations. Do not create the bibliography, it will be provided automatically. Never make up sources. Never write or create urls. Only write text relevant to the question. Example format: "According to the research [1], ...""""- response = self.llm.invoke(prompt)- if not isinstance(response, str):- response = response.content+ response = self._invoke_text(prompt)return {"content": response, "documents": documents}def analyze_followup(@@ -66,10 +64,7 @@ New Sources:Return any inconsistencies or conflicts found."""if self.is_fact_checking_enabled():- fact_check_response = self.llm.invoke(fact_check_prompt)- if not isinstance(fact_check_response, str):- fact_check_response = fact_check_response.content-+ fact_check_response = self._invoke_text(fact_check_prompt)else:fact_check_response = ""@@ -94,8 +89,6 @@ Current time is {current_timestamp} UTC for verifying temporal references in souReflect information from sources critically based on: {fact_check_response}. Never invent sources.Provide a detailed answer with citations. Do not create the bibliography, it will be provided automatically. Example format: "According to [1], ..." """- response = self.llm.invoke(prompt)- if not isinstance(response, str):- response = response.content+ response = self._invoke_text(prompt)return {"content": response, "documents": documents}diff --git a/src/local_deep_research/report_generator.py b/src/local_deep_research/report_generator.pyindex 133c129ef2..f93c716df3 100644--- a/src/local_deep_research/report_generator.py+++ b/src/local_deep_research/report_generator.py@@ -9,7 +9,7 @@ from loguru import loggerfrom .config.llm_config import get_llmfrom .config.thread_settings import get_setting_from_snapshotfrom .search_system import AdvancedSearchSystem-from .utilities import search_utilities+from .utilities.json_utils import get_llm_response_text# Default constants for context accumulation to avoid repetition# These are used as fallbacks when settings are not available@@ -163,9 +163,7 @@ class IntegratedReportGenerator:DO NOT include sections about sources, citations, references, or methodology."""- response = search_utilities.remove_think_tags(- str(self.model.invoke(prompt).content)- )+ response = get_llm_response_text(self.model.invoke(prompt))# Parse the structurestructure: List[Dict[str, Any]] = []diff --git a/tests/citation_handlers/test_forced_answer_citation_handler.py b/tests/citation_handlers/test_forced_answer_citation_handler.pyindex 21a1f6d946..13cd3f343a 100644--- a/tests/citation_handlers/test_forced_answer_citation_handler.py+++ b/tests/citation_handlers/test_forced_answer_citation_handler.py@@ -427,6 +427,59 @@ class TestForcedAnswerAnalyzeFollowup:assert mock_llm.invoke.call_count == 3assert "John Smith" in result["content"]+ def test_analyze_followup_handles_string_response(+ self,+ mock_llm_string_response,+ sample_search_results,+ sample_previous_knowledge,+ ):+ """analyze_followup returns raw string content unchanged (fact-check off, no extraction)."""+ from local_deep_research.citation_handlers.forced_answer_citation_handler import (+ ForcedAnswerCitationHandler,+ )++ handler = ForcedAnswerCitationHandler(+ llm=mock_llm_string_response,+ settings_snapshot={"general.enable_fact_checking": False},+ )++ result = handler.analyze_followup(+ "Tell me more about the topic.",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ assert result["content"] == "Test string response with citation [1]."+ assert mock_llm_string_response.invoke.call_count == 1++ def test_analyze_followup_string_response_with_fact_checking(+ self,+ mock_llm_string_response,+ sample_search_results,+ sample_previous_knowledge,+ settings_with_fact_checking,+ ):+ """With fact-check on, string fact-check + main responses pass through; no extraction."""+ from local_deep_research.citation_handlers.forced_answer_citation_handler import (+ ForcedAnswerCitationHandler,+ )++ handler = ForcedAnswerCitationHandler(+ llm=mock_llm_string_response,+ settings_snapshot=settings_with_fact_checking,+ )++ result = handler.analyze_followup(+ "Tell me more about the topic.",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ assert result["content"] == "Test string response with citation [1]."+ assert mock_llm_string_response.invoke.call_count == 2+class TestForcedAnswerEdgeCases:"""Tests for edge cases and error handling."""diff --git a/tests/citation_handlers/test_precision_extraction_error_paths.py b/tests/citation_handlers/test_precision_extraction_error_paths.pyindex 71cf8eb3b9..f129a1ef52 100644--- a/tests/citation_handlers/test_precision_extraction_error_paths.py+++ b/tests/citation_handlers/test_precision_extraction_error_paths.py@@ -54,8 +54,13 @@ class TestExtractSingleAnswerError:)assert result == content- def test_returns_content_on_attribute_error(self, mock_llm):- """When LLM response has no .content attribute, returns original."""+ def test_empty_answer_on_none_response(self, mock_llm):+ """A None LLM response is normalized to "" (no crash) and content is preserved.++ 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.+ """from local_deep_research.citation_handlers.precision_extraction_handler import (PrecisionExtractionHandler,)@@ -65,7 +70,8 @@ class TestExtractSingleAnswerError:content = "Test content"result = handler._extract_single_answer(content, "test query?", "")- assert result == content+ assert result.endswith(content)+ assert result == ". Test content"def test_successful_extraction_prepends_answer(self, mock_llm):"""Successful extraction prepends the answer to content."""diff --git a/tests/citation_handlers/test_precision_extraction_handler.py b/tests/citation_handlers/test_precision_extraction_handler.pyindex fc6e3e4209..63f56d0d98 100644--- a/tests/citation_handlers/test_precision_extraction_handler.py+++ b/tests/citation_handlers/test_precision_extraction_handler.py@@ -460,6 +460,85 @@ class TestPrecisionExtractionMethods:# Should return truncated previous knowledgeassert "Previous knowledge" in result+ def test_extract_full_name_handles_string_response(self, handler, mock_llm):+ """_extract_full_name handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "Full name: John Michael Smith"++ result = handler._extract_full_name(+ "John Smith was a scientist.", "Who is it?", "Sources"+ )++ assert "John Michael Smith" in result++ def test_extract_single_answer_handles_string_response(+ self, handler, mock_llm+ ):+ """_extract_single_answer handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "Option A, Option B, and Option C"++ result = handler._extract_single_answer(+ "Multiple options content", "Which one?", "Sources"+ )++ assert result.startswith("Option A.")++ def test_extract_dimension_handles_string_response(self, handler, mock_llm):+ """_extract_dimension handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "324 meters"++ result = handler._extract_dimension(+ "The tower is 324 meters tall.", "How tall?", "Sources"+ )++ assert result.startswith("324 meters.")++ def test_extract_score_handles_string_response(self, handler, mock_llm):+ """_extract_score handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "3-2"++ result = handler._extract_score(+ "Halftime was 1-1. Final score was 3-2.",+ "What was the final score?",+ "Sources",+ )++ assert result.startswith("3-2.")++ def test_extract_temporal_handles_string_response(self, handler, mock_llm):+ """_extract_temporal handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "1998"++ result = handler._extract_temporal(+ "Founded in 1998, acquired in 2015.",+ "When was it founded?",+ "Sources",+ )++ assert result.startswith("1998.")++ def test_extract_number_handles_string_response(self, handler, mock_llm):+ """_extract_number handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "42"++ result = handler._extract_number(+ "There are 42 employees and 10 managers.",+ "How many employees?",+ "Sources",+ )++ assert result.startswith("42.")++ def test_extract_key_facts_handles_string_response(self, handler, mock_llm):+ """_extract_key_facts handles a raw string LLM return (str branch)."""+ mock_llm.invoke.return_value = "Key facts: Founded 1998, Location: NYC"++ result = handler._extract_key_facts(+ "Previous knowledge about the company", "name"+ )++ assert "Founded 1998" in result+ assert len(result) <= 500+class TestPrecisionExtractionAnalyzeInitial:"""Tests for analyze_initial method."""@@ -625,6 +704,30 @@ class TestPrecisionExtractionAnalyzeFollowup:# First document should have index 11 (10 + 1)assert result["documents"][0].metadata["index"] == 11+ def test_analyze_followup_handles_string_response(+ self,+ mock_llm_string_response,+ sample_search_results,+ sample_previous_knowledge,+ ):+ """analyze_followup returns raw string content for a general-type question."""+ from local_deep_research.citation_handlers.precision_extraction_handler import (+ PrecisionExtractionHandler,+ )++ handler = PrecisionExtractionHandler(llm=mock_llm_string_response)++ result = handler.analyze_followup(+ "Tell me more about the topic.",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ # general-type question -> precision extraction leaves content unchanged+ assert result["content"] == "Test string response with citation [1]."+ assert "documents" in result+class TestPrecisionExtractionEdgeCases:"""Tests for edge cases and error handling."""diff --git a/tests/citation_handlers/test_standard_citation_handler.py b/tests/citation_handlers/test_standard_citation_handler.pyindex 9c3aed2379..94eb085f74 100644--- a/tests/citation_handlers/test_standard_citation_handler.py+++ b/tests/citation_handlers/test_standard_citation_handler.py@@ -4,6 +4,7 @@ Tests initialization, analyze_initial, analyze_followup, and edge cases."""from datetime import datetime, timezone+from unittest.mock import Mockclass TestStandardCitationHandlerInit:@@ -399,6 +400,90 @@ class TestStandardCitationHandlerAnalyzeFollowup:assert "Cross-reference" in first_call_argsassert "contradictions" in first_call_args+ def test_analyze_followup_handles_string_response(+ self,+ mock_llm_string_response,+ sample_search_results,+ sample_previous_knowledge,+ ):+ """analyze_followup returns raw string responses unchanged (fact-check off)."""+ from local_deep_research.citation_handlers.standard_citation_handler import (+ StandardCitationHandler,+ )++ handler = StandardCitationHandler(+ llm=mock_llm_string_response,+ settings_snapshot={"general.enable_fact_checking": False},+ )++ result = handler.analyze_followup(+ "Follow-up question?",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ assert result["content"] == "Test string response with citation [1]."+ assert mock_llm_string_response.invoke.call_count == 1++ def test_analyze_followup_string_response_with_fact_checking(+ self,+ mock_llm_string_response,+ sample_search_results,+ sample_previous_knowledge,+ settings_with_fact_checking,+ ):+ """With fact-check on, string responses pass through and the LLM is called twice."""+ from local_deep_research.citation_handlers.standard_citation_handler import (+ StandardCitationHandler,+ )++ handler = StandardCitationHandler(+ llm=mock_llm_string_response,+ settings_snapshot=settings_with_fact_checking,+ )++ result = handler.analyze_followup(+ "Follow-up question?",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ assert result["content"] == "Test string response with citation [1]."+ assert mock_llm_string_response.invoke.call_count == 2++ def test_analyze_followup_strips_think_tags(+ self,+ mock_llm,+ sample_search_results,+ sample_previous_knowledge,+ ):+ """analyze_followup strips <think>...</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 here</think>Answer with citation [1]."+ )++ handler = StandardCitationHandler(+ llm=mock_llm,+ settings_snapshot={"general.enable_fact_checking": False},+ )++ result = handler.analyze_followup(+ "Follow-up question?",+ sample_search_results,+ sample_previous_knowledge,+ nr_of_links=0,+ )++ assert "<think>" not in result["content"]+ assert "internal reasoning" not in result["content"]+ assert result["content"] == "Answer with citation [1]."+class TestStandardCitationHandlerDocumentCreation:"""Tests for document creation functionality."""diff --git a/tests/report/test_report_structure_parsing.py b/tests/report/test_report_structure_parsing.pyindex a156a07cbc..f192288fe7 100644--- a/tests/report/test_report_structure_parsing.py+++ b/tests/report/test_report_structure_parsing.py@@ -356,6 +356,46 @@ class TestMalformedStructureHandling:# Section 2 has no name after the dotassert len(structure) >= 2+ def test_determine_structure_handles_string_response(+ self, report_generator+ ):+ """A raw string LLM return (no .content) is handled without AttributeError."""+ report_generator.model.invoke.return_value = (+ "STRUCTURE\n1. Introduction\n - Overview | Purpose\nEND_STRUCTURE"+ )++ findings = {"current_knowledge": "Test content " * 100}+ structure = report_generator._determine_report_structure(+ findings, "test query"+ )++ assert len(structure) == 1+ assert structure[0]["name"] == "Introduction"++ def test_determine_structure_strips_think_tags(self, report_generator):+ """<think> reasoning is stripped before parsing so it can't create bogus sections."""+ response = (+ "<think>\n"+ "1. First I should consider the topic\n"+ "2. Then organize by theme\n"+ "</think>\n"+ "STRUCTURE\n"+ "1. Introduction\n"+ " - Overview | Purpose\n"+ "END_STRUCTURE"+ )+ report_generator.model.invoke.return_value = MagicMock(content=response)++ findings = {"current_knowledge": "Test content " * 100}+ structure = report_generator._determine_report_structure(+ findings, "test query"+ )++ # Without stripping, the numbered lines inside <think> would parse as+ # bogus sections; with stripping only the real section remains.+ assert len(structure) == 1+ assert structure[0]["name"] == "Introduction"+class TestSourceSectionFiltering:"""Tests for filtering source-related sections."""