NumPy Vector Store
A fast, lightweight, zero-setup in-memory vector store powered by NumPy.
- Tiny local vector search for projects that do not need a vector database
- Fast exact vector search using vectorized NumPy operations
- Simple typed API returning
VectorHit(index, value, metadata) - Composable filtering by passing prefiltered row indexes with
within_rows - Portable persistence as versioned, self-describing trusted local
.npzfiles - No framework opinions: bring your own embeddings, chunking, async, and metadata model
Why?
This library is purpose-built for small to medium-scale vector search tasks and offers a simple alternative to heavyweight vector databases when you do not need network services, indexing infrastructure, ingestion pipelines, or domain-specific metadata filtering.
When/Where?
Below are benchmark results for cosine similarity search to help you assess its suitability for your use case.
| Embedding Type | Dimensions | ~5ms | ~25ms | ~100ms | ~500ms |
|---|---|---|---|---|---|
| Sentence Transformers | 384 | 1K vectors 1.5MB |
10K vectors 15MB |
100K vectors 147MB |
500K vectors 732MB |
| OpenAI Small | 1536 | 500 vectors 3MB |
5K vectors 29MB |
25K vectors 147MB |
100K vectors 586MB |
| OpenAI Large | 3072 | 200 vectors 2MB |
2.5K vectors 29MB |
5K vectors 59MB |
25K vectors 293MB |
Benchmarks performed on Apple M2 hardware.
Installation
uv add numpy-vector-store
Quick Start
import numpy as np
from numpy_vector_store import VectorStore
store = VectorStore[dict[str, str]](dimensions=3)
store.add(
vectors=np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]),
metadata=[
{"title": "x-axis"},
{"title": "y-axis"},
{"title": "z-axis"},
],
)
hits = store.cosine_search(
query=np.array([0.9, 0.1, 0.0]),
top_k=2,
)
for hit in hits:
print(f"{hit.metadata['title']}: {hit.value:.3f}")
metadata is an outer sequence with one opaque payload for each vector row.
Each payload can be a dict, dataclass, tuple, list, string, integer row ID, or
another Python object that fits your application. Tuple and list payloads remain
single row values rather than being interpreted as additional array dimensions.
Normalization
VectorStore defaults to normalize=True, which scales each stored vector to
length 1. Normalization preserves vector direction while discarding magnitude:
[3.0, 4.0] -> [0.6, 0.8]
This is the default because it makes cosine similarity fast and direction-only,
which is the common case for semantic embeddings. Use normalize=False when
vector length matters, such as when magnitude encodes strength, confidence,
counts, scale, or raw geometry.
Zero vectors are rejected when normalize=True because they cannot be scaled to
unit length. Raw stores accept zero vectors for dot-product and Euclidean
search. Because cosine similarity is undefined for zero vectors,
cosine_search raises an error when its selected rows include one; use
within_rows to exclude zero rows when needed.
Numerical inputs
Stored vectors use float32 to keep the store compact. Vectors and queries must
remain finite when converted to float32, and search thresholds must also be
finite. Invalid values are rejected before they can affect stored state or
ranking.
Norms and raw metric values use float64 accumulation where float32
intermediate calculations could overflow or underflow. This allows finite
float32 vectors across the representable magnitude range to be normalized and
compared reliably.
| Method | normalize=True default |
normalize=False |
|---|---|---|
cosine_search |
True cosine similarity over stored unit vectors; fastest/default path for embeddings | True cosine similarity over raw vectors; computes vector norms during search |
dot_search |
Dot product of unit vectors, effectively equivalent to cosine similarity | True dot product over original vectors; use when magnitude should affect ranking |
euclidean_search |
Distance between normalized directions; useful only when direction-normalized distance is intended | True Euclidean distance over original vectors; use for geometric/feature-space nearest neighbors |
get |
Returns normalized vectors | Returns original vectors |
save |
Saves normalized vectors | Saves raw vectors |
load |
Loads and normalizes vectors | Loads vectors exactly as stored |
Search Methods
Use cosine_search for semantic embeddings and direction-only similarity:
hits = store.cosine_search(query, top_k=10, min_value=0.75)
Use dot_search with normalize=False when larger-magnitude vectors should
rank higher:
store = VectorStore[dict[str, str]](dimensions=3, normalize=False)
store.add(vectors, metadata)
hits = store.dot_search(query, top_k=10, min_value=0.0)
Use euclidean_search with normalize=False for raw coordinate or feature-space
nearest-neighbor search:
store = VectorStore[dict[str, str]](dimensions=3, normalize=False)
store.add(vectors, metadata)
hits = store.euclidean_search(query, top_k=10, max_value=1.5)
Prefiltering
The store does not implement a metadata query language. To filter by metadata,
produce row indexes first, then pass them with within_rows.
rows = [
i
for i, metadata in enumerate(store.metadata)
if metadata["title"].startswith("x")
]
hits = store.cosine_search(query, top_k=10, within_rows=rows)
Searches without within_rows compute directly against the stored vector matrix
and do not make a full copy of it. A filtered search gathers the selected rows
into a temporary matrix, so its additional memory use scales with the number of
selected rows and the vector dimensions. Omit within_rows when every row
should be searched; passing every row explicitly would create an unnecessary
full-size temporary matrix.
For structured NumPy metadata, use NumPy to produce the row indexes:
metadata_table = np.array(
[
("intro", "A", 2024),
("setup", "A", 2023),
("guide", "B", 2024),
],
dtype=[("title", "U20"), ("product", "U10"), ("year", "i4")],
)
store = VectorStore[int](dimensions=3)
store.add(vectors, metadata=np.arange(len(metadata_table)))
mask = (metadata_table["product"] == "A") & (metadata_table["year"] >= 2024)
rows = np.flatnonzero(mask)
hits = store.cosine_search(query, within_rows=rows)
for hit in hits:
row = metadata_table[hit.metadata]
print(row["title"], hit.value)
Persistence
Create a new store normally, then supply its destination on the first save:
store = VectorStore[dict[str, str]](dimensions=1536)
store.add(embeddings, metadata)
store.save("vectors.npz")
save(path) writes the archive and binds that path to the store. Later
save() calls update the bound archive. Passing another path performs a Save
As operation; the new path becomes the binding only after the write succeeds.
Open an existing store directly from its self-describing archive:
store = VectorStore[dict[str, str]].open("vectors.npz")
open() restores dimensions and normalize from the archive and binds its
path. Use reload() when the file may have changed externally and you
explicitly want to discard current in-memory changes:
store.reload()
Unlike the transitional load() method, reload() always rereads the bound
archive and raises if the store is unbound, the file is missing, or the archive
is invalid. A failed reload leaves the current in-memory vectors and metadata
unchanged.
The .npz suffix may be omitted. An extensionless path such as "vectors" is
resolved to "vectors.npz" for saving, opening, and reloading.
Raw-vector configuration is also restored from the archive:
store = VectorStore[dict[str, str]](
dimensions=1536,
normalize=False,
)
store.add(raw_vectors, metadata)
store.save("raw-vectors.npz")
loaded = VectorStore[dict[str, str]].open("raw-vectors.npz")
assert loaded.normalize is False
Archives written by 0.4 use format version 1 and contain format_version,
dimensions, normalize, vectors, and metadata. The stored configuration
prevents an archive from being loaded with different dimensions or normalization
semantics.
Opening and reloading validate the complete schema, array dtypes and shapes, row counts, finite vector values, and zero-norm behavior before changing in-memory state. Opaque metadata values remain individual row payloads across persistence round trips.
Each save writes a uniquely named temporary archive in the destination
directory, closes it, and then replaces the destination with os.replace.
Readers opening the destination path therefore see either the previous complete
archive or the new complete archive rather than a partially written file. If
writing or replacement fails, the previous destination remains in place and the
temporary file is removed.
Atomic replacement is not file locking or multi-writer coordination. Concurrent writers can still replace one another, and the library does not promise that a successful save has reached durable hardware storage across every operating system or power failure.
Older archives containing only vectors and metadata remain readable in 0.4
through the configuration-aware legacy API:
legacy = VectorStore[dict[str, str]](
dimensions=1536,
file_path="legacy-vectors.npz",
normalize=True,
)
legacy.load()
legacy.save()
Loading a legacy archive emits a FutureWarning, and saving rewrites it as
format version 1. open() intentionally rejects unversioned archives because
they do not contain enough configuration to construct a store safely. The
legacy reader will be removed in 0.5; migrate an archive once with 0.4 or
recreate it from source data.
Constructor file_path=, instance load(), and direct context-manager
persistence remain available with FutureWarning during the 0.4 transition.
The explicit lifecycle shown above is the preferred API and will be the only
persistence API in 0.5. See the persistence migration guide for
side-by-side replacements and the one-time legacy archive conversion.
Metadata persistence uses allow_pickle=True for flexible Python payloads, so
only load files generated by your own application or another trusted local
process. Loading untrusted .npz files is not a supported security model.
Compatibility
This project is still pre-1.0, so occasional breaking changes are expected while the API stabilizes. Changes are documented in the changelog and GitHub release notes. Deprecated APIs will keep warning for at least one point release before removal.
Version 0.4 supports Python 3.11 through 3.14 and NumPy 1.23.2 or newer. These versions are listed in the package metadata and exercised in CI, including a dedicated check against the minimum NumPy version. Python 3.10 remains supported by the 0.3 release series but is not supported by 0.4.
The project generally retains stable CPython versions until their upstream end-of-life, adds new versions after its dependencies and CI support them, and drops versions only in minor releases.
See the changelog for release history and the project roadmap for the planned path to stable API and persistence contracts. Persistence users upgrading from the 0.3 API should also read the migration guide.
Contributing
git clone https://github.com/tvanreenen/numpy-vector-store.git
cd numpy-vector-store
uv sync --frozen --group dev
Before submitting a pull request:
- Run
uv run ruff check - Run
uv run ruff format --check - Run
uv run mypy src/ - Run
uv run pytest
License
MIT License - see LICENSE file for details.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numpy_vector_store-0.4.0.tar.gz.
File metadata
- Download URL: numpy_vector_store-0.4.0.tar.gz
- Upload date:
- Size: 60.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02cc26fc2b49e5f3bae0c55b0e4fa3c41280e9cc12b3e7e19abb8666fb357b3f
|
|
| MD5 |
caa42043277f787b1e18c67b5e17f3e5
|
|
| BLAKE2b-256 |
edac01d0c9ed06d435d7f0238a26c656ff098163fb191495e38ec65d56b9e04b
|
Provenance
The following attestation bundles were made for numpy_vector_store-0.4.0.tar.gz:
Publisher:
publish-pypi.yml on tvanreenen/numpy-vector-store
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_vector_store-0.4.0.tar.gz -
Subject digest:
02cc26fc2b49e5f3bae0c55b0e4fa3c41280e9cc12b3e7e19abb8666fb357b3f - Sigstore transparency entry: 2389582250
- Sigstore integration time:
-
Permalink:
tvanreenen/numpy-vector-store@ee07ca03603121fa05afc1057fbd09726408b98a -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/tvanreenen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@ee07ca03603121fa05afc1057fbd09726408b98a -
Trigger Event:
release
-
Statement type:
File details
Details for the file numpy_vector_store-0.4.0-py3-none-any.whl.
File metadata
- Download URL: numpy_vector_store-0.4.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63e15a6e0365895de02d4cdedc866b43d751a7a4fb3e08ed5e06d7336d73d1df
|
|
| MD5 |
320b25f8aab75fd2c1bd54dc1cd1964e
|
|
| BLAKE2b-256 |
25668fe5c9412c98aec96987d38e32829ee9f249bbd94bebda923752e35b3a3e
|
Provenance
The following attestation bundles were made for numpy_vector_store-0.4.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on tvanreenen/numpy-vector-store
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_vector_store-0.4.0-py3-none-any.whl -
Subject digest:
63e15a6e0365895de02d4cdedc866b43d751a7a4fb3e08ed5e06d7336d73d1df - Sigstore transparency entry: 2389582863
- Sigstore integration time:
-
Permalink:
tvanreenen/numpy-vector-store@ee07ca03603121fa05afc1057fbd09726408b98a -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/tvanreenen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@ee07ca03603121fa05afc1057fbd09726408b98a -
Trigger Event:
release
-
Statement type: