From 89038d37ba5abb140505131d5149dcc2b1e5e3fd Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 14 Apr 2025 06:54:13 +0000 Subject: [PATCH] Fix issue #2599: Memory.search() unexpected keyword argument 'metadata' with local Mem0 Co-Authored-By: Joe Moura --- src/crewai/memory/storage/mem0_storage.py | 2 ++ tests/storage/test_mem0_storage.py | 44 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/crewai/memory/storage/mem0_storage.py b/src/crewai/memory/storage/mem0_storage.py index ccf8cc810..9e06b97b1 100644 --- a/src/crewai/memory/storage/mem0_storage.py +++ b/src/crewai/memory/storage/mem0_storage.py @@ -117,6 +117,8 @@ class Mem0Storage(Storage): # Discard the filters for now since we create the filters # automatically when the crew is created. results = self.memory.search(**params) + if isinstance(results, dict) and 'results' in results: + return [r for r in results['results'] if r["score"] >= score_threshold] return [r for r in results if r["score"] >= score_threshold] def _get_user_id(self) -> str: diff --git a/tests/storage/test_mem0_storage.py b/tests/storage/test_mem0_storage.py index f9e56739f..2e443f3a9 100644 --- a/tests/storage/test_mem0_storage.py +++ b/tests/storage/test_mem0_storage.py @@ -15,6 +15,7 @@ from crewai.task import Task class MockCrew: def __init__(self, memory_config): self.memory_config = memory_config + self.agents = [] # Add empty agents list for testing @pytest.fixture @@ -153,3 +154,46 @@ def test_mem0_storage_with_explict_config( mem0_storage_with_memory_client_using_explictly_config.memory_config == expected_config ) + + +def test_mem0_storage_external_memory_search( + mem0_storage_with_memory_client_using_config_from_crew, mock_mem0_memory_client +): + """Test that Mem0Storage search correctly handles external memory with metadata parameter.""" + mem0_storage_with_memory_client_using_config_from_crew.memory_type = "external" + + mock_search_response = { + 'results': [ + { + 'id': '1', + 'score': 0.9, + 'metadata': {'type': 'external'}, + 'memory': 'test external memory 1' + }, + { + 'id': '2', + 'score': 0.8, + 'metadata': {'type': 'external'}, + 'memory': 'test external memory 2' + }, + { + 'id': '3', + 'score': 0.3, # Below threshold + 'metadata': {'type': 'external'}, + 'memory': 'test external memory 3' + } + ] + } + + mock_mem0_memory_client.search.return_value = mock_search_response + + results = mem0_storage_with_memory_client_using_config_from_crew.search( + "test query", + score_threshold=0.5 + ) + + mock_mem0_memory_client.search.assert_called_once() + + assert len(results) == 2 + assert results[0]['score'] == 0.9 + assert results[1]['score'] == 0.8