Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(thinking): leave a thinking block with a null or non-string value unchanged (#4562)

HetCreepcc1eb5402aad
diff --git a/headroom/transforms/thinking_compactor.py b/headroom/transforms/thinking_compactor.py
index 46fda82d9..c03afd2e7 100644
--- a/headroom/transforms/thinking_compactor.py
+++ b/headroom/transforms/thinking_compactor.py
@@ -162,7 +162,10 @@ def compact_thinking_to_text(
if not (isinstance(block, dict) and block.get("type") == "thinking"):
new_content.append(block)
continue
- text = block.get("thinking", "")
+ text = block.get("thinking")
+ if not isinstance(text, str): # null/non-str: leave the block as it is
+ new_content.append(block)
+ continue
words = len(text.split())
if words < min_words:
new_content.append(block)
diff --git a/tests/test_thinking_compactor_version_gate.py b/tests/test_thinking_compactor_version_gate.py
index 07238dcfb..8df296b6d 100644
--- a/tests/test_thinking_compactor_version_gate.py
+++ b/tests/test_thinking_compactor_version_gate.py
@@ -51,3 +51,23 @@ def test_date_suffix_is_not_read_as_minor_version() -> None:
assert bills_prior_thinking("claude-sonnet-4-20250514") is False
# Sanity: the real 4.6 id still bills.
assert bills_prior_thinking("claude-sonnet-4-6-20260101") is True
+
+
+def test_compact_thinking_to_text_leaves_null_thinking_block_alone() -> None:
+ """``"thinking": null`` must not raise (docstring: "Never raises")."""
+ from headroom.transforms.thinking_compactor import compact_thinking_to_text
+
+ class _K:
+ def compress(self, text, **kwargs):
+ return type("R", (), {"compressed": text[:10]})()
+
+ null_block = {"type": "thinking", "thinking": None, "signature": "s"}
+ msgs = [
+ {"role": "user", "content": "q"},
+ {"role": "assistant", "content": [null_block, {"type": "text", "text": "a"}]},
+ {"role": "user", "content": "q2"},
+ {"role": "assistant", "content": [{"type": "text", "text": "b"}]},
+ ]
+ out, stats = compact_thinking_to_text(msgs, kompress=_K())
+ assert out[1]["content"][0] == null_block
+ assert stats["blocks"] == 0