This release is a pre-release and may not be stable for production use.
langchain-qann
A LangChain vector store backed by qann: HNSW, IVF with fast-scan PQ and re-ranking, or exact search, with metadata filters and hybrid (dense + BM25 or SPLADE) search. Texts, ids and metadata are stored in the qann index, so a store saves to one file.
This is a community integration maintained with qann, not by LangChain.
pip install langchain-qann
Quick start
from langchain_qann import QANNVectorStore
store = QANNVectorStore.from_texts(texts, embeddings, metadatas=metadatas)
docs = store.similarity_search("how do bonds work?", k=10)
retriever = store.as_retriever(search_kwargs={"k": 10})
store.save("store.qann")
store = QANNVectorStore.load("store.qann", embeddings, mmap=True)
Choosing the index
Three levels, from simplest to full control.
1. Presets. Pass index= by name; parameters follow qann's configuration guide.
index= |
What it builds | When |
|---|---|---|
"hnsw" (default) |
HNSWIndex(M=16, ef_construction=200) |
adds at any time; low latency |
"ivf-pq" |
IVF, fast-scan PQ (dim / 2 subspaces), RefineIndex(k_factor=4), nlist = max(100, √n) |
least RAM with load(..., mmap=True); trains on the first batch (at least 1,000 texts), so build it with from_texts on the whole corpus |
"flat" |
FlatIndex |
exact; small corpora |
All presets use cosine distance; metric="l2" or "ip" changes that.
2. Any qann index. Build it with the parameters you want and pass it in:
import qann
base = qann.IVFIndex(384, nlist=2048, metric=qann.Metric.Cosine)
base.enable_pq(192, fast_scan=True, rotate=True)
base.train(sample_vectors)
store = QANNVectorStore(embeddings, index=qann.RefineIndex(base, k_factor=8))
store = QANNVectorStore(embeddings, index=qann.HNSWIndex(384, M=32, ef_construction=400, metric=qann.Metric.Cosine))
store.index is the underlying qann index, for any other setting.
3. Search settings. nprobe, ef_search and k_factor are set on the store for every query, or per query:
store.search_params = {"nprobe": 32}
docs = store.similarity_search("query", k=10, nprobe=64)
retriever = store.as_retriever(search_kwargs={"k": 10, "ef_search": 128})
Hybrid search
sparse= adds a sparse side, searched together with the dense one and fused (qann's HybridIndex):
store = QANNVectorStore.from_texts(texts, embeddings, sparse="bm25")
docs = store.similarity_search("form 10-K filing deadline", k=10)
sparse="bm25"scores the texts with BM25: lowercase words and numbers, English stopwords removed, no stemming. Each query term's IDF comes from the current document counts, so adds and deletes update it without re-encoding stored documents. It isqann.BM25, so the counts are rebuilt from the stored texts onloadin C++ (0.15 s for 58k documents, about 5 s for 1M).- A custom encoder, e.g. SPLADE, is any object with
encode_documents(texts)andencode_query(text)returningscipy.sparserows with term ids below 2^31. Pass it again toload(..., sparse=encoder), like the embeddings. - Weights.
weights=(dense, sparse)sets the fusion, default(1, 1). Set it insearch_paramsor per query:weights=(1, 0)is dense only and(0, 1)sparse only.method="rrf"switches from linear fusion to reciprocal rank fusion. - Tuning.
store.tune_hybrid_weights(queries, relevant)picks the weights from labeled queries and keeps them, and they're saved with the store.relevantholds the relevant document ids per query, as a set or{id: grade}. Tune on queries you won't evaluate on. - Text vs vector. Searches by text use both sides; searches by vector (
similarity_search_by_vector) have no text for the sparse side, so they use the dense side. - Scores.
similarity_search_with_scorereturns the fused score (higher is better) instead of a distance.
On BEIR FiQA (58k passages, bge-small, index="ivf-pq"), nDCG@10:
| nDCG@10 | |
|---|---|
| dense only | 0.384 |
| BM25 only | 0.238 |
| hybrid, default (1, 1) | 0.373 |
| hybrid, tuned on half the queries (1, 0.5), scored on the other half | 0.389 (default: 0.368) |
Where BM25 is much weaker than the embeddings, as here, equal weights hurt and tuning matters; on SciFact, where BM25 is strong, the default already beats dense alone.
Filters
filter= takes any of:
store.similarity_search("q", filter={"source": "10-K", "year": [2023, 2024]}) # exact matches; a list means any of
store.similarity_search("q", filter="year >= 2020 AND lang = 'en'") # SQL WHERE
store.similarity_search("q", filter=("year >= ? AND lang = ?", [2020, "en"])) # with parameters
store.similarity_search("q", filter=qann.field("year") >= 2020) # qann.Expr
Metadata strings and numbers become filterable columns (numbers as floats, booleans as 0 / 1). Other values, such as lists and dicts, are returned with the document but can't be filtered on. A key whose values change type (a string, then a number) keeps the first type for filtering.
Also supported
add_texts/add_documentswith ids; adding an existing id replaces that document.add_embeddings(texts, vectors, metadatas, ids=...)for precomputed embeddings.get_by_ids,delete(ids)(delete()removes everything).similarity_search_with_score(qann distance, lower is closer; the fused score with hybrid search),similarity_search_with_relevance_scores, search by vector.max_marginal_relevance_search.- Async methods (LangChain's defaults, run in a thread; qann releases the GIL during queries and adds).
Metadata
Release files for langchain-qann 0.1.0a2
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 |
|---|---|---|---|---|
| langchain_qann-0.1.0a2.tar.gz | 12.8 kB | With provenance | Details for langchain_qann-0.1.0a2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_qann-0.1.0a2-py3-none-any.whl | Python 3 | none | any | Details for langchain_qann-0.1.0a2-py3-none-any.whl |
Total release size: 23.4 kB
Release files / langchain_qann-0.1.0a2.tar.gz
| Download URL | langchain_qann-0.1.0a2.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f29513b1ae55cba0953af14b20c8a7949744f3fc5b7ab902cbeee1fb4f1d1dc
|
|
BLAKE2b-256 checksum How to use checksums |
383c3f55c3ae6518d1fd1ce41c46c7a70a934ab18e5c47f12fdf37dd38d3a004
|
| 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 13, 2026.
Transparency logRelease files / langchain_qann-0.1.0a2-py3-none-any.whl
| Download URL | langchain_qann-0.1.0a2-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7e455e6870705f8670e76252129a606d46d863f52b3aba11e0622711e08a11f7
|
|
BLAKE2b-256 checksum How to use checksums |
fe65e8ec271c485f266a612aa555db0e80fac4b62d51ce2ff836abd1e9ca40c6
|
| 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 13, 2026.
Transparency logSecurity for langchain-qann 0.1.0a2
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
Both files in this release have been uploaded with provenance metadata.
Attestation for langchain_qann-0.1.0a2.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 13, 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.