Skip to content

armgit / mem0

Public Imported from GitHub
0

The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.

fix(faiss): return [[]] for uninitialized index to honor list() contract (#5725)

Bartok299c42321306
diff --git a/mem0/vector_stores/faiss.py b/mem0/vector_stores/faiss.py
index 103b99739..e99fc9224 100644
--- a/mem0/vector_stores/faiss.py
+++ b/mem0/vector_stores/faiss.py
@@ -603,7 +603,7 @@ class FAISS(VectorStoreBase):
List[OutputData]: List of vectors.
"""
if self.index is None:
- return []
+ return [[]]
results = []
count = 0
diff --git a/tests/vector_stores/test_faiss.py b/tests/vector_stores/test_faiss.py
index 6333b119a..29dc7a208 100644
--- a/tests/vector_stores/test_faiss.py
+++ b/tests/vector_stores/test_faiss.py
@@ -285,6 +285,20 @@ def test_list(faiss_instance):
assert result.payload["category"] == "A"
+def test_list_uninitialized_index_returns_nested_list(faiss_instance):
+ # Regression for the List[List[OutputData]] contract: callers (e.g.
+ # Memory.delete_all) do `vector_store.list(filters=...)[0]`, so an
+ # uninitialized index must return [[]] (one level deep) and NOT a bare
+ # [], which would make result[0] raise IndexError on an empty store.
+ faiss_instance.index = None
+
+ results = faiss_instance.list()
+
+ assert results == [[]]
+ # The contract callers rely on: result[0] is the (empty) memory list.
+ assert results[0] == []
+
+
def test_col_info(faiss_instance, mock_faiss_index):
# Mock index attributes
mock_faiss_index.ntotal = 5