From 299c4232130669ecdca569246b1d553e7df0c1cc Mon Sep 17 00:00:00 2001 From: Bartok Date: Mon, 22 Jun 2026 02:27:06 -0400 Subject: [PATCH] fix(faiss): return [[]] for uninitialized index to honor list() contract (#5725) --- mem0/vector_stores/faiss.py | 2 +- tests/vector_stores/test_faiss.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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