Skip to content
Pydantic AI

Email Assistant with Pydantic AI

Build an AI email assistant that searches your inbox, drafts replies, and manages email workflows — using Pydantic AI.

emailautomationproductivityassistant

Working Code

Pydantic AI
from pydantic_ai import Agent, RunContext
agent = Agent(
"openai:gpt-4o",
system_prompt="You are an email assistant. Search emails to find context, then help draft professional replies. Always create drafts — never send directly.",
)
@agent.tool
async def search_emails(ctx: RunContext, query: str, limit: int = 5) -> str:
"""Search the inbox for emails matching a query."""
results = await email_client.search(query, max_results=limit)
return "\n\n".join(
f"From: {e.sender}\nSubject: {e.subject}\nDate: {e.date}\nPreview: {e.body[:200]}"
for e in results
)
@agent.tool
async def draft_email(ctx: RunContext, to: str, subject: str, body: str) -> str:
"""Create an email draft."""
draft_id = await email_client.create_draft(to=to, subject=subject, body=body)
return f"Draft created (ID: {draft_id}). Review before sending."
result = await agent.run("Find the latest email from the marketing team and draft a reply confirming the deadline")
print(result.output)

Step by Step

1

Install dependencies

Install Pydantic AI and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the Pydantic AI agent with your tools, set the system prompt, and execute a query.

Ready to build with Pydantic AI?

Generate a production-ready project with Pydantic AI pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.