Use DSPy's declarative programming model to build email agents that improve automatically through optimization — with MultiMail handling delivery and human oversight.
DSPy is a framework from Stanford for programming with foundation models using signatures and optimizers rather than manual prompt engineering. Instead of writing prompts, you declare what the model should do, and DSPy compiles your declarations into optimized prompts. MultiMail provides the email infrastructure that DSPy modules need to actually send, receive, and manage messages.
By integrating MultiMail with DSPy, you can build email agents where the email composition logic is optimized automatically. Define signatures for tasks like 'compose a professional reply' or 'classify email urgency,' and let DSPy's optimizers find the best prompt strategies. MultiMail handles the delivery with configurable human oversight.
DSPy's assertion system complements MultiMail's oversight modes. Assertions validate email content quality at the generation level, while MultiMail's gated_send mode adds human judgment at the delivery level. Together they ensure both content quality and send authorization.
DSPy's optimizers automatically improve your email drafting prompts based on metrics. Define what a good email reply looks like, provide examples, and let DSPy find the optimal prompting strategy for your use case.
DSPy's assertion system validates email content quality (length, tone, completeness). MultiMail's oversight modes handle delivery safety (correct recipients, appropriate timing). Together they cover both content and delivery concerns.
Define email tasks as DSPy signatures rather than writing complex prompts. A signature like 'email_content -> professional_reply' is clearer and more maintainable than a hand-crafted prompt, and DSPy optimizes it automatically.
Use DSPy's metric system to measure email quality. Track response relevance, tone appropriateness, and accuracy against your knowledge base, then optimize your modules to maximize these metrics.
While DSPy optimizes the intelligence layer, MultiMail provides production-grade email delivery, thread tracking, contact management, and audit logging. Each handles what it does best.
import dspy
import requests
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
"cm"># Define signatures for email tasks
class ClassifyEmail(dspy.Signature):
"""Classify an email by urgency and intent."""
email_subject: str = dspy.InputField()
email_body: str = dspy.InputField()
urgency: str = dspy.OutputField(desc="one of: urgent, routine, low")
intent: str = dspy.OutputField(desc="one of: question, request, update, complaint")
class ComposeReply(dspy.Signature):
"""Compose a professional email reply."""
original_email: str = dspy.InputField()
context: str = dspy.InputField(desc="relevant background information")
reply: str = dspy.OutputField(desc="professional email reply body")
"cm"># Configure DSPy
lm = dspy.LM("openai/gpt-4o")
dspy.configure(lm=lm)Create DSPy signatures for email tasks and modules that call MultiMail for delivery.
class EmailResponder(dspy.Module):
def __init__(self):
self.classify = dspy.Predict(ClassifyEmail)
self.compose = dspy.ChainOfThought(ComposeReply)
def forward(self, email_subject, email_body, context, mailbox_id, message_id):
"cm"># Classify the email
classification = self.classify(
email_subject=email_subject,
email_body=email_body
)
"cm"># Compose a reply
reply = self.compose(
original_email=f"Subject: {email_subject}\n\n{email_body}",
context=context
)
"cm"># Send via MultiMail (gated_send queues for approval)
resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
"message_id": message_id,
"body": reply.reply
})
return dspy.Prediction(
urgency=classification.urgency,
intent=classification.intent,
reply=reply.reply,
send_result=resp.json()
)
"cm"># Use the module
responder = EmailResponder()
result = responder(
email_subject="Pricing inquiry",
email_body="What are your current rates for the Pro plan?",
context="Pro plan is $29/month with 25 mailboxes and 30,000 emails.",
mailbox_id="your_mailbox_id",
message_id="msg_abc123"
)Create a DSPy module that classifies, composes, and sends email replies through MultiMail.
class ValidatedEmailResponder(dspy.Module):
def __init__(self):
self.classify = dspy.Predict(ClassifyEmail)
self.compose = dspy.ChainOfThought(ComposeReply)
def forward(self, email_subject, email_body, context, mailbox_id, message_id):
classification = self.classify(
email_subject=email_subject,
email_body=email_body
)
reply = self.compose(
original_email=f"Subject: {email_subject}\n\n{email_body}",
context=context
)
"cm"># Assertions validate content quality before sending
dspy.Assert(
len(reply.reply) > 50,
"Reply is too short. Provide a substantive response."
)
dspy.Assert(
len(reply.reply) < 2000,
"Reply is too long. Keep it concise and professional."
)
dspy.Suggest(
"thank" in reply.reply.lower() or "appreciate" in reply.reply.lower(),
"Consider acknowledging the sender&"cm">#039;s message."
)
# Send via MultiMail after assertions pass
resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
"message_id": message_id,
"body": reply.reply
})
return dspy.Prediction(
urgency=classification.urgency,
reply=reply.reply,
send_result=resp.json()
)Use DSPy assertions to validate email content before sending through MultiMail.
from dspy.evaluate import Evaluate
"cm"># Define a metric for email quality
def email_quality_metric(example, prediction, trace=None):
reply = prediction.reply
score = 0
"cm"># Check reply addresses the question
if example.intent == "question" and "?" not in reply:
score += 1 "cm"># Answers don't contain questions
"cm"># Check appropriate length
if 100 < len(reply) < 1000:
score += 1
"cm"># Check professional tone
if reply[0].isupper() and reply.rstrip().endswith("."):
score += 1
return score / 3
"cm"># Create training examples
trainset = [
dspy.Example(
email_subject="Pricing question",
email_body="What does the Pro plan cost?",
context="Pro plan: $29/month, 25 mailboxes, 30k emails.",
mailbox_id="test", message_id="test",
intent="question"
).with_inputs("email_subject", "email_body", "context", "mailbox_id", "message_id")
]
"cm"># Optimize the module
optimizer = dspy.MIPROv2(metric=email_quality_metric, auto="light")
optimized_responder = optimizer.compile(
EmailResponder(),
trainset=trainset
)Define email quality metrics and use DSPy's optimizers to improve your email agent automatically.
Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.
Install DSPy and requests for calling the MultiMail API.
pip install dspy requestsCreate DSPy Signature classes that declare your email tasks — classification, composition, and any other email processing steps.
Create a DSPy Module that chains your signatures together and calls the MultiMail API for delivery. Add assertions for content validation.
responder = EmailResponder()
result = responder(email_subject="...", email_body="...", context="...", mailbox_id="...", message_id="...")Define quality metrics and use DSPy's optimizers to improve your email agent. Review pending emails in the MultiMail dashboard when using gated_send mode.
Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.