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:Vibrantlabsai Ragas HaystackEmbeddings

From Leeroopedia
Knowledge Sources
Domains Embeddings, Haystack Integration, LLM Evaluation
Last Updated 2026-02-12 00:00 GMT

Overview

HaystackEmbeddingsWrapper adapts Haystack text embedder components for use within the Ragas evaluation framework, providing both synchronous and asynchronous embedding generation.

Description

The HaystackEmbeddingsWrapper class bridges between Haystack's text embedder ecosystem and Ragas' embedding interface by extending BaseRagasEmbeddings. It accepts any of the four supported Haystack embedder types: AzureOpenAITextEmbedder, HuggingFaceAPITextEmbedder, OpenAITextEmbedder, or SentenceTransformersTextEmbedder.

The wrapper validates the embedder type at initialization and sets up an AsyncPipeline from Haystack to support asynchronous embedding operations. Synchronous embeddings are generated by calling the embedder's run method directly, while asynchronous embeddings route through Haystack's async pipeline infrastructure.

The class uses NumPy vectorized conversion to ensure consistent float type output from synchronous embed operations. For batch document embedding, it iterates over texts individually for the synchronous path, and uses asyncio.gather for concurrent processing in the asynchronous path.

Usage

Use this wrapper when you have an existing Haystack pipeline with text embedder components and want to evaluate your LLM application using Ragas metrics that require embeddings. This avoids duplicating embedder configuration between your Haystack pipeline and Ragas evaluation setup.

Code Reference

Source Location

Signature

class HaystackEmbeddingsWrapper(BaseRagasEmbeddings):
    def __init__(
        self,
        embedder: Union[
            "AzureOpenAITextEmbedder",
            "HuggingFaceAPITextEmbedder",
            "OpenAITextEmbedder",
            "SentenceTransformersTextEmbedder",
        ],
        run_config: Optional[RunConfig] = None,
        cache: Optional[CacheInterface] = None,
    ): ...

Import

from ragas.embeddings.haystack_wrapper import HaystackEmbeddingsWrapper

I/O Contract

Inputs

Name Type Required Description
embedder AzureOpenAITextEmbedder / HuggingFaceAPITextEmbedder / OpenAITextEmbedder / SentenceTransformersTextEmbedder Yes An instance of a supported Haystack text embedder component
run_config RunConfig No Configuration object to manage embedding execution settings such as retry logic; defaults to a new RunConfig instance
cache CacheInterface No A cache instance for storing and retrieving embedding results

Outputs

embed_query / aembed_query

Name Type Description
return List[float] A list of floats representing the embedding vector for a single text

embed_documents / aembed_documents

Name Type Description
return List[List[float]] A list of embedding vectors, one per input text

Usage Examples

Basic Usage with OpenAI Embedder

from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder
from ragas.embeddings.haystack_wrapper import HaystackEmbeddingsWrapper

# Create a Haystack OpenAI text embedder
haystack_embedder = OpenAITextEmbedder(model="text-embedding-3-small")

# Wrap it for use with Ragas
ragas_embeddings = HaystackEmbeddingsWrapper(embedder=haystack_embedder)

# Generate embeddings
embedding = ragas_embeddings.embed_query("What is machine learning?")
print(len(embedding))  # Embedding dimension size

Batch Document Embedding

from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder
from ragas.embeddings.haystack_wrapper import HaystackEmbeddingsWrapper

haystack_embedder = OpenAITextEmbedder(model="text-embedding-3-small")
ragas_embeddings = HaystackEmbeddingsWrapper(embedder=haystack_embedder)

documents = [
    "Machine learning is a subset of AI.",
    "Deep learning uses neural networks.",
    "NLP processes human language.",
]
embeddings = ragas_embeddings.embed_documents(documents)
print(len(embeddings))  # 3

Async Usage

import asyncio
from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder
from ragas.embeddings.haystack_wrapper import HaystackEmbeddingsWrapper

haystack_embedder = OpenAITextEmbedder(model="text-embedding-3-small")
ragas_embeddings = HaystackEmbeddingsWrapper(embedder=haystack_embedder)

async def main():
    embedding = await ragas_embeddings.aembed_query("What is deep learning?")
    embeddings = await ragas_embeddings.aembed_documents(["Text 1", "Text 2"])
    return embedding, embeddings

result = asyncio.run(main())

Related Pages

Page Connections

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