fix: handle missing parameters in Mem0Storage.search() to prevent KeyError

- Replace unsafe del statements with safe .pop() calls
- Add comprehensive tests for missing parameter scenarios
- Fixes issue #3266 where KeyError occurred with local_mem0_config

The issue occurred when using Memory (OSS) with local_mem0_config where
parameters like 'metadata', 'version', 'output_format' might not be
present in the params dictionary. Using .pop() with None default
safely removes parameters without raising KeyError.

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-08-03 16:00:43 +00:00
parent 88ed91561f
commit ab1a6714b5
2 changed files with 56 additions and 3 deletions

View File

@@ -363,3 +363,55 @@ def test_search_method_with_agent_id_and_user_id():
assert len(results) == 2
assert results[0]["content"] == "Result 1"
def test_search_method_with_memory_oss_missing_params():
"""Test search method handles missing parameters gracefully when using Memory (OSS)"""
config = {
"user_id": "test_user",
"local_mem0_config": {
"vector_store": {"provider": "mock_vector_store"},
"llm": {"provider": "mock_llm"},
}
}
mock_memory = MagicMock(spec=Memory)
mock_results = {"results": [{"score": 0.9, "content": "Result 1"}]}
mock_memory.search = MagicMock(return_value=mock_results)
with patch("mem0.memory.main.Memory.from_config", return_value=mock_memory):
mem0_storage = Mem0Storage(type="external", config=config)
results = mem0_storage.search("test query", limit=5, score_threshold=0.5)
mock_memory.search.assert_called_once_with(
query="test query",
limit=5,
user_id="test_user",
filters={"AND": [{"user_id": "test_user"}]},
threshold=0.5
)
assert len(results) == 1
assert results[0]["content"] == "Result 1"
def test_search_method_with_memory_oss_no_optional_params():
"""Test search method works when no optional parameters are present"""
mock_memory = MagicMock(spec=Memory)
mock_results = {"results": []}
mock_memory.search = MagicMock(return_value=mock_results)
with patch("mem0.memory.main.Memory", return_value=mock_memory):
mem0_storage = Mem0Storage(type="external", config={})
results = mem0_storage.search("test query")
mock_memory.search.assert_called_once_with(
query="test query",
limit=3,
filters={},
threshold=0.35
)
assert len(results) == 0