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): tolerate None message content in inline memory parser (#3211)

Abhay Singh5ccec67f0c3a
diff --git a/headroom/memory/inline_extractor.py b/headroom/memory/inline_extractor.py
index f560ae3ed..0d36de161 100644
--- a/headroom/memory/inline_extractor.py
+++ b/headroom/memory/inline_extractor.py
@@ -56,9 +56,9 @@ Skip for greetings/small talk. If nothing: <memory>{"memories": []}</memory>"""
class ParsedResponse:
"""Response with extracted memories."""
- content: str # The actual response (without memory block)
+ content: str | None # The actual response (without memory block); None for tool-call turns
memories: list[dict[str, Any]] # Extracted memories
- raw: str # Original full response
+ raw: str | None # Original full response
def inject_memory_instruction(
@@ -101,11 +101,12 @@ def inject_memory_instruction(
return messages
-def parse_response_with_memory(response_text: str) -> ParsedResponse:
+def parse_response_with_memory(response_text: str | None) -> ParsedResponse:
"""Parse LLM response to extract memories.
Args:
- response_text: Raw LLM response
+ response_text: Raw LLM response. May be ``None`` when the model answered
+ with tool/function calls instead of text.
Returns:
ParsedResponse with content and memories separated
@@ -113,6 +114,15 @@ def parse_response_with_memory(response_text: str) -> ParsedResponse:
memories: list[dict[str, Any]] = []
content = response_text
+ # A chat completion's ``message.content`` is ``None`` whenever the model
+ # answers with tool/function calls instead of text, so the two call sites
+ # here forward ``None`` (and any non-str payload) straight in. ``re.search``
+ # on a non-str raises ``TypeError``, which crashed the whole request. Return
+ # the payload untouched — preserving ``None`` so a tool-call response is not
+ # coerced to text — with no memories to extract.
+ if not isinstance(response_text, str):
+ return ParsedResponse(content=response_text, memories=memories, raw=response_text)
+
# Extract <memory> block
memory_pattern = r"<memory>\s*(.*?)\s*</memory>"
match = re.search(memory_pattern, response_text, re.DOTALL | re.IGNORECASE)
@@ -179,7 +189,7 @@ class InlineMemoryWrapper:
model: str = "gpt-4o-mini",
short_instruction: bool = True,
**kwargs: Any,
- ) -> tuple[str, list[dict[str, Any]]]:
+ ) -> tuple[str | None, list[dict[str, Any]]]:
"""Send chat request and extract memories inline.
Args:
@@ -213,7 +223,7 @@ class InlineMemoryWrapper:
messages: list[dict[str, Any]],
model: str = "gpt-4o-mini",
**kwargs: Any,
- ) -> tuple[Any, str, list[dict[str, Any]]]:
+ ) -> tuple[Any, str | None, list[dict[str, Any]]]:
"""Send chat request and return full response object.
Args:
diff --git a/tests/test_memory_wrapper.py b/tests/test_memory_wrapper.py
index 0bd009713..5907766a3 100644
--- a/tests/test_memory_wrapper.py
+++ b/tests/test_memory_wrapper.py
@@ -224,3 +224,17 @@ def test_parse_response_with_memory_tolerates_non_object_memory_block() -> None:
)
assert parsed.memories == [{"content": "User likes Python"}]
assert parsed.content == "text"
+
+
+def test_parse_response_with_memory_tolerates_none_content() -> None:
+ from headroom.memory.inline_extractor import parse_response_with_memory
+
+ # `chat.completions` returns `message.content == None` whenever the model
+ # answers with tool/function calls instead of text. The callers forward that
+ # `None` straight in, where `re.search(pattern, None)` used to raise
+ # `TypeError` and crash the whole request. It must degrade gracefully and
+ # preserve `None` (not coerce a tool-call turn to text).
+ parsed = parse_response_with_memory(None)
+ assert parsed.content is None
+ assert parsed.memories == []
+ assert parsed.raw is None