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.

fix(llm): skip object content blocks that declare a non-text type (#7609)

Lukasc8e351cea89e
diff --git a/changelog.d/7609.bugfix.md b/changelog.d/7609.bugfix.md
new file mode 100644
index 0000000000..1b8a492bbb
--- /dev/null
+++ b/changelog.d/7609.bugfix.md
@@ -0,0 +1 @@
+LLM replies returned as a list of content blocks no longer pick up the text of tool-result or thinking block objects; only blocks typed as text (or untyped) contribute to the parsed reply.
diff --git a/src/local_deep_research/utilities/json_utils.py b/src/local_deep_research/utilities/json_utils.py
index 6cecba47c3..0f248593ac 100644
--- a/src/local_deep_research/utilities/json_utils.py
+++ b/src/local_deep_research/utilities/json_utils.py
@@ -60,7 +60,8 @@ def _coerce_content_blocks(blocks: list) -> str:
Accepts blocks that are plain strings, dicts with a ``"text"`` key (when
``type`` is ``"text"`` or unset), or objects exposing a ``.text`` str
- attribute. Returns ``""`` for an empty/textless list.
+ attribute (when their ``type`` attribute, if any, is ``"text"``).
+ Returns ``""`` for an empty/textless list.
"""
parts: list[str] = []
for block in blocks:
@@ -72,6 +73,10 @@ def _coerce_content_blocks(blocks: list) -> str:
):
parts.append(block["text"])
else:
+ # A tool_result or thinking block object can expose .text too.
+ # As for dicts, only an untyped or "text"-typed block counts.
+ if getattr(block, "type", "text") != "text":
+ continue
text_attr = getattr(block, "text", None)
if isinstance(text_attr, str):
parts.append(text_attr)
diff --git a/tests/utilities/test_json_utils.py b/tests/utilities/test_json_utils.py
index 025daf0f9a..aae46d3722 100644
--- a/tests/utilities/test_json_utils.py
+++ b/tests/utilities/test_json_utils.py
@@ -1,7 +1,10 @@
"""Tests for json_utils module."""
+from types import SimpleNamespace
from unittest.mock import Mock
+import pytest
+
class TestGetLlmResponseText:
"""Tests for get_llm_response_text function."""
@@ -74,6 +77,38 @@ class TestGetLlmResponseText:
response.content = 42
assert get_llm_response_text(response) == "42"
+ def test_object_blocks_keep_only_text_typed_text(self):
+ """An object block that declares a non-text type is not reply text,
+ even when it exposes a string ``.text``."""
+ from local_deep_research.utilities.json_utils import (
+ get_llm_response_text,
+ )
+
+ response = Mock()
+ response.content = [
+ SimpleNamespace(type="text", text="Paris"),
+ SimpleNamespace(type="tool_result", text="5"),
+ SimpleNamespace(type="thinking", text="let me think"),
+ SimpleNamespace(text="!"),
+ ]
+ assert get_llm_response_text(response) == "Paris!"
+
+ @pytest.mark.parametrize("block_type", [7, None, ["text"]])
+ def test_object_and_dict_blocks_share_the_type_rule(self, block_type):
+ """Only an untyped or "text"-typed block is text, whatever the
+ shape of the block or of a foreign ``type`` value."""
+ from local_deep_research.utilities.json_utils import (
+ get_llm_response_text,
+ )
+
+ response = Mock()
+ response.content = [
+ SimpleNamespace(type=block_type, text="object"),
+ {"type": block_type, "text": "dict"},
+ "!",
+ ]
+ assert get_llm_response_text(response) == "!"
+
def test_removes_paired_think_tags(self):
"""Test that paired <think>...</think> tags are removed."""
from local_deep_research.utilities.json_utils import (