Summary
Since July 2024 the Python QdrantVectorStore has a retrieval_mode option, with dense, sparse and hybrid modes (langchain#24164). @langchain/qdrant for JS (1.0.3) still writes and queries a single unnamed dense vector. I’d like to add a hybrid mode to the JS package, and I’m happy to open the PR.
Use case
Hybrid search helps when queries hinge on exact terms that dense retrieval alone handles poorly: identifiers, section numbers, acronyms, product codes, proper names. Python users of the package already have it. In JS the only way to get it today is to bypass the vector store: you call QdrantClient directly for both indexing and search, and wrap it in a custom BaseRetriever. That works, but it duplicates the vector store, and asRetriever() and existing chains don’t benefit.
Why this is easier now than it used to be
The Python hybrid mode computes sparse vectors on the client with FastEmbed (FastEmbedSparse), which has no JS equivalent. Since 1.15.3, a self-hosted Qdrant can do it on the server instead:
- the point carries a
Document({ text, model: "qdrant/bm25", options: { language } }), and Qdrant turns it into a sparse BM25 vector, with language-specific stemming and stop words; modifier: "idf"on the sparse vector makes Qdrant compute IDF over the whole collection.
So the feature needs no new dependency. A prototype works on Qdrant 1.15.3 and 1.19.1 with @qdrant/js-client-rest 1.18. The language option does matter. With language: "italian", a query with “contratti” matches a chunk that only contains “contratto”. With the default (English), it finds nothing.
Proposed API
const store = await QdrantVectorStore.fromExistingCollection(embeddings, {
url,
collectionName: "docs",
retrievalMode: "hybrid", // "dense" (default) | "hybrid"
sparseVectorName: "langchain-sparse", // default, same as Python
sparseDocumentOptions: { // passed as-is to the Qdrant Document
model: "qdrant/bm25",
options: { language: "english" },
},
});
await store.similaritySearch("how long can a fixed-term contract last?", 5); // hybrid
store.asRetriever({ k: 5 }); // hybrid as well
| Option | Default | Notes |
|---|---|---|
retrievalMode |
"dense" |
the default keeps today’s behaviour |
vectorName |
"" (unnamed) |
same as Python; today’s collections keep working |
sparseVectorName |
"langchain-sparse" |
same as Python |
sparseDocumentOptions |
{ model: "qdrant/bm25" } |
model and BM25 options (language, k, b, avg_len, …) |
hybridFusion |
{ fusion: "rrf" } |
same default as Python; any Qdrant fusion query (dbsf, or weighted rrf on Qdrant ≥ 1.17) |
Behaviour
ensureCollectioncreates the dense vector plus a sparse vector withmodifier: "idf"when the mode ishybrid.addVectorsalso writes the sparse vector, built fromdocument.pageContent. Its signature already receives the documents, so it does not change.similaritySearchandsimilaritySearchWithScoreare overridden. Inhybridmode they send a singlequerywith two prefetches, one dense and one sparse, each with limitkand with the filter applied to both, plus the fusion. This is the same shape as the Python implementation. Indensemode the request is the same as today.similaritySearchVectorWithScoreandmaxMarginalRelevanceSearchalways search the dense vector, as Python’s by-vector search and MMR do.
Impact on the VectorStore interface
None in @langchain/core: the change is limited to overrides in @langchain/qdrant. That is also why it can’t be done without overrides. The base similaritySearch and similaritySearchWithScore embed the query and pass only the vector to similaritySearchVectorWithScore, so the query text never reaches the sparse side.
Two semantic caveats, to be documented:
- In hybrid mode,
similaritySearchWithScorereturns a fusion score, not a similarity. Thresholds become meaningless, for example inScoreThresholdRetriever, which filters onscore >= minSimilarityScore. VectorStorerequires anembeddingsinstance. A sparse-only mode, which Python supports withembedding=None, would not fit as cleanly, so I’d leave it out of the first PR.
Minimum Qdrant versions
I checked these against the release notes and ran them on the official Docker images:
| Feature | Minimum version | Notes |
|---|---|---|
Query API (prefetch + fusion) |
1.10 | needed by the Python implementation too |
| Server-side BM25 on a self-hosted instance | 1.15.3 | Local BM25 first shipped in 1.15.2 (qdrant#6891). There, however, the inference service starts only when a cloud inference address is configured, so a default instance answers InferenceService is not initialized (also on 1.15.1). 1.15.3 (qdrant#7056) makes that configuration optional, enables stemming and stop words by default, and adds language. |
Weighted RRF (rrf: { weights }) |
1.17.0 | qdrant#8063; 1.15.3 rejects it. Plain RRF is enough for the default. |
So hybrid mode would require Qdrant ≥ 1.15.3, while dense mode keeps working on any version.
Open questions
- A mode on the store, or a separate method? The mode follows Python; a separate method follows the Weaviate JS store, which has
hybridSearch(query, options)next to a densesimilaritySearch. I’d prefer the mode, becauseasRetriever()and existing chains get hybrid search for free. Would you rather follow the Weaviate precedent? - Sparse-only mode: in the first PR or later?
- Precomputed sparse vectors: is server-side BM25 enough, or should there also be a way to pass sparse vectors computed elsewhere (like Python’s
SparseEmbeddings)? - Version requirement: should
hybridmode just document Qdrant ≥ 1.15.3, or check the server version, for example inensureCollection?
Test cases
Unit tests, with a mocked client:
densemode sends exactly today’s requests;hybridmode sends two prefetches with the rightusing, the filter on both, and the default and custom fusion;ensureCollectioncreates the sparse vector withmodifier: "idf";addVectorswrites both vectors.
Integration tests, against a real Qdrant ≥ 1.15.3:
- in
hybridmode, a chunk that only BM25 can find (via stemming) is returned, and indensemode it is not; - the filter is respected in
hybridmode; asRetriever()returns the hybrid results;- an existing unnamed-vector collection still works in
densemode.
The PR would touch only libs/providers/langchain-qdrant, plus the docs page and a changeset (@langchain/qdrant: minor).