[JS] @langchain/qdrant: add a hybrid retrieval mode (parity with langchain-qdrant for Python)

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

  • ensureCollection creates the dense vector plus a sparse vector with modifier: "idf" when the mode is hybrid.
  • addVectors also writes the sparse vector, built from document.pageContent. Its signature already receives the documents, so it does not change.
  • similaritySearch and similaritySearchWithScore are overridden. In hybrid mode they send a single query with two prefetches, one dense and one sparse, each with limit k and with the filter applied to both, plus the fusion. This is the same shape as the Python implementation. In dense mode the request is the same as today.
  • similaritySearchVectorWithScore and maxMarginalRelevanceSearch always 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, similaritySearchWithScore returns a fusion score, not a similarity. Thresholds become meaningless, for example in ScoreThresholdRetriever, which filters on score >= minSimilarityScore.
  • VectorStore requires an embeddings instance. A sparse-only mode, which Python supports with embedding=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

  1. 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 dense similaritySearch. I’d prefer the mode, because asRetriever() and existing chains get hybrid search for free. Would you rather follow the Weaviate precedent?
  2. Sparse-only mode: in the first PR or later?
  3. 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)?
  4. Version requirement: should hybrid mode just document Qdrant ≥ 1.15.3, or check the server version, for example in ensureCollection?

Test cases

Unit tests, with a mocked client:

  • dense mode sends exactly today’s requests;
  • hybrid mode sends two prefetches with the right using, the filter on both, and the default and custom fusion;
  • ensureCollection creates the sparse vector with modifier: "idf";
  • addVectors writes both vectors.

Integration tests, against a real Qdrant ≥ 1.15.3:

  • in hybrid mode, a chunk that only BM25 can find (via stemming) is returned, and in dense mode it is not;
  • the filter is respected in hybrid mode;
  • asRetriever() returns the hybrid results;
  • an existing unnamed-vector collection still works in dense mode.

The PR would touch only libs/providers/langchain-qdrant, plus the docs page and a changeset (@langchain/qdrant: minor).

@valeriocomo Welcome to the LangChain community!

Really appreciate the effort you’ve put into detailing your request. However, I’d recommend opening an issue in the official repository so the maintainers can review it and track it more effectively:

Thanks @keenborder786 for the pointer! I’ve opened the issue here:

The design discussion (a retrieval mode on the store vs. a separate hybridSearch() method, plus the other open questions) will continue there.

Thank you @valeriocomo will appreciate if you can close this thread by marking the answer as solution.

Great proposal. Bringing @langchain/qdrant to parity with Python’s langchain-qdrant (retrieval_mode: "dense" | "sparse" | "hybrid") is much cleaner now that @qdrant/js-client-rest (>= 1.10.0) exposes the unified client.query() API with native server-side Reciprocal Rank Fusion (fusion: "rrf") and Distribution-Based Score Fusion (fusion: "dbsf").

Recommended Architecture for the @langchain/qdrant PR

To keep full backward compatibility with existing single-vector JS collections while supporting hybrid search:

  1. Constructor & QdrantLibArgs Extension:
    • Add retrievalMode?: "dense" | "sparse" | "hybrid" (defaulting to "dense").
    • Add sparseEmbeddings?: SparseEmbeddingsInterface (matching (texts: string[]) => Promise<{ indices: number[]; values: number[] }[]>), plus vectorName?: string and sparseVectorName?: string (defaulting to "langchain-sparse" to match Python’s SPARSE_VECTOR_NAME).
  2. Single-Roundtrip Hybrid Query via client.query():
    Instead of issuing separate dense and sparse requests and fusing on the client, use Qdrant’s prefetch + fusion pipeline in similaritySearchVectorWithScore:
const queryResponse = await this.client.query(this.collectionName, {
  prefetch: [
    {
      query: denseEmbedding,
      using: this.vectorName,
      limit: k * 2,
      filter,
    },
    {
      query: {
        indices: sparseEmbedding.indices,
        values: sparseEmbedding.values,
      },
      using: this.sparseVectorName,
      limit: k * 2,
      filter,
    },
  ],
  query: { fusion: this.fusionType ?? "rrf" },
  limit: k,
  with_payload: true,
  with_vector: false,
});
  1. Collection Initialization (ensureCollection): When retrievalMode === "hybrid" or "sparse", pass sparse_vectors: { [this.sparseVectorName]: {} } alongside vectors in createCollection. That makes existing Python langchain-qdrant hybrid collections 100% interoperable with @langchain/qdrant in TypeScript.

thanks for your feedback!
I’ll add these details to my issue

Checking the output right after that step on [JS] @langchain/qdrant: add a hybrid retrieval m confirms whether the parameter bound cleanly. Hope that resolves the blocker.