Skip to content
Pydantic AI

Asistente de email con Pydantic AI

Construye un asistente de email con IA que busca en tu bandeja, redacta respuestas y gestiona flujos de email — con Pydantic AI.

emailautomationproductivityassistant

Código funcional

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)

Paso a paso

1

Instalar dependencias

Instala Pydantic AI y las herramientas necesarias para este caso de uso.

2

Definir herramientas

Crea las funciones de herramientas específicas del dominio que tu agente usará para interactuar con servicios externos.

3

Crear el agente y ejecutar

Inicializa el agente de Pydantic AI con tus herramientas, establece el prompt del sistema y ejecuta una consulta.

¿Listo para construir con Pydantic AI?

Genera un proyecto listo para producción con Pydantic AI preconfigurado — FastAPI + Next.js, auth, streaming y más.

Comenzar

¿Listo para construir tu primer agente IA en producción?

Herramientas open-source, patrones probados en batalla, cero boilerplate. Configura tu stack y despliega en minutos — no meses.