~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: remove redundant per-site <think>-stripping; rely on central wrapper (#4343)
LearningCircuita761417b6df6
diff --git a/changelog.d/+remove-redundant-think-stripping.bugfix.md b/changelog.d/+remove-redundant-think-stripping.bugfix.mdnew file mode 100644index 0000000000..938c421524--- /dev/null+++ b/changelog.d/+remove-redundant-think-stripping.bugfix.md@@ -0,0 +1 @@+Remove redundant per-site `<think>`-tag stripping now that `get_llm` LLMs are normalized centrally in `ProcessingLLMWrapper`. Direct `self.model.invoke(...).content` is already think-stripped, so ~25 scattered `remove_think_tags(...)` calls across the search strategies, constraint/evidence/knowledge components, and synthesis path were dropped (relying on the single central strip point), keeping explicit handling only where it is load-bearing (agent/`bind_tools` paths, injected LLMs). Empty-answer fallbacks now log a warning so a silent degrade is visible.diff --git a/src/local_deep_research/advanced_search_system/constraints/constraint_analyzer.py b/src/local_deep_research/advanced_search_system/constraints/constraint_analyzer.pyindex 507f61491f..fc8b82117e 100644--- a/src/local_deep_research/advanced_search_system/constraints/constraint_analyzer.py+++ b/src/local_deep_research/advanced_search_system/constraints/constraint_analyzer.py@@ -8,7 +8,6 @@ from typing import Listfrom langchain_core.language_models import BaseChatModelfrom loguru import logger-from ...utilities.search_utilities import remove_think_tagsfrom .base_constraint import Constraint, ConstraintType@@ -57,7 +56,7 @@ Focus on answer verification, not query parsing."""response = self.model.invoke(prompt)- content = remove_think_tags(response.content)+ content = response.contentconstraints = []current_constraint = {}diff --git a/src/local_deep_research/advanced_search_system/evidence/evaluator.py b/src/local_deep_research/advanced_search_system/evidence/evaluator.pyindex 0c9a900a51..fca4c2d3b6 100644--- a/src/local_deep_research/advanced_search_system/evidence/evaluator.py+++ b/src/local_deep_research/advanced_search_system/evidence/evaluator.py@@ -7,7 +7,6 @@ from typing import Dictfrom langchain_core.language_models import BaseChatModelfrom loguru import logger-from ...utilities.search_utilities import remove_think_tagsfrom ..constraints.base_constraint import Constraintfrom .base_evidence import Evidence, EvidenceType@@ -59,7 +58,7 @@ QUOTE: [relevant text]"""response = self.model.invoke(prompt)- content = remove_think_tags(response.content)+ content = response.content# Parse responseparsed = self._parse_evidence_response(content)diff --git a/src/local_deep_research/advanced_search_system/findings/repository.py b/src/local_deep_research/advanced_search_system/findings/repository.pyindex 96e4db6caf..14035f6bfe 100644--- a/src/local_deep_research/advanced_search_system/findings/repository.py+++ b/src/local_deep_research/advanced_search_system/findings/repository.py@@ -8,7 +8,6 @@ from typing import Dict, List, Unionfrom langchain_core.documents import Documentfrom langchain_core.language_models import BaseLanguageModel-from ...utilities.json_utils import get_llm_response_textfrom ...utilities.search_utilities import format_findingsfrom .base_findings import BaseFindingsRepository@@ -363,8 +362,12 @@ Use IEEE style citations [1], [2], etc. Never make up your own citations.120, self.model.invoke, prompt)- # Normalize response (handles str/.content, strips <think> tags)- synthesized_content = get_llm_response_text(response)+ # <think> tags are stripped centrally in ProcessingLLMWrapper;+ # keep only a shape-guard for non-message returns.+ if hasattr(response, "content"):+ synthesized_content = response.content+ else:+ synthesized_content = str(response)logger.info(f"Successfully synthesized final answer for query: '{query}'"@@ -399,10 +402,12 @@ Use IEEE style citations [1], [2], etc. Never make up your own citations.):response = self.model.invoke(prompt)- # Normalize response (handles str/.content, strips <think> tags)- synthesized_content = get_llm_response_text(- response- )+ # <think> tags are stripped centrally in ProcessingLLMWrapper;+ # keep only a shape-guard for non-message returns.+ if hasattr(response, "content"):+ synthesized_content = response.content+ else:+ synthesized_content = str(response)logger.info(f"Successfully synthesized final answer for query: '{query}'"diff --git a/src/local_deep_research/advanced_search_system/knowledge/followup_context_manager.py b/src/local_deep_research/advanced_search_system/knowledge/followup_context_manager.pyindex 2e9c7d507c..31a26c0cb1 100644--- a/src/local_deep_research/advanced_search_system/knowledge/followup_context_manager.py+++ b/src/local_deep_research/advanced_search_system/knowledge/followup_context_manager.py@@ -10,7 +10,6 @@ from typing import Dict, List, Any, Optionalfrom loguru import loggerfrom langchain_core.language_models.chat_models import BaseChatModel-from ...utilities.search_utilities import remove_think_tagsclass FollowUpContextHandler:@@ -156,9 +155,7 @@ Return up to 10 most important entities, one per line.response = self.model.invoke(prompt)entities = [line.strip()- for line in remove_think_tags(response.content)- .strip()- .split("\n")+ for line in response.content.strip().split("\n")if line.strip()]return entities[:10]@@ -296,7 +293,7 @@ Create a summary of {max_sentences} sentences that captures the most relevant intry:response = self.model.invoke(prompt)- summary = remove_think_tags(response.content).strip()+ summary = response.content.strip()# Apply length constraint if specifiedif max_length and len(summary) > max_length:@@ -343,9 +340,7 @@ What specific information is missing or needs clarification? List up to 5 gaps,response = self.model.invoke(prompt)gaps = [line.strip()- for line in remove_think_tags(response.content)- .strip()- .split("\n")+ for line in response.content.strip().split("\n")if line.strip()]return gaps[:5]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.pyindex bcb996faf8..fb9a78081f 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,7 +6,6 @@ from loguru import loggerfrom datetime import datetime, UTCfrom typing import List-from ...utilities.json_utils import get_llm_response_textfrom .base_knowledge import BaseKnowledgeGenerator@@ -59,7 +58,7 @@ Generate detailed knowledge that:Format your response as a well-structured paragraph."""- knowledge = get_llm_response_text(self.model.invoke(prompt))+ knowledge = self.model.invoke(prompt).contentlogger.info("Generated knowledge successfully")return knowledge@@ -90,7 +89,7 @@ Generate detailed knowledge that:Format your response as a well-structured paragraph."""try:- return get_llm_response_text(self.model.invoke(prompt))+ return self.model.invoke(prompt).contentexcept Exception:logger.exception("Error generating sub-knowledge")return ""@@ -127,9 +126,7 @@ Accumulated Knowledge:Compressed Knowledge:"""try:- compressed_knowledge = get_llm_response_text(- self.model.invoke(prompt)- )+ compressed_knowledge = self.model.invoke(prompt).contentlogger.info(f"Compressed knowledge length: {len(compressed_knowledge)}")diff --git a/src/local_deep_research/api/research_functions.py b/src/local_deep_research/api/research_functions.pyindex 84847f08d4..7c9b335b64 100644--- a/src/local_deep_research/api/research_functions.py+++ b/src/local_deep_research/api/research_functions.py@@ -16,7 +16,6 @@ from ..report_generator import IntegratedReportGeneratorfrom ..search_system import AdvancedSearchSystemfrom ..utilities.db_utils import no_db_settingsfrom ..utilities.thread_context import clear_search_context, set_search_context-from ..utilities.search_utilities import remove_think_tagsfrom .settings_utils import create_settings_snapshot@@ -730,7 +729,7 @@ def analyze_documents(logger.info(f"LLM summary generation completed in {llm_elapsed:.2f}s")if hasattr(summary_response, "content"):- summary = remove_think_tags(summary_response.content)+ summary = summary_response.contentelse:summary = str(summary_response)diff --git a/src/local_deep_research/citation_handlers/precision_extraction_handler.py b/src/local_deep_research/citation_handlers/precision_extraction_handler.pyindex f173692d03..a38225882f 100644--- a/src/local_deep_research/citation_handlers/precision_extraction_handler.py+++ b/src/local_deep_research/citation_handlers/precision_extraction_handler.py@@ -485,6 +485,10 @@ The answer is:"""try:answer = self._invoke_text(extraction_prompt)if not answer:+ logger.warning(+ "Empty LLM answer in score extraction; "+ "falling back to first matched score"+ )return f"{scores[0][0]}-{scores[0][1]}. {content}"return f"{answer}. {content}"except Exception:@@ -511,6 +515,10 @@ The answer is:"""try:answer = self._invoke_text(extraction_prompt)if not answer:+ logger.warning(+ "Empty LLM answer in temporal extraction; "+ "falling back to first matched year"+ )return f"{years[0]}. {content}"# Clean to just the year/dateyear_match = self.answer_patterns["year"].search(answer)@@ -542,6 +550,10 @@ The answer is:"""try:answer = self._invoke_text(extraction_prompt)if not answer:+ logger.warning(+ "Empty LLM answer in number extraction; "+ "falling back to first matched number"+ )return f"{numbers[0]}. {content}"return f"{answer}. {content}"except Exception:diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.pyindex b834b43906..953fd73692 100644--- a/src/local_deep_research/config/llm_config.py+++ b/src/local_deep_research/config/llm_config.py@@ -398,6 +398,14 @@ def wrap_llm_without_think_tags(def _normalize_response(response: Any) -> Any:"""Strip <think> tags and normalize the response shape.+ This is the SINGLE authorized place that strips <think> tags from a+ fresh LLM response. Every LLM from get_llm() is wrapped here, so+ downstream code can use ``response.content`` directly — do NOT add+ per-site ``remove_think_tags`` / ``get_llm_response_text`` calls on+ fresh ``invoke``/``ainvoke`` results; they are redundant and hide+ bugs. (Exceptions: agent/bind_tools paths bypass this wrapper, and+ injected/unwrapped LLMs — those may still need explicit handling.)+A message keeps its object identity (only ``.content`` is rewritten,so ``additional_kwargs``/``reasoning_content``/``tool_calls`` survive).A bare-string return (some providers/wrappers) is wrapped into andiff --git a/src/local_deep_research/utilities/search_utilities.py b/src/local_deep_research/utilities/search_utilities.pyindex 3adc109c19..ac94218a04 100644--- a/src/local_deep_research/utilities/search_utilities.py+++ b/src/local_deep_research/utilities/search_utilities.py@@ -24,6 +24,10 @@ LANGUAGE_CODE_MAP = {def remove_think_tags(text: str) -> str:+ # NOTE: Fresh LLM responses from get_llm() are already <think>-stripped+ # centrally by ProcessingLLMWrapper (config/llm_config.py). Use this only on+ # text NOT from a fresh wrapped invoke (accumulated/concatenated text, or+ # agent/bind_tools output that bypasses the wrapper).# Remove paired <think>...</think> tagstext = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)# Remove any orphaned opening or closing think tagsdiff --git a/tests/advanced_search_system/evidence/test_evaluator.py b/tests/advanced_search_system/evidence/test_evaluator.pyindex 38e72a0c36..b0fb2ebc8e 100644--- a/tests/advanced_search_system/evidence/test_evaluator.py+++ b/tests/advanced_search_system/evidence/test_evaluator.py@@ -8,7 +8,7 @@ test_evaluator_integration.py:- extract_evidence handles invalid confidence values gracefully"""-from unittest.mock import Mock, patch+from unittest.mock import Mockclass TestEvidenceEvaluatorInit:@@ -29,12 +29,7 @@ class TestEvidenceEvaluatorInit:class TestExtractEvidence:"""Tests for extract_evidence method."""- @patch(- "local_deep_research.advanced_search_system.evidence.evaluator.remove_think_tags"- )- def test_extract_evidence_handles_invalid_confidence(- self, mock_remove_tags- ):+ def test_extract_evidence_handles_invalid_confidence(self):"""Test that invalid confidence is handled."""from local_deep_research.advanced_search_system.evidence.evaluator import (EvidenceEvaluator,@@ -51,7 +46,6 @@ TYPE: inferenceSOURCE: AnalysisCONFIDENCE: not_a_number""")- mock_remove_tags.side_effect = lambda x: xevaluator = EvidenceEvaluator(mock_model)constraint = Constraint(diff --git a/tests/advanced_search_system/findings/test_findings_repository.py b/tests/advanced_search_system/findings/test_findings_repository.pyindex 5f356b7838..1d9c1afef6 100644--- a/tests/advanced_search_system/findings/test_findings_repository.py+++ b/tests/advanced_search_system/findings/test_findings_repository.py@@ -129,28 +129,6 @@ 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 (diff --git a/tests/advanced_search_system/knowledge/test_standard_knowledge.py b/tests/advanced_search_system/knowledge/test_standard_knowledge.pyindex fa7bea1052..8f82712c7f 100644--- a/tests/advanced_search_system/knowledge/test_standard_knowledge.py+++ b/tests/advanced_search_system/knowledge/test_standard_knowledge.py@@ -59,43 +59,6 @@ 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 (