~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(llm): hoist ProcessingLLMWrapper to module scope (#4914)
LearningCircuit3e4bcb9502ec
diff --git a/changelog.d/+hoist-processing-llm-wrapper.misc.md b/changelog.d/+hoist-processing-llm-wrapper.misc.mdnew file mode 100644index 0000000000..af559c684d--- /dev/null+++ b/changelog.d/+hoist-processing-llm-wrapper.misc.md@@ -0,0 +1 @@+Moved `ProcessingLLMWrapper` out of the `wrap_llm_without_think_tags` closure to module scope in `config/llm_config.py` — it's now importable and `isinstance`-checkable, and defined once instead of rebuilt on every call. Pure refactor; no behavior change.diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.pyindex de274c9037..026d87d8ed 100644--- a/src/local_deep_research/config/llm_config.py+++ b/src/local_deep_research/config/llm_config.py@@ -316,6 +316,123 @@ def _log_llm_error(error: Exception) -> None:logger.warning(f"LLM Request - Failed with error: {safe_msg}")+class ProcessingLLMWrapper:+ def __init__(self, base_llm):+ self.base_llm = base_llm++ @staticmethod+ 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: ``.stream()``/``.astream()``, ``with_config``,+ ``bind`` and other Runnable transforms still bypass this wrapper via+ ``__getattr__``, as do injected/unwrapped LLMs — those may still need+ explicit handling. ``bind_tools`` is NOT an exception: it re-wraps so+ the stripper survives — see the ``bind_tools`` override below and #4804.)++ 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 an+ ``AIMessage`` so callers can always rely on ``.content``. Anything+ else is passed through unchanged.++ Only *string* ``.content`` is stripped: ``remove_think_tags`` is+ text-only, and the ``<think>...</think>`` artifact is only ever+ emitted as plain text by some local models. Non-string content+ (e.g. provider content-block lists like Anthropic's, or ``None``)+ is passed through untouched — running the regex on it would raise+ ``TypeError`` or corrupt the structured content.+ """+ if hasattr(response, "content"):+ if isinstance(response.content, str):+ response.content = remove_think_tags(response.content)+ elif isinstance(response, str):+ response = AIMessage(content=remove_think_tags(response))+ return response++ @staticmethod+ def _log_llm_error(error: Exception) -> None:+ _log_llm_error(error)++ def invoke(self, *args: Any, **kwargs: Any) -> Any:+ try:+ response = self.base_llm.invoke(*args, **kwargs)+ except Exception as e:+ self._log_llm_error(e)+ raise+ return self._normalize_response(response)++ async def ainvoke(self, *args: Any, **kwargs: Any) -> Any:+ # Async counterpart of invoke(); without this, ainvoke() would fall+ # through __getattr__ to the base LLM and bypass think-tag stripping.+ try:+ response = await self.base_llm.ainvoke(*args, **kwargs)+ except Exception as e:+ self._log_llm_error(e)+ raise+ return self._normalize_response(response)++ # Pass through any other attributes to the base LLM+ def __getattr__(self, name):+ return getattr(self.base_llm, name)++ def close(self):+ """Close underlying HTTP clients held by this LLM. Idempotent."""+ try:+ from ..utilities.llm_utils import _close_base_llm++ _close_base_llm(self.base_llm)+ except Exception:+ logger.debug(+ "best-effort cleanup of HTTP clients on shutdown",+ exc_info=True,+ )++ def bind_tools(self, tools, **kwargs: Any) -> Any:+ """Re-wrap the tool-bound model so the <think>-stripper survives.++ ``langchain.agents.create_agent()`` and similar helpers call+ ``model.bind_tools(...)`` and then use the *return value* as+ the model that actually runs inside the agent loop. Without+ this override, Python falls through ``__getattr__`` to+ ``self.base_llm.bind_tools(...)``, which returns an unwrapped+ ``BaseChatModel``; the wrapper's ``_normalize_response`` (the+ single authorized site that strips ``<think>…`` from fresh+ responses) is then bypassed for every LLM call the agent+ makes.++ That bypass is the root cause of the regression tracked in+ issue #4804: reasoning-mode models (Qwen 3.x, deepseek-r1,+ etc.) emit ``<think>…</think>`` as plain text, the wrapper+ fails to strip it, the raw artifact is appended to the+ agent's ``AIMessage.content``, and on the next turn the+ provider's strict parser rejects the request with+ ``Failed to parse input at pos 0: <think>…``.++ Re-wrapping the bound model keeps the stripper in the loop+ for every LLM call the agent makes, not just the ones we+ issue directly via ``self.model.invoke(...)``.++ Args:+ tools: Tool definitions (schemas, callables, or+ ``BaseTool`` instances) to bind to the model.+ **kwargs: Provider-specific binding options forwarded+ verbatim to ``base_llm.bind_tools``.++ Returns:+ A ``ProcessingLLMWrapper`` wrapping the bound model, so+ every subsequent ``invoke``/``ainvoke`` continues to+ strip ``<think>`` tags.+ """+ bound = self.base_llm.bind_tools(tools, **kwargs)+ return ProcessingLLMWrapper(bound)++def wrap_llm_without_think_tags(llm,research_id=None,@@ -377,117 +494,4 @@ def wrap_llm_without_think_tags(else:llm.callbacks.extend(callbacks)- class ProcessingLLMWrapper:- def __init__(self, base_llm):- self.base_llm = base_llm-- @staticmethod- 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 an- ``AIMessage`` so callers can always rely on ``.content``. Anything- else is passed through unchanged.-- Only *string* ``.content`` is stripped: ``remove_think_tags`` is- text-only, and the ``<think>...</think>`` artifact is only ever- emitted as plain text by some local models. Non-string content- (e.g. provider content-block lists like Anthropic's, or ``None``)- is passed through untouched — running the regex on it would raise- ``TypeError`` or corrupt the structured content.- """- if hasattr(response, "content"):- if isinstance(response.content, str):- response.content = remove_think_tags(response.content)- elif isinstance(response, str):- response = AIMessage(content=remove_think_tags(response))- return response-- @staticmethod- def _log_llm_error(error: Exception) -> None:- _log_llm_error(error)-- def invoke(self, *args: Any, **kwargs: Any) -> Any:- try:- response = self.base_llm.invoke(*args, **kwargs)- except Exception as e:- self._log_llm_error(e)- raise- return self._normalize_response(response)-- async def ainvoke(self, *args: Any, **kwargs: Any) -> Any:- # Async counterpart of invoke(); without this, ainvoke() would fall- # through __getattr__ to the base LLM and bypass think-tag stripping.- try:- response = await self.base_llm.ainvoke(*args, **kwargs)- except Exception as e:- self._log_llm_error(e)- raise- return self._normalize_response(response)-- # Pass through any other attributes to the base LLM- def __getattr__(self, name):- return getattr(self.base_llm, name)-- def close(self):- """Close underlying HTTP clients held by this LLM. Idempotent."""- try:- from ..utilities.llm_utils import _close_base_llm-- _close_base_llm(self.base_llm)- except Exception:- logger.debug(- "best-effort cleanup of HTTP clients on shutdown",- exc_info=True,- )-- def bind_tools(self, tools, **kwargs: Any) -> Any:- """Re-wrap the tool-bound model so the <think>-stripper survives.-- ``langchain.agents.create_agent()`` and similar helpers call- ``model.bind_tools(...)`` and then use the *return value* as- the model that actually runs inside the agent loop. Without- this override, Python falls through ``__getattr__`` to- ``self.base_llm.bind_tools(...)``, which returns an unwrapped- ``BaseChatModel``; the wrapper's ``_normalize_response`` (the- single authorized site that strips ``<think>…`` from fresh- responses) is then bypassed for every LLM call the agent- makes.-- That bypass is the root cause of the regression tracked in- issue #4804: reasoning-mode models (Qwen 3.x, deepseek-r1,- etc.) emit ``<think>…</think>`` as plain text, the wrapper- fails to strip it, the raw artifact is appended to the- agent's ``AIMessage.content``, and on the next turn the- provider's strict parser rejects the request with- ``Failed to parse input at pos 0: <think>…``.-- Re-wrapping the bound model keeps the stripper in the loop- for every LLM call the agent makes, not just the ones we- issue directly via ``self.model.invoke(...)``.-- Args:- tools: Tool definitions (schemas, callables, or- ``BaseTool`` instances) to bind to the model.- **kwargs: Provider-specific binding options forwarded- verbatim to ``base_llm.bind_tools``.-- Returns:- A ``ProcessingLLMWrapper`` wrapping the bound model, so- every subsequent ``invoke``/``ainvoke`` continues to- strip ``<think>`` tags.- """- bound = self.base_llm.bind_tools(tools, **kwargs)- return ProcessingLLMWrapper(bound)-return ProcessingLLMWrapper(llm)