Combine Haystack's composable pipeline architecture with MultiMail's email infrastructure to build RAG-powered email agents that retrieve, reason, and respond.
Haystack is an end-to-end NLP framework by deepset for building search and RAG pipelines. Its composable architecture lets you snap together retrievers, generators, and custom components into processing pipelines. MultiMail provides the email infrastructure layer that turns Haystack's retrieval and generation capabilities into production email workflows.
By integrating MultiMail with Haystack, you can build pipelines that process inbound emails, retrieve relevant context from document stores, generate informed responses, and send replies through managed mailboxes. MultiMail's gated_send mode ensures that RAG-generated replies are reviewed for factual accuracy before reaching recipients.
Connect Haystack to MultiMail by creating custom pipeline components that call the MultiMail REST API. Haystack's component model makes it natural to add email input/output stages to any pipeline.
Create custom Haystack components for email operations that integrate seamlessly into your pipelines. An EmailFetcher component can pull from MultiMail's inbox, and an EmailSender component can deliver generated responses.
Haystack excels at retrieval-augmented generation. Combine its retriever and generator components with MultiMail to build pipelines that answer emails based on your knowledge base, ensuring responses are grounded in actual data.
RAG pipelines can still produce inaccurate responses. MultiMail's gated_send mode adds a human review step between Haystack's output and actual email delivery, catching errors that retrieval alone cannot prevent.
Build a Haystack pipeline that fetches emails from MultiMail and indexes them into a document store. This makes your email history searchable and retrievable for future context-aware responses.
Haystack's pipeline architecture lets you compose complex email workflows from simple components. Chain classification, routing, retrieval, generation, and sending stages into end-to-end email processing pipelines.
import requests
from haystack import component, Document
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
@component
class EmailFetcher:
@component.output_types(emails=list[Document])
def run(self, mailbox_id: str, limit: int = 10):
resp = requests.get(
f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
headers=HEADERS, params={"limit": limit}
)
emails = resp.json().get("emails", [])
docs = [
Document(
content=f"From: {e[&"cm">#039;from']}\nSubject: {e['subject']}\n\n{e['body']}",
meta={"message_id": e["message_id"], "from": e["from"]}
)
for e in emails
]
return {"emails": docs}
@component
class EmailSender:
@component.output_types(result=dict)
def run(self, to: str, subject: str, body: str, mailbox_id: str):
resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
"mailbox_id": mailbox_id, "to": to,
"subject": subject, "body": body
})
return {"result": resp.json()}Build custom Haystack components that fetch emails from and send emails through MultiMail.
from haystack import Pipeline
from haystack.components.generators import OpenAIGenerator
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack_integrations.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
"cm"># Set up document store with your knowledge base
doc_store = InMemoryDocumentStore()
retriever = InMemoryBM25Retriever(document_store=doc_store)
prompt_template = """
Based on the following context, draft a professional email reply.
Context:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
Original email:
{{ email_content }}
Draft a concise, helpful reply:
"""
pipeline = Pipeline()
pipeline.add_component("retriever", retriever)
pipeline.add_component("prompt", PromptBuilder(template=prompt_template))
pipeline.add_component("generator", OpenAIGenerator(model="gpt-4o"))
pipeline.add_component("sender", EmailSender())
pipeline.connect("retriever.documents", "prompt.documents")
pipeline.connect("prompt", "generator")
"cm"># Run the pipeline (send step done separately for oversight)
result = pipeline.run({
"retriever": {"query": "pricing information"},
"prompt": {"email_content": "What are your current rates?"}
})Create a pipeline that retrieves context from a document store and generates email replies.
from haystack import Pipeline
from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
from haystack.components.writers import DocumentWriter
index_pipeline = Pipeline()
index_pipeline.add_component("fetcher", EmailFetcher())
index_pipeline.add_component("cleaner", DocumentCleaner())
index_pipeline.add_component("splitter", DocumentSplitter(
split_by="sentence", split_length=3
))
index_pipeline.add_component("writer", DocumentWriter(
document_store=doc_store
))
index_pipeline.connect("fetcher.emails", "cleaner")
index_pipeline.connect("cleaner", "splitter")
index_pipeline.connect("splitter", "writer")
"cm"># Index recent emails
index_pipeline.run({"fetcher": {"mailbox_id": "your_mailbox_id", "limit": 50}})Fetch emails from MultiMail and index them into a Haystack document store for future retrieval.
Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.
Install Haystack and requests for calling the MultiMail API.
pip install haystack-ai requestsBuild Haystack @component classes for EmailFetcher and EmailSender that wrap MultiMail API endpoints.
Compose your email processing pipeline by connecting components: fetcher, retriever, prompt builder, generator, and sender.
pipeline = Pipeline()
pipeline.add_component("fetcher", EmailFetcher())
pipeline.add_component("generator", OpenAIGenerator())
pipeline.connect("fetcher", "generator")Execute the pipeline. Review and approve pending emails in the MultiMail dashboard when using gated_send mode.
result = pipeline.run({"fetcher": {"mailbox_id": "your_mailbox_id"}})Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.