Skip to content

armgit / LEANN

Public Imported from GitHub
0

[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.

fix: set ntotal for storage as well (#129)

Andy Leee58810067439
diff --git a/packages/leann-core/src/leann/api.py b/packages/leann-core/src/leann/api.py
index 1c8ab55..07d8373 100644
--- a/packages/leann-core/src/leann/api.py
+++ b/packages/leann-core/src/leann/api.py
@@ -735,6 +735,20 @@ class LeannBuilder:
storage_index = faiss.IndexFlatL2(index.d)
index.storage = storage_index
index.own_fields = True
+ # Faiss expects storage.ntotal to reflect the existing graph's
+ # population (even if the vectors themselves were pruned from disk
+ # for recompute mode). When we attach a fresh IndexFlat here its
+ # ntotal starts at zero, which later causes IndexHNSW::add to
+ # believe new "preset" levels were provided and trips the
+ # `n0 + n == levels.size()` assertion. Seed the temporary storage
+ # with the current ntotal so Faiss maintains the proper offset for
+ # incoming vectors.
+ try:
+ storage_index.ntotal = index.ntotal
+ except AttributeError:
+ # Older Faiss builds may not expose ntotal as a writable
+ # attribute; in that case we fall back to the default behaviour.
+ pass
if index.d != embedding_dim:
raise ValueError(
f"Existing index dimension ({index.d}) does not match new embeddings ({embedding_dim})."