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: preserve sequential IDs for legacy indexes

Abi69f0185e8e02
diff --git a/packages/leann-core/src/leann/cli.py b/packages/leann-core/src/leann/cli.py
index 0b56acc..7b9c36c 100644
--- a/packages/leann-core/src/leann/cli.py
+++ b/packages/leann-core/src/leann/cli.py
@@ -1913,16 +1913,16 @@ Examples:
def _existing_index_id_scheme(self, index_path: str) -> Optional[str]:
"""Return the passage_id_scheme recorded in an existing index's meta.json.
- Returns None when the index doesn't exist yet or the field isn't
- recorded (older indexes pre-#330). Callers should treat None as
- "fall back to whatever the args say or the default".
+ Returns None when the index doesn't exist yet. Older indexes pre-#330
+ have a meta file without passage_id_scheme and must be treated as
+ sequential so incremental updates never mix ID schemes.
"""
meta_path = Path(index_path).with_suffix(".leann.meta.json")
if not meta_path.exists():
return None
try:
with open(meta_path, encoding="utf-8") as f:
- return json.load(f).get("passage_id_scheme")
+ return json.load(f).get("passage_id_scheme", "sequential")
except Exception:
return None
diff --git a/tests/test_passage_id_scheme.py b/tests/test_passage_id_scheme.py
index e921d0a..e83c7ac 100644
--- a/tests/test_passage_id_scheme.py
+++ b/tests/test_passage_id_scheme.py
@@ -24,6 +24,25 @@ def test_builder_content_hash_passage_ids_are_content_stable():
assert builder.chunks[2]["id"] == _content_id("different text")
+def test_existing_index_id_scheme_treats_legacy_meta_as_sequential(tmp_path):
+ cli = LeannCLI()
+ index_path = tmp_path / "legacy.leann"
+
+ assert cli._existing_index_id_scheme(str(index_path)) is None
+
+ meta_path = tmp_path / "legacy.leann.meta.json"
+ meta_path.write_text(json.dumps({"version": "1.0", "backend_name": "hnsw"}), encoding="utf-8")
+
+ assert cli._existing_index_id_scheme(str(index_path)) == "sequential"
+
+ meta_path.write_text(
+ json.dumps({"version": "1.1", "backend_name": "hnsw", "passage_id_scheme": "content-hash"}),
+ encoding="utf-8",
+ )
+
+ assert cli._existing_index_id_scheme(str(index_path)) == "content-hash"
+
+
def test_migrate_ids_rewrites_passages_offsets_idmap_meta_and_bm25(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
index_dir = tmp_path / ".leann" / "indexes" / "sample"