knn-inspect for Python
Reads OpenSearch k-NN native segment files (.faiss, .faissc)
offline: verifies them, reports graph statistics, and measures recall
against brute force. These are the five commands of the
knn-inspect CLI, as Python
functions. Neither Faiss nor Java is needed.
pip install knn-inspect
import numpy as np
import knn_inspect
report = knn_inspect.verify("_2_165_vec.faiss", space="cosine")
print(report["status"]) # clean, warnings, or failed
print(report.text) # as `knn-inspect verify` prints it
queries = np.random.default_rng(1).random((100, 128), dtype=np.float32)
print(knn_inspect.recall("_2_165_vec.faiss", queries=queries, k=10)["recall"])
Functions
| Function | The command |
|---|---|
info(path) |
A native file's layout, dimensions, HNSW parameters or IVF lists, envelope |
verify(path, *, space=None, max_findings=5) |
Every structural check |
stats(path) |
Per-level graph statistics, or an IVF index's list sizes |
recall(path, *, queries=None, sample=None, ...) |
Search results, the tool's own or given, scored against exact brute force |
scan(path, *, space=None, max_findings=5) |
verify and a summary for every native file under a directory |
Options are keyword-only, named as the command's flags (--max-findings
as max_findings), with the command's defaults; help(knn_inspect.recall)
lists recall's.
Each function returns a Report: a dict equal to what json.loads
gives of the command's --json output, described field by field in
docs/json.md,
with the command's text as report.text.
recall takes its inputs as files, as the command does, or as data:
queries: a buffer (a numpy array,array.array,bytes) of float32 vectors, or for a binary file of packed bits, or a sequence of float vectors. A 2-D array's row width is held to the file's dimension; a 1-D buffer is read as a queries file's bytes are.results: a sequence of label sequences, one per query, in query order.ids: a sequence of ints naming node i asids[i], when the result lists use ids other than the file's labels.
Scoring a live index's results
recall scores one segment's native file, whose labels are the
segment's Lucene doc ids, while a search hit carries its _id. To score
what a live index returned, give the index one shard merged to one
segment, store each document's position in a field, name the hits by it,
and pass ids: each document's position in doc-id order. With
opensearch-py:
import numpy as np
from opensearchpy import OpenSearch, helpers
import knn_inspect
client = OpenSearch("http://localhost:9200")
name, dim, k = "vectors", 32, 10
rng = np.random.default_rng(0)
vectors = rng.random((2000, dim), dtype=np.float32)
# One shard, and each document's position stored beside its vector.
client.indices.create(index=name, body={
"settings": {"index": {"number_of_shards": 1, "number_of_replicas": 0, "knn": True}},
"mappings": {"properties": {
"pos": {"type": "integer"},
"vec": {"type": "knn_vector", "dimension": dim,
"method": {"name": "hnsw", "engine": "faiss", "space_type": "l2"}},
}},
})
helpers.bulk(client, ({"_index": name, "_id": str(i), "pos": i, "vec": v.tolist()}
for i, v in enumerate(vectors)))
# One segment: merged, then refreshed and flushed so the old ones go.
client.indices.forcemerge(index=name, max_num_segments=1)
client.indices.refresh(index=name)
client.indices.flush(index=name)
queries = rng.random((50, dim), dtype=np.float32)
responses = [
client.search(index=name, body={
"size": k,
"query": {"knn": {"vec": {"vector": q.tolist(), "k": k}}},
"_source": ["pos"],
})
for q in queries
]
results = [[h["_source"]["pos"] for h in r["hits"]["hits"]] for r in responses]
# Each document's pos in doc-id order: node i of the segment is ids[i].
order = client.search(index=name, body={"size": len(vectors), "sort": ["_doc"], "_source": ["pos"]})
ids = [h["_source"]["pos"] for h in order["hits"]["hits"]]
Then copy the shard's index directory off the node: it is
nodes/0/indices/<uuid>/0/index under the node's data path, the uuid
from client.indices.get_settings(index=name)[name]["settings"]["index"]["uuid"].
Copy the whole directory, not only the native file: for a field whose
graph holds no vectors (on_disk), recall reads the segment's .vec
as well. The native file is .faiss, or .faissc in a segment small
enough for Lucene's compound file.
from pathlib import Path
segment = next(Path("index").glob("*.faiss*"))
report = knn_inspect.recall(segment, queries=queries, results=results, ids=ids, k=k)
print(report["recall"])
Leave out ids and the positions are read as doc ids, which need not
follow insertion order once segments have merged: the recall can then
come out wrong, with no error. An index of more than 10,000 documents
needs search_after for the _doc-sorted listing, past the default
index.max_result_window.
Errors and warnings
A report that cannot be built raises knn_inspect.Error, its message
the command's error line with options named as keywords. Options refused
before the file is opened, contradictory or out of range, raise
knn_inspect.OptionsError, which is also a ValueError. What recall
warns of (for a graph without its vectors, what scan's check of the
field's files finds that does not stop the search) is issued as a
knn_inspect.RecallWarning before the report is returned or the error
raised.
The functions release the GIL while they run.
Building from source
With a Rust toolchain (the repository's rust-toolchain.toml) and a
virtual environment active, from python/:
pip install maturin pytest numpy
maturin develop --release
pytest
The tests compare every report with the CLI's on the repository's
fixtures, building the CLI with cargo.
Metadata
Release files for knn-inspect 0.18.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| knn_inspect-0.18.0.tar.gz | 352.2 kB | With provenance | Details for knn_inspect-0.18.0.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| knn_inspect-0.18.0-cp311-abi3-win_amd64.whl | CPython 3.11 | abi3 | Windows x86-64 | Details for knn_inspect-0.18.0-cp311-abi3-win_amd64.whl |
| knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_x86_64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ x86-64 | Details for knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_x86_64.whl |
| knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_aarch64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ ARM64 | Details for knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_aarch64.whl |
| knn_inspect-0.18.0-cp311-abi3-macosx_11_0_arm64.whl | CPython 3.11 | abi3 | macOS 11.0+ ARM64 | Details for knn_inspect-0.18.0-cp311-abi3-macosx_11_0_arm64.whl |
Total release size: 3.3 MB
Release files / knn_inspect-0.18.0.tar.gz
| Download URL | knn_inspect-0.18.0.tar.gz |
|---|---|
| Size | 352.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c0a0c74e0c2202d83d0ed41df9106ed74df7e1c44350ef310f495b1b1119891
|
|
BLAKE2b-256 checksum How to use checksums |
39f542fc7fcae9aa5349b808d9e9e76e357ea2804762333547608e1ef6a099d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / knn_inspect-0.18.0-cp311-abi3-win_amd64.whl
| Download URL | knn_inspect-0.18.0-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 631.5 kB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
07b9c223d307dc76f374c8f11c19bdf55257b1c4827616abb9678fafef627fc7
|
|
BLAKE2b-256 checksum How to use checksums |
c1d7de5b38fc2906cb14d116fa0fd1c6165c50694d57a6582372b53e71dfcf70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_x86_64.whl
| Download URL | knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 822.6 kB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
d4a4de776fd987a7b94a90eea8b7b7da2b87b98f2095c4924a8e80736e0d61d1
|
|
BLAKE2b-256 checksum How to use checksums |
7bef5cdcee703d9c35f1573bcccf0e03d6b69b74086f2afff71ec6b77a9c66bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_aarch64.whl
| Download URL | knn_inspect-0.18.0-cp311-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 813.9 kB |
| Tags | CPython 3.11 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
b8d18b55aac8538f37029e26ebbc7fb9093d74f4a53bae8ea4c3fd1b26ce1e3d
|
|
BLAKE2b-256 checksum How to use checksums |
9869fc7a8aeea6a4fee396c7caa9e084ae0b2b4b0e7db148939e597a04e46820
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / knn_inspect-0.18.0-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | knn_inspect-0.18.0-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 719.1 kB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
1c93d4b106642f409ecd4cd39554f531fd71fb77708f8c56cd9ca5f60e5010dc
|
|
BLAKE2b-256 checksum How to use checksums |
43fd1904e57310aaae46a755318a61ab08e2ea082fc12402fec5aed15f0a106d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for knn-inspect 0.18.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 5 files in this release have been uploaded with provenance metadata.
Attestation for knn_inspect-0.18.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.