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(llm): harden #4804 think-stripper fix — accurate comments + real create_agent regression test (#4913)

LearningCircuitb9f378ca3e90
diff --git a/changelog.d/+think-strip-followup-tests.misc.md b/changelog.d/+think-strip-followup-tests.misc.md
new file mode 100644
index 0000000000..b4b0a4d751
--- /dev/null
+++ b/changelog.d/+think-strip-followup-tests.misc.md
@@ -0,0 +1 @@
+Hardened the #4804 `<think>`-stripper fix: corrected the now-stale `bind_tools` comments in the LangGraph agent, made the async wrapper test actually assert stripping, and added a real `create_agent` integration regression test (sync + async).
diff --git a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
index c21c0d754e..3996adf7f7 100644
--- a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
@@ -360,14 +360,16 @@ def _make_research_subtopic_tool(
if sub_fetch is not None:
sub_tools.append(sub_fetch)
try:
- # NOTE: create_agent() binds tools to the BASE LLM
- # (bind_tools resolves via ProcessingLLMWrapper.__getattr__),
- # bypassing the wrapper's <think>-tag stripping. Reasoning-model
- # output from this agent loop is NOT think-stripped (cosmetic
- # leak only; does not crash). Known limitation — see
- # ProcessingLLMWrapper in config/llm_config.py. Do NOT "fix" by
- # re-wrapping per call; the proper fix is a Runnable wrapper
- # subclass so bind_tools stays wrapped.
+ # create_agent() calls model.bind_tools(); ProcessingLLMWrapper
+ # (config/llm_config.py) overrides bind_tools to re-wrap the
+ # bound model, so the wrapper's <think>-tag stripping survives
+ # the agent loop and runs on every model call here (fix #4804).
+ # Scope note: other Runnable transforms still escape the wrapper
+ # — with_config/bind/stream delegate via __getattr__ to the
+ # unwrapped base model (silent, unstripped), and `|` raises
+ # TypeError (the wrapper defines no __or__). None are on this
+ # create_agent path. Closing that whole class would need a full
+ # Runnable-subclass wrapper.
agent = create_agent(
model=model,
tools=sub_tools,
@@ -887,11 +889,12 @@ class LangGraphAgentStrategy(BaseSearchStrategy):
)
# Create agent — may fail if model doesn't support tool calling.
- # NOTE: create_agent() binds tools to the BASE LLM (bind_tools resolves
- # via ProcessingLLMWrapper.__getattr__), bypassing the wrapper's
- # <think>-tag stripping. Reasoning-model output from this agent loop is
- # NOT think-stripped (cosmetic leak only; does not crash). Known
- # limitation — see ProcessingLLMWrapper in config/llm_config.py.
+ # create_agent() calls model.bind_tools(); ProcessingLLMWrapper overrides
+ # bind_tools to re-wrap the bound model, so the wrapper's <think>-tag
+ # stripping survives the agent loop here (fix #4804). Other Runnable
+ # transforms still escape the wrapper (with_config/bind/stream delegate
+ # via __getattr__ unstripped; `|` raises TypeError, no __or__), but none
+ # are on this create_agent path — see config/llm_config.py.
try:
agent = create_agent(
model=self.model,
diff --git a/tests/config/test_llm_config.py b/tests/config/test_llm_config.py
index 17d5a59919..086b07093e 100644
--- a/tests/config/test_llm_config.py
+++ b/tests/config/test_llm_config.py
@@ -292,7 +292,85 @@ class TestWrapLlmWithoutThinkTags:
result = asyncio.run(bound_wrapper.ainvoke("test"))
mock_bound.ainvoke.assert_called_once_with("test")
+ # Must actually strip on the async path. Without the bind_tools
+ # override this fails: the raw bound model is returned and its
+ # ainvoke never runs _normalize_response, so content keeps the
+ # <think> block. Regression guard for #4804.
assert result is mock_response
+ assert result.content == "ok"
+
+
+class TestBindToolsCreateAgentIntegration:
+ """End-to-end guard for #4804.
+
+ The unit tests above exercise ``ProcessingLLMWrapper.bind_tools`` in
+ isolation. This class drives a REAL ``langchain.agents.create_agent`` over
+ the think-stripping wrapper and asserts the agent's final message has
+ ``<think>…</think>`` stripped — the actual path that regressed. Without the
+ ``bind_tools`` override, ``create_agent`` binds tools to the unwrapped base
+ model and the ``<think>`` block leaks into the agent's output.
+ """
+
+ @staticmethod
+ def _build_agent():
+ from langchain.agents import create_agent
+ from langchain_core.language_models.chat_models import BaseChatModel
+ from langchain_core.messages import AIMessage
+ from langchain_core.outputs import ChatGeneration, ChatResult
+ from langchain_core.tools import tool
+
+ class _FakeThinkModel(BaseChatModel):
+ """Reasoning-style model: emits a ``<think>`` block and no tool
+ calls, so ``create_agent`` runs exactly one model turn and stops."""
+
+ @property
+ def _llm_type(self) -> str:
+ return "fake-think"
+
+ def _generate(
+ self, messages, stop=None, run_manager=None, **kwargs
+ ):
+ message = AIMessage(
+ content="<think>internal reasoning</think>FINAL_ANSWER"
+ )
+ return ChatResult(generations=[ChatGeneration(message=message)])
+
+ def bind_tools(self, tools, **kwargs):
+ # The fake never emits tool_calls, so returning self is enough
+ # for create_agent to bind tools and run a single turn.
+ return self
+
+ @tool
+ def noop(query: str) -> str:
+ """No-op tool so the agent has a non-empty tool list."""
+ return query
+
+ # Patch the settings lookup so wrap_llm_without_think_tags doesn't try
+ # to read rate-limit settings from a (nonexistent) settings context.
+ with patch(
+ "local_deep_research.config.llm_config.get_setting_from_snapshot",
+ return_value=False,
+ ):
+ wrapped = wrap_llm_without_think_tags(_FakeThinkModel())
+ return create_agent(model=wrapped, tools=[noop])
+
+ def test_create_agent_invoke_strips_think_tags(self):
+ agent = self._build_agent()
+ result = agent.invoke({"messages": [{"role": "user", "content": "hi"}]})
+ final = result["messages"][-1]
+ assert "<think>" not in final.content
+ assert final.content == "FINAL_ANSWER"
+
+ def test_create_agent_ainvoke_strips_think_tags(self):
+ import asyncio
+
+ agent = self._build_agent()
+ result = asyncio.run(
+ agent.ainvoke({"messages": [{"role": "user", "content": "hi"}]})
+ )
+ final = result["messages"][-1]
+ assert "<think>" not in final.content
+ assert final.content == "FINAL_ANSWER"
class TestGetLlm: