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)

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. The counts are rebuilt from the stored texts on load (about 2 s for 58k documents).
  • 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.0a1

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.0a1
File Size Uploaded Provenance Actions
langchain_qann-0.1.0a1.tar.gz 13.6 kB With provenance Details for langchain_qann-0.1.0a1.tar.gz

Built distribution (wheel)

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

Total release size: 25.3 kB

Release files / langchain_qann-0.1.0a1.tar.gz

Download URL langchain_qann-0.1.0a1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
24526f45048cdc0fcba7130faa7765d4582a68909ee1dc54e5c5e89d6d087e50
BLAKE2b-256 checksum
How to use checksums
ef04012b68a9c52bf66b6d1e77a636aea355d72fab215e58e802968d7450d7cd
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 12, 2026.

Transparency log

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

Download URL langchain_qann-0.1.0a1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26be95322304830d34729b6c80677b7cf26eed13b4c2146ec7737d353939c0f4
BLAKE2b-256 checksum
How to use checksums
46032198419c9b1aaf5f0523bc6ddb0b3e1248da8c26158f06f2f793f1c8abea
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 12, 2026.

Transparency log

Security for langchain-qann 0.1.0a1

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.0a1.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 12, 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