Persistence and corpus I/O
Use this route when an index already exists or must be written for later
retrieval. It owns the low-level BM25.save, BM25.load, and
BM25.load_scores contracts, the optional saved corpus, and on-disk memory
behavior. It does not replace tokenization, scoring, high-level file ingestion,
Hub transfer, or evaluation workflows.
Choose the loading mode
- Normal local reload:
BM25.load(index_dir, load_corpus=False) loads the
numeric arrays and BM25 parameters into memory. Add load_corpus=True only
when result documents should be returned from the saved corpus.
- Large index: use
mmap=True for the three CSC score arrays. If documents
are also needed, load_corpus=True, mmap=True uses JsonlCorpus rather than
reading the whole JSONL file into a list.
- IDs-only retrieval: leave
load_corpus=False; retrieval returns document
IDs. Keep the separately managed corpus in exactly the same index order if it
will be applied later.
- Scores-only inspection: create a BM25 object and call
load_scores with
the saved array names and the known num_docs. This does not load parameters,
vocabulary, or corpus metadata; use load for a usable retriever.
Read file-format-and-api.md for exact
signatures, filenames, serialization rules, and custom-name examples. Read
large-corpus.md before loading a large index or
batching retrieval. Use troubleshooting.md for
failure diagnosis. Run the bounded local check in
scripts/save_reload_smoke.py after changing a
persistence workflow.
Safe save/reload recipe
- Tokenize and index the documents with one stable vocabulary. The corpus used
for result display is not the tokenized index: it is a positional document
store, so keep its order and length aligned with the indexed documents.
- Save the index directory.
save creates parent directories and writes the
three numeric CSC arrays, BM25 parameters, and BM25 vocabulary. Pass
corpus=... (or set BM25(corpus=...)) to write a JSONL corpus as well.
- If a
Tokenizer will be reconstructed independently, save its tokenizer
vocabulary under its default vocab.tokenizer.json or another distinct
name. Do not overwrite the BM25 model's default vocab.index.json.
- Reload using the same custom filenames used during save. For textual query
tokenization, also restore the matching tokenizer vocabulary; for integer
token IDs, ensure the IDs came from the saved BM25 vocabulary.
- Verify one small query and, when
load_corpus=True, verify that the returned
document at each result ID is the expected positional entry.
import bm25s
index = bm25s.BM25(method="bm25+")
index.index(bm25s.tokenize(["red fox", "blue whale"], stopwords=[]))
index.save(
"local-index",
corpus=[{"id": "r", "text": "red fox"}, {"id": "b", "text": "blue whale"}],
data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",
vocab_name="model-vocab.json", params_name="model-params.json",
nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",
)
reloaded = bm25s.BM25.load(
"local-index", load_corpus=True, mmap=True,
data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",
vocab_name="model-vocab.json", params_name="model-params.json",
nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",
)
The mmap flag is a loading choice, not a save format. It maps data,
indices, and indptr with NumPy read mode; it does not make indexing or
serialization incremental. The BM25L/BM25+ non-occurrence array is loaded
separately and is required for those methods.
Corpus and file invariants
- The default files are
data.csc.index.npy, indices.csc.index.npy,
indptr.csc.index.npy, vocab.index.json, params.index.json,
nonoccurrence_array.index.npy, and corpus.jsonl.
params.index.json is required to reconstruct the model. The three CSC
arrays and the vocabulary are required for a normal usable load. Missing
files should be repaired or the correct custom names supplied; do not create
empty placeholders.
load_vocab=False deliberately skips the BM25 vocabulary. In this version
the resulting vocab_dict and unique_token_ids_set are empty, so the
object is suitable for metadata/array inspection but not normal retrieval.
Use load_vocab=True for retrieval, or explicitly restore a verified matching
vocabulary before issuing queries.
load_corpus=True is best effort: if the named corpus file is absent, load
still returns the index with no loaded corpus. It does not validate that the
corpus length or order equals the indexed document count.
- Strings are written as
{"id": <position>, "text": <string>}. Top-level
dictionaries, lists, and tuples are accepted and are written as JSON. Nested
values must be JSON-serializable (null, booleans, numbers, strings, arrays,
and objects); sets, bytes, arbitrary Python objects, and most NumPy objects
are not portable JSON values.
- A document that cannot be serialized is warned about and skipped, which can
make the saved corpus shorter than the index. Treat that warning as a data
integrity failure, not as a harmless partial save. A non-iterable corpus is
not a valid
save input.
- A JSONL companion index is written beside the corpus. For
documents.jsonl
the companion is documents.mmindex.json; JsonlCorpus uses an existing
companion without checking that it is fresh. Regenerate it after editing the
JSONL file.
Security and compatibility
Keep allow_pickle=False unless a trusted legacy index explicitly requires
object-array loading. Setting it to True permits NumPy pickle handling and
must never be used to load an untrusted index directory. Numeric arrays written
by the normal save path should work with the default False setting. The
stdlib JSON fallback and the optional orjson implementation both produce the
same documented JSON-shaped corpus contract; orjson is not a reason to enable
pickle.
Install the base package for NumPy-backed local persistence. The core extra
adds optional helpers such as orjson, progress reporting, stemming, and
Numba; it is not required for the save/load file contract. Keep optional
backends explicit and CPU-safe. Do not download a benchmark corpus merely to
validate a local index.
Operational handoff
Report the index directory, exact filenames, whether mmap and
load_corpus were used, the model method, corpus count versus
scores["num_docs"], vocabulary strategy, and any skipped-document warnings.
Close a loaded JsonlCorpus when a long-lived process no longer needs it. For a
missing BM25L/BM25+ non-occurrence file or a corpus/index order mismatch, stop
retrieval and repair the artifacts rather than silently returning plausible but
wrong scores or documents.
1---2name: persistence-and-corpus-io3description: "Persist and reopen bm25s indexes and JSONL corpora safely,4 including custom filenames, mmap loading, JsonlCorpus access, vocabulary5 loading, and large-index memory planning."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/persistence-and-corpus-io9 disco-role: operating10license: MIT11---1213# Persistence and corpus I/O1415Use this route when an index already exists or must be written for later16retrieval. It owns the low-level `BM25.save`, `BM25.load`, and17`BM25.load_scores` contracts, the optional saved corpus, and on-disk memory18behavior. It does not replace tokenization, scoring, high-level file ingestion,19Hub transfer, or evaluation workflows.2021## Choose the loading mode2223- **Normal local reload:** `BM25.load(index_dir, load_corpus=False)` loads the24 numeric arrays and BM25 parameters into memory. Add `load_corpus=True` only25 when result documents should be returned from the saved corpus.26- **Large index:** use `mmap=True` for the three CSC score arrays. If documents27 are also needed, `load_corpus=True, mmap=True` uses `JsonlCorpus` rather than28 reading the whole JSONL file into a list.29- **IDs-only retrieval:** leave `load_corpus=False`; retrieval returns document30 IDs. Keep the separately managed corpus in exactly the same index order if it31 will be applied later.32- **Scores-only inspection:** create a BM25 object and call `load_scores` with33 the saved array names and the known `num_docs`. This does not load parameters,34 vocabulary, or corpus metadata; use `load` for a usable retriever.3536Read [file-format-and-api.md](references/file-format-and-api.md) for exact37signatures, filenames, serialization rules, and custom-name examples. Read38[large-corpus.md](references/large-corpus.md) before loading a large index or39batching retrieval. Use [troubleshooting.md](references/troubleshooting.md) for40failure diagnosis. Run the bounded local check in41[scripts/save_reload_smoke.py](scripts/save_reload_smoke.py) after changing a42persistence workflow.4344## Safe save/reload recipe45461. Tokenize and index the documents with one stable vocabulary. The corpus used47 for result display is not the tokenized index: it is a positional document48 store, so keep its order and length aligned with the indexed documents.492. Save the index directory. `save` creates parent directories and writes the50 three numeric CSC arrays, BM25 parameters, and BM25 vocabulary. Pass51 `corpus=...` (or set `BM25(corpus=...)`) to write a JSONL corpus as well.523. If a `Tokenizer` will be reconstructed independently, save its tokenizer53 vocabulary under its default `vocab.tokenizer.json` or another distinct54 name. Do not overwrite the BM25 model's default `vocab.index.json`.554. Reload using the same custom filenames used during save. For textual query56 tokenization, also restore the matching tokenizer vocabulary; for integer57 token IDs, ensure the IDs came from the saved BM25 vocabulary.585. Verify one small query and, when `load_corpus=True`, verify that the returned59 document at each result ID is the expected positional entry.6061```python62import bm25s6364index = bm25s.BM25(method="bm25+")65index.index(bm25s.tokenize(["red fox", "blue whale"], stopwords=[]))66index.save(67 "local-index",68 corpus=[{"id": "r", "text": "red fox"}, {"id": "b", "text": "blue whale"}],69 data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",70 vocab_name="model-vocab.json", params_name="model-params.json",71 nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",72)73reloaded = bm25s.BM25.load(74 "local-index", load_corpus=True, mmap=True,75 data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",76 vocab_name="model-vocab.json", params_name="model-params.json",77 nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",78)79```8081The `mmap` flag is a loading choice, not a save format. It maps `data`,82`indices`, and `indptr` with NumPy read mode; it does not make indexing or83serialization incremental. The BM25L/BM25+ non-occurrence array is loaded84separately and is required for those methods.8586## Corpus and file invariants8788- The default files are `data.csc.index.npy`, `indices.csc.index.npy`,89 `indptr.csc.index.npy`, `vocab.index.json`, `params.index.json`,90 `nonoccurrence_array.index.npy`, and `corpus.jsonl`.91- `params.index.json` is required to reconstruct the model. The three CSC92 arrays and the vocabulary are required for a normal usable load. Missing93 files should be repaired or the correct custom names supplied; do not create94 empty placeholders.95- `load_vocab=False` deliberately skips the BM25 vocabulary. In this version96 the resulting `vocab_dict` and `unique_token_ids_set` are empty, so the97 object is suitable for metadata/array inspection but not normal retrieval.98 Use `load_vocab=True` for retrieval, or explicitly restore a verified matching99 vocabulary before issuing queries.100- `load_corpus=True` is best effort: if the named corpus file is absent, load101 still returns the index with no loaded corpus. It does not validate that the102 corpus length or order equals the indexed document count.103- Strings are written as `{"id": <position>, "text": <string>}`. Top-level104 dictionaries, lists, and tuples are accepted and are written as JSON. Nested105 values must be JSON-serializable (null, booleans, numbers, strings, arrays,106 and objects); sets, bytes, arbitrary Python objects, and most NumPy objects107 are not portable JSON values.108- A document that cannot be serialized is warned about and skipped, which can109 make the saved corpus shorter than the index. Treat that warning as a data110 integrity failure, not as a harmless partial save. A non-iterable corpus is111 not a valid `save` input.112- A JSONL companion index is written beside the corpus. For `documents.jsonl`113 the companion is `documents.mmindex.json`; `JsonlCorpus` uses an existing114 companion without checking that it is fresh. Regenerate it after editing the115 JSONL file.116117## Security and compatibility118119Keep `allow_pickle=False` unless a trusted legacy index explicitly requires120object-array loading. Setting it to `True` permits NumPy pickle handling and121must never be used to load an untrusted index directory. Numeric arrays written122by the normal save path should work with the default `False` setting. The123stdlib JSON fallback and the optional `orjson` implementation both produce the124same documented JSON-shaped corpus contract; `orjson` is not a reason to enable125pickle.126127Install the base package for NumPy-backed local persistence. The `core` extra128adds optional helpers such as `orjson`, progress reporting, stemming, and129Numba; it is not required for the save/load file contract. Keep optional130backends explicit and CPU-safe. Do not download a benchmark corpus merely to131validate a local index.132133## Operational handoff134135Report the index directory, exact filenames, whether `mmap` and136`load_corpus` were used, the model method, corpus count versus137`scores["num_docs"]`, vocabulary strategy, and any skipped-document warnings.138Close a loaded `JsonlCorpus` when a long-lived process no longer needs it. For a139missing BM25L/BM25+ non-occurrence file or a corpus/index order mismatch, stop140retrieval and repair the artifacts rather than silently returning plausible but141wrong scores or documents.