fix(memory): skip messages without a content key in message parsers (#5575)
This commit is contained in:
@@ -1,10 +1,50 @@
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from mem0.memory.utils import parse_vision_messages, remove_spaces_from_entities, sanitize_relationship_for_cypher
|
||||
from mem0.memory.utils import (
|
||||
parse_messages,
|
||||
parse_vision_messages,
|
||||
remove_spaces_from_entities,
|
||||
sanitize_relationship_for_cypher,
|
||||
)
|
||||
|
||||
|
||||
class TestParseMessages:
|
||||
def test_skips_message_without_content_key(self):
|
||||
# Reproduces #5067: a FunctionCalling assistant message carries
|
||||
# `tool_calls` but no `content` key -> used to raise KeyError.
|
||||
messages = [
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "tool_calls": [{"id": "1", "function": {"name": "search"}}]},
|
||||
{"role": "assistant", "content": "done"},
|
||||
]
|
||||
result = parse_messages(messages)
|
||||
assert result == "user: hi\nassistant: done\n"
|
||||
|
||||
def test_skips_explicit_none_content(self):
|
||||
messages = [{"role": "assistant", "content": None}, {"role": "user", "content": "ok"}]
|
||||
assert parse_messages(messages) == "user: ok\n"
|
||||
|
||||
def test_plain_roles_pass_through(self):
|
||||
messages = [
|
||||
{"role": "system", "content": "sys"},
|
||||
{"role": "user", "content": "u"},
|
||||
{"role": "assistant", "content": "a"},
|
||||
]
|
||||
assert parse_messages(messages) == "system: sys\nuser: u\nassistant: a\n"
|
||||
|
||||
|
||||
class TestParseVisionMessages:
|
||||
def test_skips_message_without_content_key(self):
|
||||
# Reproduces #5067 for the vision parser path.
|
||||
messages = [
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "tool_calls": [{"id": "1", "function": {"name": "search"}}]},
|
||||
]
|
||||
result = parse_vision_messages(messages, llm=None)
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"role": "user", "content": "hi"}
|
||||
|
||||
def test_multimodal_list_without_llm_extracts_text(self):
|
||||
messages = [
|
||||
{"role": "user", "content": [
|
||||
|
||||
Reference in New Issue
Block a user