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): keep multimodal user content intact when injecting memories (#4648)

HetCreep07d5add5ba31
diff --git a/headroom/memory/wrapper.py b/headroom/memory/wrapper.py
index 0f55d35df..7053a58d1 100644
--- a/headroom/memory/wrapper.py
+++ b/headroom/memory/wrapper.py
@@ -27,6 +27,19 @@ from headroom.memory.models import Memory
logger = logging.getLogger(__name__)
+def _text_of(content: Any) -> str:
+ """Return the text of a message's content: a string, or the text parts of a list."""
+ if isinstance(content, list):
+ return "\n".join(
+ part["text"]
+ for part in content
+ if isinstance(part, dict)
+ and part.get("type") == "text"
+ and isinstance(part.get("text"), str)
+ )
+ return str(content)
+
+
class MemoryWrapper:
"""Wraps an LLM client to add automatic memory with zero extra latency.
@@ -140,12 +153,18 @@ class MemoryWrapper:
if not user_content:
return messages
+ # Multimodal content is a list of parts; search on its text parts only,
+ # never on the list's repr.
+ query = _text_of(user_content)
+ if not query:
+ return messages
+
# Search for relevant memories (async -> sync)
loop = asyncio.new_event_loop()
try:
memories = loop.run_until_complete(
self._memory.search(
- query=str(user_content),
+ query=query,
user_id=self._user_id,
session_id=self._session_id,
top_k=self._top_k,
@@ -169,7 +188,12 @@ class MemoryWrapper:
for msg in new_messages:
if msg.get("role") == "user":
original = msg.get("content", "")
- msg["content"] = f"{context_block}\n\n{original}"
+ if isinstance(original, list):
+ # Keep the parts (images, files) as they are; add the
+ # context as a leading text part.
+ msg["content"] = [{"type": "text", "text": context_block}, *original]
+ else:
+ msg["content"] = f"{context_block}\n\n{original}"
break
return new_messages
diff --git a/tests/test_memory_wrapper.py b/tests/test_memory_wrapper.py
index 5907766a3..02b24922d 100644
--- a/tests/test_memory_wrapper.py
+++ b/tests/test_memory_wrapper.py
@@ -121,6 +121,46 @@ def test_inject_memories_handles_empty_and_inserts_context() -> None:
}
+def test_inject_memories_keeps_multimodal_content_parts() -> None:
+ """List (multimodal) user content keeps its parts; the context is a new text part.
+
+ See https://github.com/headroomlabs-ai/headroom/issues/4362 (item 10).
+ """
+ client, _response = make_client()
+ fake_memory = FakeMemory()
+ wrapper = MemoryWrapper(client, user_id="alice", _memory=fake_memory)
+ fake_memory.search_results = [
+ SimpleNamespace(memory=SimpleNamespace(content="Prefers Python")),
+ ]
+ image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
+ parts = [{"type": "text", "text": "What is in this chart?"}, image]
+ original = [{"role": "user", "content": parts}]
+
+ injected = wrapper._inject_memories(original)
+
+ assert original[0]["content"] == parts
+ assert injected[0]["content"] == [
+ {"type": "text", "text": "<context>\n- Prefers Python\n</context>"},
+ {"type": "text", "text": "What is in this chart?"},
+ image,
+ ]
+ assert fake_memory.last_search["query"] == "What is in this chart?"
+
+
+def test_inject_memories_skips_search_for_content_without_text() -> None:
+ client, _response = make_client()
+ fake_memory = FakeMemory()
+ wrapper = MemoryWrapper(client, user_id="alice", _memory=fake_memory)
+ fake_memory.search_results = [
+ SimpleNamespace(memory=SimpleNamespace(content="Prefers Python")),
+ ]
+ image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
+ messages = [{"role": "user", "content": [image]}]
+
+ assert wrapper._inject_memories(messages) == messages
+ assert not hasattr(fake_memory, "last_search")
+
+
def test_store_memories_persists_only_nonempty_content() -> None:
client, _response = make_client()
fake_memory = FakeMemory()