Skip to content

Email Triage & Grounded Auto-Reply Workflow

A pipeline that reads every email landing in a connected Gmail account, classifies it, and either acts on it or drafts a reply grounded in internal documentation. It was deployed in the cloud and tested end to end with live inbound email.

Problem

Spam, invoices, general messages and questions about internal documentation all land in the same inbox, and each needs different handling. The system had to ingest real-time email events over a webhook, classify them reliably, and either act immediately or answer from a knowledge base, without a human reading each message first.

Approach

Nylas connects to Gmail and turns new messages into signed webhooks, received by a FastAPI endpoint that verifies the signature and queues the event on Celery/Redis. A workflow with deterministic routing then classifies the email with a PydanticAI agent (gpt-4o-mini) and sends it to the right handler: delete (spam), download the attachment (invoice), send an acknowledgement (general), or answer from a pgvector knowledge base via a second agent (gpt-5.1) that grounds its reply in retrieved documents and flags low-confidence answers for manual follow-up.

Responses are evaluated and traced with Langfuse.

Results

  • All test emails classified and routed correctly across the four categories
  • < 3 s from webhook to action on test emails
  • End-to-end flow verified against captured webhook events for each category

Solution Overview

Gmail 
    → Nylas (webhooks) → FastAPI (/events) → Postgres + Celery/Redis
    → EmailWorkflow: Filter → Classify (gpt-4o-mini) → Route
        → Spam (delete) / Invoice (download) / General (reply)
        → RAG with internal docs (gpt-5.1 + pgvector)
    → back through Nylas → Gmail

Tech Stack

  • Nylas API (email/calendar, OAuth, webhooks)
  • Python, FastAPI, Celery, Redis
  • PostgreSQL + pgvector (Supabase)
  • PydanticAI, OpenAI (gpt-4o-mini, gpt-5.1, text-embedding-3-small)
  • GenAI Launchpad framework (Datalumina)
  • Docker, Caddy, Hetzner Cloud
  • Sentry, Langfuse
  • Let's have a virtual coffee together!


    Want to see if we're a match? Let's have a chat and find out. Schedule a free 30-minute session to discuss your data challenges and explore how we can work together.

    Book Free Intro Call