From ee0c38e0816e773ff0087068009ae6c8529eb41c Mon Sep 17 00:00:00 2001 From: Barry <91018388+barry166@users.noreply.github.com> Date: Mon, 29 Jun 2026 17:23:22 +0800 Subject: [PATCH] fix(cli): handle null memory fields in output formatters (#5957) --- cli/python/src/mem0_cli/output.py | 12 ++++++------ cli/python/tests/test_output.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cli/python/src/mem0_cli/output.py b/cli/python/src/mem0_cli/output.py index f9b8d2d35..8aad44bbb 100644 --- a/cli/python/src/mem0_cli/output.py +++ b/cli/python/src/mem0_cli/output.py @@ -20,8 +20,8 @@ def format_memories_text(console: Console, memories: list[dict], title: str = "m console.print(f"\n[{BRAND_COLOR}]Found {count} {title}:[/]\n") for i, mem in enumerate(memories, 1): - memory_text = mem.get("memory", mem.get("text", "")) - mem_id = mem.get("id", "")[:8] + memory_text = mem.get("memory") or mem.get("text") or "" + mem_id = (mem.get("id") or "")[:8] score = mem.get("score") created = _format_date(mem.get("created_at")) category = mem.get("categories", [None]) @@ -67,8 +67,8 @@ def format_memories_table( table.add_column("Created", max_width=12) for mem in memories: - mem_id = mem.get("id", "") - memory_text = mem.get("memory", mem.get("text", "")) + mem_id = mem.get("id") or "" + memory_text = mem.get("memory") or mem.get("text") or "" if len(memory_text) > 60: memory_text = memory_text[:57] + "..." categories = mem.get("categories", []) @@ -104,8 +104,8 @@ def format_single_memory(console: Console, mem: dict, output: str = "text") -> N format_json(console, mem) return - memory_text = mem.get("memory", mem.get("text", "")) - mem_id = mem.get("id", "") + memory_text = mem.get("memory") or mem.get("text") or "" + mem_id = mem.get("id") or "" lines = [] lines.append(f" [white bold]{memory_text}[/]") diff --git a/cli/python/tests/test_output.py b/cli/python/tests/test_output.py index e9256ad00..cbcc6c733 100644 --- a/cli/python/tests/test_output.py +++ b/cli/python/tests/test_output.py @@ -54,6 +54,11 @@ class TestTextFormat: output = buf.getvalue() assert "Found 0" in output + def test_format_memories_text_handles_null_fields(self): + console, buf = _make_console() + format_memories_text(console, [{"id": None, "memory": None, "created_at": None}]) + assert "Found 1 memories" in buf.getvalue() + class TestTableFormat: def test_format_memories_table(self): @@ -70,6 +75,13 @@ class TestTableFormat: # Should still render (empty table) assert "ID" in output + def test_format_memories_table_handles_null_fields(self): + console, buf = _make_console() + format_memories_table(console, [{"id": None, "memory": None, "created_at": None}]) + output = buf.getvalue() + assert "ID" in output + assert "Memory" in output + class TestSingleMemory: def test_format_single_memory_text(self): @@ -87,6 +99,17 @@ class TestSingleMemory: output = buf.getvalue() assert '"memory"' in output + def test_format_single_memory_handles_null_fields(self): + console, buf = _make_console() + format_single_memory( + console, + {"id": None, "memory": None, "text": "Fallback memory", "created_at": None}, + "text", + ) + output = buf.getvalue() + assert "Fallback memory" in output + assert "ID:" not in output + class TestAddResult: def test_format_add_result_text(self):