~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
fix(strategy): handle list-form LLM content in LangGraph agent (#6244)
Faisal Hussain089d9cc3d5d9
diff --git a/changelog.d/6244.bugfix.md b/changelog.d/6244.bugfix.mdnew file mode 100644index 0000000000..3696cd2e3f--- /dev/null+++ b/changelog.d/6244.bugfix.md@@ -0,0 +1 @@+Fix list-form LLM content handling in LangGraph agent strategy consumers to prevent downstream type errors and Python list-representation leakage, including observation, reasoning, final-answer, and fallback synthesis paths.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.pyindex f08938d653..11e761457a 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@@ -49,6 +49,7 @@ from ...utilities.url_utils import (library_display_url,preferred_chunk_display,)+from ...utilities.json_utils import get_llm_response_textfrom ...database.thread_local_session import thread_cleanupfrom ..tools.fetch import FETCH_MODES, build_fetch_tool, make_library_resolverfrom .base_strategy import (@@ -2201,7 +2202,7 @@ class LangGraphAgentStrategy(BaseSearchStrategy):"""tool_name = getattr(msg, "name", "tool")display_name = self._display_tool_name(tool_name)- raw = str(getattr(msg, "content", ""))+ raw = get_llm_response_text(msg)# Suppress the misleading "📄 From the page: Cannot fetch <url>: ..."# pattern for ``fetch_content`` denial/error observations. The fetch@@ -2691,7 +2692,7 @@ class LangGraphAgentStrategy(BaseSearchStrategy):for msg in msgs:if isinstance(msg, AIMessage):agent_messages.append(msg)- content = msg.content or ""+ content = get_llm_response_text(msg)tool_calls = getattr(msg, "tool_calls", [])# Surface the model's *thinking* output (the@@ -2803,15 +2804,10 @@ class LangGraphAgentStrategy(BaseSearchStrategy):logger.warning("LangGraph agent hit recursion limit, synthesizing partial results")- if not final_content:- final_content = self._synthesize_from_collector(query)except Exception as exc:logger.exception("LangGraph agent error")- if not final_content:- if self.collector.results:- final_content = self._synthesize_from_collector(query)- else:- return self._error_result(self._format_agent_error(exc))+ if not final_content and not self.collector.results:+ return self._error_result(self._format_agent_error(exc))if not final_content:if self.collector.results:@@ -2849,11 +2845,7 @@ class LangGraphAgentStrategy(BaseSearchStrategy):)try:response = self.model.invoke(prompt)- return (- response.content- if hasattr(response, "content")- else str(response)- )+ return get_llm_response_text(response)except Exception as exc:logger.exception("Fallback synthesis failed")return _scrub_tool_error(diff --git a/tests/strategies/test_langgraph_agent_strategy.py b/tests/strategies/test_langgraph_agent_strategy.pyindex 92d5896e04..b42ff2de9e 100644--- a/tests/strategies/test_langgraph_agent_strategy.py+++ b/tests/strategies/test_langgraph_agent_strategy.py@@ -31,6 +31,10 @@ from local_deep_research.advanced_search_system.strategies.primary_search_metadaPrimarySourceScope,PrimarySourceType,)+from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ _OBSERVATION_DETAIL_MAX_CHARS,+ _OBSERVATION_PREVIEW_MAX_CHARS,+)from local_deep_research.security.egress import EngineClassification@@ -2763,22 +2767,27 @@ class TestObservationEvent:def test_message_is_flattened_150_char_preview(self):strategy = self._make_strategy()- content = "line one\nline two " + "x" * 200+ content = "line one\nline two " + "x" * (+ _OBSERVATION_PREVIEW_MAX_CHARS + 50+ )message, _ = strategy._observation_event(self._msg(content=content))assert message.startswith("📄 From the web (SearXNG): ")preview = message.split("📄 From the web (SearXNG): ", 1)[1]- assert len(preview) == 150+ assert len(preview) == _OBSERVATION_PREVIEW_MAX_CHARSassert "\n" not in messageassert preview.startswith("line one line two ")def test_metadata_carries_full_detail_with_newlines(self):+strategy = self._make_strategy()content = "\n\n".join(f"[{i}] Title {i} (http://a{i}.com)\nSnippet text for result {i}"for i in range(1, 6))- assert len(content) > 150 # long enough that the preview truncates+ assert (+ len(content) > _OBSERVATION_PREVIEW_MAX_CHARS+ ) # long enough that the preview truncatesmessage, metadata = strategy._observation_event(self._msg(content=content))@@ -2806,7 +2815,7 @@ class TestObservationEvent:attached — length alone must not gate it."""strategy = self._make_strategy()content = "Title: Foo Bar\nURL: http://example.com\nSnippet: short"- assert len(content) <= 150+ assert len(content) <= _OBSERVATION_PREVIEW_MAX_CHARS_, metadata = strategy._observation_event(self._msg(content=content))assert metadata["content"] == content@@ -2925,6 +2934,151 @@ class TestObservationEvent:assert metadata["phase"] == "observation"assert metadata["tool"] == tool_name+ def test_list_content_observation_preview_and_detail(self):+ """List-form tool observation must produce a preview truncated to+ ``_OBSERVATION_PREVIEW_MAX_CHARS`` with flattened newlines, while+ metadata detail preserves original newlines without list repr (#4615)."""+ strategy = self._make_strategy()+ excess = 50+ raw_text = "Line 1: Search results\nLine 2: " + "a" * (+ _OBSERVATION_PREVIEW_MAX_CHARS + excess+ )+ assert (+ _OBSERVATION_PREVIEW_MAX_CHARS+ < len(raw_text)+ <= _OBSERVATION_DETAIL_MAX_CHARS+ )++ content = [{"type": "text", "text": raw_text}]+ message, metadata = strategy._observation_event(+ self._msg(name="web_search", content=content)+ )++ prefix = "📄 From the web (SearXNG): "+ assert message.startswith(prefix)+ preview = message[len(prefix) :]++ # 1. Preview is truncated according to the configured preview limit+ assert len(preview) == _OBSERVATION_PREVIEW_MAX_CHARS+ # 2. Preview newlines are flattened+ assert "\n" not in message+ expected_preview = raw_text[:_OBSERVATION_PREVIEW_MAX_CHARS].replace(+ "\n", " "+ )+ assert preview == expected_preview+ assert "[{" not in message+ assert "'type'" not in message++ # 3. Detail preserves the original text/newlines without repr leakage+ assert metadata["content"] == raw_text+ assert "\n" in metadata["content"]+ assert "[{" not in metadata["content"]+ assert "'type'" not in metadata["content"]++ def test_list_content_observation_detail_bounded_by_configured_limit(self):+ """List-form tool observation exceeding ``_OBSERVATION_DETAIL_MAX_CHARS``+ must bound metadata detail to the configured limit with ellipsis (#4615)."""+ strategy = self._make_strategy()+ excess = 250+ raw_text = "Header line\nBody: " + "b" * (+ _OBSERVATION_DETAIL_MAX_CHARS + excess+ )+ content = [{"type": "text", "text": raw_text}]+ _, metadata = strategy._observation_event(+ self._msg(name="web_search", content=content)+ )++ expected_detail = raw_text[:_OBSERVATION_DETAIL_MAX_CHARS] + " …"+ assert metadata["content"] == expected_detail+ assert metadata["content"].startswith("Header line\nBody: ")+ assert "[{" not in metadata["content"]+ assert "'type'" not in metadata["content"]++ def test_list_content_fetch_denial_returns_none(self):+ """List-form fetch_content denial must be detected by startswith+ and return None rather than leaking the repr under 'From the page:' (#4615)."""+ strategy = self._make_strategy()+ denial = [+ {+ "type": "text",+ "text": (+ "Cannot fetch https://example.com/page: blocked by egress "+ "policy (scope_mismatch_private_only)."+ ),+ }+ ]+ assert (+ strategy._observation_event(+ self._msg(name="fetch_content", content=denial)+ )+ is None+ )++ error = [+ {+ "type": "text",+ "text": (+ "Error fetching https://example.com/page: ConnectionError('boom') "+ "(check your internet connection)."+ ),+ }+ ]+ assert (+ strategy._observation_event(+ self._msg(name="fetch_content", content=error)+ )+ is None+ )+++# ---------------------------------------------------------------------------+# Fallback synthesis list-content extraction (#4615)+# ---------------------------------------------------------------------------+++class TestSynthesizeFromCollectorListContent:+ """Pin ``LangGraphAgentStrategy._synthesize_from_collector`` list-content+ extraction (issue #4615)."""++ def _make_strategy(self):+ from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ LangGraphAgentStrategy,+ )++ return LangGraphAgentStrategy(+ model=MagicMock(),+ search=MagicMock(),+ all_links_of_system=[],+ settings_snapshot={"search.tool": {"value": "searxng"}},+ )++ def test_synthesize_from_collector_extracts_list_content(self):+ """Fallback synthesis model response with list-form content blocks must+ extract clean text rather than returning a raw list or leaking repr (#4615)."""+ strategy = self._make_strategy()+ strategy.collector.add_results(+ [+ {+ "title": "Source 1",+ "snippet": "Snippet 1",+ "link": "https://example.com/1",+ }+ ]+ )+ response = MagicMock()+ response.content = [+ {"type": "text", "text": "Synthesized fallback answer."},+ {"type": "tool_use", "name": "search", "input": {}},+ ]+ strategy.model.invoke.return_value = response++ result = strategy._synthesize_from_collector("test query")++ assert isinstance(result, str)+ assert result == "Synthesized fallback answer."+ assert "[{" not in result+ assert "'type'" not in result+# ---------------------------------------------------------------------------# Step heartbeat (full tool listing)@@ -3629,6 +3783,150 @@ class TestQueryParameterNotClobbered:assert result["findings"][0]["question"] == original_query+# ---------------------------------------------------------------------------+# analyze_topic list-content regression tests (#4615)+# ---------------------------------------------------------------------------+++class TestAnalyzeTopicListContent:+ """Regression suite for list-form LLM content in ``analyze_topic`` (issue #4615).++ Anthropic/Bedrock extended thinking and tool use can return message content+ as a list of content blocks rather than a plain string. Proves:+ 1. Tool-calling AIMessage with list content feeds clean text to reasoning progress.+ 2. Non-tool-calling AIMessage with list content populates final_content.+ 3. Both flow through real unmocked _finalize without TypeErrors or repr leaks.+ """++ def _make_strategy(self):+ from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ LangGraphAgentStrategy,+ )++ return LangGraphAgentStrategy(+ model=MagicMock(),+ search=MagicMock(),+ all_links_of_system=[],+ settings_snapshot={"search.tool": {"value": "mock"}},+ )++ def test_analyze_topic_list_content_handles_reasoning_and_final_answer(+ self,+ ):+ """End-to-end stream flow: iteration 1 tests reasoning from list content,+ iteration 2 tests final_content from list content, flowing to real _finalize."""+ from langchain_core.messages import AIMessage++ progress_calls = []+ strategy = self._make_strategy()+ strategy.progress_callback = lambda msg, pct, meta: (+ progress_calls.append((msg, pct, meta))+ )++ # Iteration 1: tool call with list-form reasoning content+ agent_msg_iter1 = AIMessage(+ content=[+ {+ "type": "text",+ "text": "Reasoning: need to search for Paris landmarks.",+ }+ ],+ tool_calls=[+ {+ "name": "web_search",+ "args": {"query": "Paris landmarks"},+ "id": "call_1",+ "type": "tool_call",+ }+ ],+ )+ tool_obs_msg = SimpleNamespace(+ name="web_search",+ content="Eiffel Tower and Louvre Museum are top landmarks.",+ )++ # Iteration 2: final answer (no tool calls) with list-form content+ agent_msg_iter2 = AIMessage(+ content=[+ {+ "type": "text",+ "text": "Paris is renowned for the Eiffel Tower and Louvre [1].",+ }+ ],+ )++ def stream_events():+ yield {"agent": {"messages": [agent_msg_iter1]}}+ strategy.collector.add_results(+ [+ {+ "title": "Paris Landmarks",+ "snippet": "Eiffel Tower and Louvre Museum",+ "link": "https://example.com/paris",+ }+ ]+ )+ yield {"tools": {"messages": [tool_obs_msg]}}+ yield {"agent": {"messages": [agent_msg_iter2]}}++ mock_agent = MagicMock()+ mock_agent.stream.side_effect = lambda *args, **kwargs: stream_events()++ # Mock citation handler to echo the final answer+ strategy.citation_handler = MagicMock()+ strategy.citation_handler.analyze_followup.return_value = {+ "content": "Paris is renowned for the Eiffel Tower and Louvre [1].",+ "documents": [],+ }++ with (+ patch.object(strategy, "_build_tools", return_value=[MagicMock()]),+ patch("langchain.agents.create_agent", return_value=mock_agent),+ ):+ result = strategy.analyze_topic("Tell me about Paris")++ # 1. Reasoning progress received clean text without list repr+ reasoning_updates = [+ msg+ for msg, _, meta in progress_calls+ if meta.get("phase") == "agent_reasoning"+ ]+ assert len(reasoning_updates) == 1+ assert (+ "Reasoning: need to search for Paris landmarks."+ in reasoning_updates[0]+ )+ assert "[{" not in reasoning_updates[0]+ assert "'type'" not in reasoning_updates[0]++ # 2. Citation handler received clean string previous_knowledge+ assert strategy.citation_handler.analyze_followup.called+ prev_kw = (+ strategy.citation_handler.analyze_followup.call_args.kwargs.get(+ "previous_knowledge"+ )+ )+ assert (+ prev_kw == "Paris is renowned for the Eiffel Tower and Louvre [1]."+ )+ assert isinstance(prev_kw, str)++ # 3. Real unmocked _finalize completed cleanly with string findings+ assert result["error"] is None+ final_text = result["findings"][0]["content"]+ assert isinstance(final_text, str)+ assert (+ final_text+ == "Paris is renowned for the Eiffel Tower and Louvre [1]."+ )+ assert "[{" not in final_text+ assert "'type'" not in final_text+ assert (+ result["current_knowledge"]+ == "Paris is renowned for the Eiffel Tower and Louvre [1]."+ )++class TestProgressMetadataKeepsStableId:"""Progress metadata ``tool`` must carry the STABLE tool id while thehuman-readable engine label appears only in the message text.