Skip to main content
Pre-release

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)

Durable writes

save() writes the whole index each time. With persist_directory, every add_texts and delete is logged to disk before it returns, and the store reopens from the same directory after a restart or crash:

store = QANNVectorStore(embeddings, persist_directory="store/")   # opens the store there, or creates it on the first add
store.add_texts(texts, metadatas=metadatas)                       # durable when it returns: no save() needed
store = QANNVectorStore.load("store/", embeddings, mmap=True)     # also opens a persist_directory

The index is checkpointed as the log grows, and tuned hybrid weights are checkpointed when tune_hybrid_weights returns. Needs qann 0.1.0a33 or later.

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})

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 is qann.BM25, so the counts are rebuilt from the stored texts on load in 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) and encode_query(text) returning scipy.sparse rows with term ids below 2^31. Pass it again to load(..., sparse=encoder), like the embeddings.
  • Weights. weights=(dense, sparse) sets the fusion, default (1, 1). Set it in search_params or 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. relevant holds 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_score returns 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_documents with 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.0a3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-qann 0.1.0a3
File Size Uploaded Provenance Actions
langchain_qann-0.1.0a3.tar.gz 13.7 kB With provenance Details for langchain_qann-0.1.0a3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-qann 0.1.0a3
File Interpreter ABI Platform Actions
langchain_qann-0.1.0a3-py3-none-any.whl Python 3 none any Details for langchain_qann-0.1.0a3-py3-none-any.whl

Total release size: 25.0 kB

Release files / langchain_qann-0.1.0a3.tar.gz

Download URL langchain_qann-0.1.0a3.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
18428a64ee04a66d210fc0401626bc9c778e957aa92842ddfd6aed490730b650
BLAKE2b-256 checksum
How to use checksums
854ebf6c391ee6fe46210e4c2f8bd769ba9911074b9eb9c527fd9c248e6562e4
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 19, 2026.

Transparency log

Release files / langchain_qann-0.1.0a3-py3-none-any.whl

Download URL langchain_qann-0.1.0a3-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf4bafac8402c004980913c32c512c05efbd3a48a570b623554139f2a81722dd
BLAKE2b-256 checksum
How to use checksums
0c7ca94878148ebab20b8748889fc33f955994040e370c1b6cbc6f161f653ebc
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 19, 2026.

Transparency log

Security for langchain-qann 0.1.0a3

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.

Select from 2 files

Attestation for langchain_qann-0.1.0a3.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 19, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page