Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain SparseEmbeddings

From Leeroopedia
Knowledge Sources
Domains Vector Store, Embeddings, Qdrant, Sparse Embeddings
Last Updated 2026-02-11 00:00 GMT

Overview

Abstract base class and data model defining the interface for sparse embedding models used with Qdrant vector store.

Description

This module in the langchain-qdrant partner package defines two core abstractions: SparseVector, a Pydantic model representing a sparse vector with indices and values fields, and SparseEmbeddings, an abstract base class that specifies the interface for sparse embedding implementations. The SparseEmbeddings ABC requires implementations for embed_documents and embed_query, and provides default async versions (aembed_documents and aembed_query) that delegate to the sync methods via run_in_executor.

Usage

Import SparseEmbeddings when implementing a custom sparse embedding model for use with Qdrant hybrid search. Import SparseVector when you need to construct or type-hint sparse vector data structures.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/qdrant/langchain_qdrant/sparse_embeddings.py
  • Lines: 1-33

Signature

class SparseVector(BaseModel, extra="forbid"):
    """Sparse vector structure."""
    indices: list[int] = Field(..., description="indices must be unique")
    values: list[float] = Field(..., description="values and indices must be the same length")

class SparseEmbeddings(ABC):
    """An interface for sparse embedding models to use with Qdrant."""

    @abstractmethod
    def embed_documents(self, texts: list[str]) -> list[SparseVector]:
        """Embed search docs."""

    @abstractmethod
    def embed_query(self, text: str) -> SparseVector:
        """Embed query text."""

    async def aembed_documents(self, texts: list[str]) -> list[SparseVector]:
        """Asynchronous Embed search docs."""
        ...

    async def aembed_query(self, text: str) -> SparseVector:
        """Asynchronous Embed query text."""
        ...

Import

from langchain_qdrant.sparse_embeddings import SparseEmbeddings, SparseVector

I/O Contract

SparseVector Fields

Name Type Required Description
indices list[int] Yes List of non-zero dimension indices. Must be unique.
values list[float] Yes List of values corresponding to each index. Must be the same length as indices.

embed_documents

Inputs

Name Type Required Description
texts list[str] Yes List of document texts to embed.

Outputs

Name Type Description
return list[SparseVector] List of sparse vectors, one per input document.

embed_query

Inputs

Name Type Required Description
text str Yes Single query text to embed.

Outputs

Name Type Description
return SparseVector A sparse vector representation of the query.

Usage Examples

Implementing a Custom Sparse Embeddings Class

from langchain_qdrant.sparse_embeddings import SparseEmbeddings, SparseVector

class MySparseEmbeddings(SparseEmbeddings):
    def embed_documents(self, texts: list[str]) -> list[SparseVector]:
        # Custom sparse embedding logic
        return [
            SparseVector(indices=[0, 1, 2], values=[0.1, 0.5, 0.3])
            for _ in texts
        ]

    def embed_query(self, text: str) -> SparseVector:
        # Custom sparse query embedding logic
        return SparseVector(indices=[0, 1], values=[0.4, 0.6])

Using SparseVector Directly

from langchain_qdrant.sparse_embeddings import SparseVector

vec = SparseVector(indices=[10, 42, 99], values=[0.8, 0.3, 0.5])
print(vec.indices)  # [10, 42, 99]
print(vec.values)   # [0.8, 0.3, 0.5]

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment