hnswlib
hnswlib
What it is and what it does
Hnswlib is a Python binding to a header-only C++ implementation of the Hierarchical Navigable Small World (HNSW) algorithm for fast approximate nearest neighbor search. It lets you build an index of high-dimensional vectors and query them efficiently to find the k nearest neighbors, with support for three distance metrics: squared L2, inner product, and cosine distance. The library is designed for incremental workflows—you can add, update, and delete vectors after index creation, mark elements as deleted without rebuilding, and save/load indexes to disk.
The main dependency is numpy for array handling. Installation requires a C++ compiler because the package compiles a native extension at install time. Once built, it offers thread-safe batch operations for both insertion and querying, with tunable parameters (M and ef_construction) to trade off memory footprint and construction speed against query accuracy. It is commonly used in machine learning and information retrieval pipelines where you need to find similar embeddings or vectors quickly without exhaustive search.
Use it for:
- Build a semantic search engine over document embeddings to find similar texts or documents by vector similarity.
- Implement real-time product recommendation by indexing product embeddings and querying for nearest neighbors to a user's preference vector.
- Create a reverse image search system by indexing image feature vectors and finding visually similar images.
- Deduplicate large datasets by indexing all embeddings and finding near-duplicate vectors within a distance threshold.
- Support incremental machine learning pipelines where new training examples are continuously added to an index for online similarity lookup.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Fast approximate nearest neighbor search using the HNSW algorithm with Python bindings, supporting incremental index construction, updates, and deletions on vector data.
Yes, with conditions. Hnswlib is actively maintained, has no known vulnerabilities, and is popular (571766 monthly downloads). Install it if you need fast approximate nearest neighbor search and can handle the C++ compilation requirement. However, verify the license before use in proprietary contexts, and be aware that the last release was 985 days ago—check whether the current version meets your needs or if you need to build from the active repository.
Install
hnswlib on PyPI
pip
pip install hnswlibuv
uv add hnswlibpoetry
poetry add hnswlibInstalling hnswlib
Before you install
High install friction due to C++ compilation requirement (header-only HNSW implementation). Package is actively maintained with recent commits and a large repository (5309 stars), but the last release was 985 days ago despite ongoing development activity.
License in practice
License treatment is unclear—no SPDX identifier or raw license text is available in the metadata. Verify the actual license before use in proprietary or restricted contexts.
Quickstart
pip install hnswlib
import hnswlib
import numpy as np
index = hnswlib.Index(space='l2', dim=16)
index.init_index(max_elements=100, M=16, ef_construction=200)
data = np.random.random((100, 16))
index.add_items(data, np.arange(100))
labels, distances = index.knn_query(data[:5], k=5)
Requires a C++ compiler and build tools to compile the C++ extension during installation.
Verify before relying
- Whether Python version support is truly unspecified or if there are undocumented minimum/maximum version constraints.
- Current maintenance status and release cadence given the 985-day gap between latest release and active repository commits.
- Whether the package is suitable for production use given the release timing relative to active development.
Package facts
| License | not declared (unclear) |
| Python support | not specified |
| Install friction | high — source build required |
| Runtime dependencies | 1 — numpy |
| Maintenance | actively maintained — 985 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 571,766/month — #5,948 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: hnswlib-0.8.0.tar.gz
Tags
More Scientific/Engineering packages
NumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
pandaspandas provides fast, flexible data structures…
permissive · top 100 on PyPI
scipyscipy provides numerical algorithms for…
permissive · top 100 on PyPI
scikit-learnscikit-learn provides a comprehensive Python…
permissive · top 1,000 on PyPI
dilldill extends Python's pickle module to…
permissive · top 1,000 on PyPI
multiprocessMultiprocess is an enhanced fork of Python's…
permissive · top 1,000 on PyPI
pyspark-hnswProvides a PySpark-compatible implementation of…
unclear · top 15,000 on PyPI
voyagerVoyager performs fast approximate…
permissive · top 15,000 on PyPI
usearchUSearch provides approximate nearest-neighbor…
permissive · top 15,000 on PyPI
nmslibnmslib provides efficient similarity search in…
unclear · top 15,000 on PyPI
cuvs-cu12Provides GPU-accelerated approximate nearest…
permissive · top 15,000 on PyPI
libcuvs-cu12GPU-accelerated vector search and clustering…
permissive · top 15,000 on PyPI
pynndescentPyNNDescent builds approximate nearest neighbor…
permissive · top 5,000 on PyPI
annoyAnnoy searches for approximate nearest…
permissive · top 5,000 on PyPI
strsimpyImplements a dozen string similarity and…
permissive · top 15,000 on PyPI