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(memory): handle list-shaped system content in inline memory injection (#3794)

Abhay Singh717527bc8681
diff --git a/headroom/memory/inline_extractor.py b/headroom/memory/inline_extractor.py
index 0d36de161..d04b9ae63 100644
--- a/headroom/memory/inline_extractor.py
+++ b/headroom/memory/inline_extractor.py
@@ -61,6 +61,22 @@ class ParsedResponse:
raw: str | None # Original full response
+def _append_instruction(content: Any, instruction: str) -> Any:
+ """Append the memory instruction to a system prompt's content.
+
+ ``content`` may be a plain string or a list of content-part dicts (OpenAI
+ allows ``content`` as a list; Anthropic system prompts are commonly a list of
+ ``{"type": "text", ...}`` blocks). Concatenating a str onto a list raises
+ ``TypeError``, so a list gets the instruction appended as a trailing text
+ part instead. Any other shape falls back to a string join.
+ """
+ if isinstance(content, list):
+ return [*content, {"type": "text", "text": instruction}]
+ if isinstance(content, str):
+ return content + instruction
+ return f"{content}{instruction}" if content else instruction
+
+
def inject_memory_instruction(
messages: list[dict[str, Any]],
short: bool = True,
@@ -83,7 +99,7 @@ def inject_memory_instruction(
if msg.get("role") == "system":
messages[i] = {
**msg,
- "content": msg.get("content", "") + instruction,
+ "content": _append_instruction(msg.get("content", ""), instruction),
}
has_system = True
break
diff --git a/tests/test_inline_extractor_inject.py b/tests/test_inline_extractor_inject.py
new file mode 100644
index 000000000..3ca98287b
--- /dev/null
+++ b/tests/test_inline_extractor_inject.py
@@ -0,0 +1,61 @@
+"""Tests for inline_extractor.inject_memory_instruction system-prompt handling.
+
+The system prompt's ``content`` can be a plain string or a list of content-part
+dicts (OpenAI allows a list; Anthropic system prompts are commonly a list of
+``{"type": "text", ...}`` blocks). The instruction must be appended without
+crashing on the list form.
+"""
+
+from __future__ import annotations
+
+from headroom.memory.inline_extractor import (
+ MEMORY_INSTRUCTION,
+ MEMORY_INSTRUCTION_SHORT,
+ inject_memory_instruction,
+)
+
+
+def test_str_system_content_is_concatenated() -> None:
+ out = inject_memory_instruction(
+ [{"role": "system", "content": "You are X."}, {"role": "user", "content": "hi"}],
+ short=True,
+ )
+ assert out[0]["content"] == "You are X." + MEMORY_INSTRUCTION_SHORT
+
+
+def test_list_system_content_appends_a_text_part() -> None:
+ """Regression: a list-shaped system content used to raise
+ ``TypeError: can only concatenate list (not "str") to list``."""
+ out = inject_memory_instruction(
+ [
+ {"role": "system", "content": [{"type": "text", "text": "You are X."}]},
+ {"role": "user", "content": "hi"},
+ ],
+ short=True,
+ )
+ content = out[0]["content"]
+ assert isinstance(content, list)
+ assert content[0] == {"type": "text", "text": "You are X."}
+ assert content[-1] == {"type": "text", "text": MEMORY_INSTRUCTION_SHORT}
+ # The original list is not mutated in place.
+ assert len(content) == 2
+
+
+def test_list_system_content_long_instruction() -> None:
+ out = inject_memory_instruction(
+ [{"role": "system", "content": [{"type": "text", "text": "sys"}]}],
+ short=False,
+ )
+ assert out[0]["content"][-1] == {"type": "text", "text": MEMORY_INSTRUCTION}
+
+
+def test_missing_system_prepends_default() -> None:
+ out = inject_memory_instruction([{"role": "user", "content": "hi"}], short=True)
+ assert out[0]["role"] == "system"
+ assert MEMORY_INSTRUCTION_SHORT in out[0]["content"]
+
+
+def test_original_messages_not_mutated() -> None:
+ original = [{"role": "system", "content": [{"type": "text", "text": "sys"}]}]
+ inject_memory_instruction(original, short=True)
+ assert original[0]["content"] == [{"type": "text", "text": "sys"}]