From 51857c90082a55e820f2f8d8f8d3bb7d5526b099 Mon Sep 17 00:00:00 2001 From: Afonso Coutinho Date: Wed, 3 Jun 2026 05:25:48 +0100 Subject: [PATCH] fix: chat memory extraction crashes on a non-dict message (#1749) --- src/memory.py | 2 ++ tests/test_memory_extract_chat_nondict.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/test_memory_extract_chat_nondict.py diff --git a/src/memory.py b/src/memory.py index 2254c28..edb4767 100644 --- a/src/memory.py +++ b/src/memory.py @@ -51,6 +51,8 @@ class MemoryManager: memories = [] for msg in chat_history: + if not isinstance(msg, dict): + continue if msg.get("role") == "assistant": content = str(msg.get("content", "")) lines = content.split('\n') diff --git a/tests/test_memory_extract_chat_nondict.py b/tests/test_memory_extract_chat_nondict.py new file mode 100644 index 0000000..44b2c3c --- /dev/null +++ b/tests/test_memory_extract_chat_nondict.py @@ -0,0 +1,15 @@ +from src.memory import MemoryManager + + +def test_extract_memory_from_chat_skips_non_dict_messages(tmp_path): + # chat_history rows can be malformed (a non-dict slipping in from a partial + # session blob); the old loop did msg.get(...) and crashed on the first one. + m = MemoryManager(str(tmp_path)) + history = [ + {"role": "assistant", "content": "- remember to buy milk"}, + "junk-msg", + None, + {"role": "user", "content": "hi"}, + ] + out = m.extract_memory_from_chat(history) + assert any(e["text"] == "remember to buy milk" for e in out)