AutomationFlowsAI & RAG › Label Incoming Gmail Emails with Gpt-5 and Gmail Labels

Label Incoming Gmail Emails with Gpt-5 and Gmail Labels

ByThomas Abraham @heyabt on n8n.io

This workflow watches for new Gmail messages, uses OpenAI to classify each email into a category, maps that category to a Gmail label ID, and then applies the matching label to the message automatically. Triggers every minute when a new email arrives in Gmail. Sends the email…

Event trigger★★★★☆ complexityAI-powered9 nodesGmail TriggerOpenAI ChatChain LlmGmail
AI & RAG Trigger: Event Nodes: 9 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #17489 — we link there as the canonical source.

This workflow follows the Chainllm → Gmail recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "4Fm1aToIhbW9opSI",
  "name": "Auto-Label Gmail Messages with GPT-5",
  "tags": [],
  "nodes": [
    {
      "id": "18eb3799-22c4-49c1-9672-51e3f939d185",
      "name": "Sticky Note - Main",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 400,
        "height": 680,
        "content": "## Auto-Label Gmail Messages with GPT-5\nEvery new email gets read by AI and automatically labeled - sales, support, spam, personal, or urgent - so your inbox sorts itself.\n\n## How it works\n1. New email triggers the workflow\n2. AI reads the sender, subject, and body and picks one category\n3. The category is mapped to your real Gmail label ID\n4. The label gets applied to the message\n\n## Setup\n- [ ] Connect Gmail OAuth2 credentials (used in both Gmail nodes)\n- [ ] Connect OpenAI credentials in OpenAI GPT-5 Mini Model\n- [ ] Open Set Label Taxonomy and replace each placeholder with your real Gmail label IDs (Gmail Settings > Labels, or via the Gmail API - label names won't work here, only IDs)\n- [ ] Edit the category list in Set Label Taxonomy if you want different categories than Sales/Support/Spam/Personal/Urgent"
      },
      "typeVersion": 1
    },
    {
      "id": "f3d33d49-ed02-4f08-a3f9-04979306770a",
      "name": "Sticky Note - Classify and Apply",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1008,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 750,
        "height": 140,
        "content": "## Classify then Label\nAI picks a category, this maps it to a real label ID, then applies it."
      },
      "typeVersion": 1
    },
    {
      "id": "2d9af757-fd51-4683-8f55-b0b524bb6b60",
      "name": "Sticky Note - Warning",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1264,
        432
      ],
      "parameters": {
        "color": 3,
        "width": 260,
        "height": 132,
        "content": "**Label IDs, not label names.** If Apply Label in Gmail fails, check that the IDs in Set Label Taxonomy are real and still exist."
      },
      "typeVersion": 1
    },
    {
      "id": "c753b883-ec06-4692-9cf8-5195dc4d388f",
      "name": "When Email Received in Gmail",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        512,
        240
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "258d67ae-e0cc-4f41-8325-87ee91cac416",
      "name": "Set Label Taxonomy",
      "type": "n8n-nodes-base.set",
      "position": [
        752,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "asn-1",
              "name": "CategoryList",
              "type": "string",
              "value": "Sales, Support, Spam, Personal, Urgent"
            },
            {
              "id": "asn-2",
              "name": "Sales_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SALES_LABEL_ID"
            },
            {
              "id": "asn-3",
              "name": "Support_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SUPPORT_LABEL_ID"
            },
            {
              "id": "asn-4",
              "name": "Spam_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SPAM_LABEL_ID"
            },
            {
              "id": "asn-5",
              "name": "Personal_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_PERSONAL_LABEL_ID"
            },
            {
              "id": "asn-6",
              "name": "Urgent_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_URGENT_LABEL_ID"
            },
            {
              "id": "asn-7",
              "name": "Default_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_DEFAULT_LABEL_ID"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "15c722d2-2115-4fc0-b8b7-ba968612504c",
      "name": "OpenAI GPT-5 Mini Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1008,
        432
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "1a222393-cec7-4594-8c89-557ced140506",
      "name": "AI Email Classifier",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        1008,
        240
      ],
      "parameters": {
        "text": "=Classify this email into EXACTLY one of these categories: {{ $json.CategoryList }}\n\nFrom: {{ $('When Email Received in Gmail').item.json.from }}\nSubject: {{ $('When Email Received in Gmail').item.json.subject }}\nBody: {{ $('When Email Received in Gmail').item.json.snippet }}\n\nRespond with ONLY the category name, exactly as spelled in the list above. No punctuation, no explanation, nothing else.",
        "promptType": "define"
      },
      "typeVersion": 1.6,
      "continueOnFail": true
    },
    {
      "id": "08ae8108-bfc1-49b4-9f8c-eb8c288f5cd2",
      "name": "Map Category to Gmail Label",
      "type": "n8n-nodes-base.code",
      "position": [
        1296,
        240
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const taxonomy = $('Set Label Taxonomy').item.json;\nconst category = ($input.item.json.text || '').trim();\n\nconst map = {\n  'Sales': taxonomy.Sales_LabelId,\n  'Support': taxonomy.Support_LabelId,\n  'Spam': taxonomy.Spam_LabelId,\n  'Personal': taxonomy.Personal_LabelId,\n  'Urgent': taxonomy.Urgent_LabelId\n};\n\nconst labelId = map[category] || taxonomy.Default_LabelId;\n\nreturn {\n  json: {\n    category: category || 'Unclassified',\n    labelId\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "b74bf4dc-5da5-41ea-9962-7d1f138d5d4e",
      "name": "Apply Label in Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1504,
        240
      ],
      "parameters": {
        "labelIds": "={{ [$json.labelId] }}",
        "messageId": "={{ $('When Email Received in Gmail').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1,
      "continueOnFail": true
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "cfd31970-68b2-48bd-abe6-930d2b48b265",
  "nodeGroups": [],
  "connections": {
    "Set Label Taxonomy": {
      "main": [
        [
          {
            "node": "AI Email Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Email Classifier": {
      "main": [
        [
          {
            "node": "Map Category to Gmail Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI GPT-5 Mini Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Email Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Map Category to Gmail Label": {
      "main": [
        [
          {
            "node": "Apply Label in Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Email Received in Gmail": {
      "main": [
        [
          {
            "node": "Set Label Taxonomy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

This workflow watches for new Gmail messages, uses OpenAI to classify each email into a category, maps that category to a Gmail label ID, and then applies the matching label to the message automatically. Triggers every minute when a new email arrives in Gmail. Sends the email…

Source: https://n8n.io/workflows/17489/ — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

Email-Automation-Template. Uses lmChatAnthropic, microsoftOutlookTrigger, gmail, chainLlm. Event-driven trigger; 38 nodes.

Anthropic Chat, Microsoft Outlook Trigger, Gmail +4
AI & RAG

Use cases Automatically detect and classify sponsorship inquiries from your Gmail inbox Extract company name, contact, budget, campaign type, and channel from emails using AI Log every deal to a Notio

Gmail Trigger, OpenAI Chat, Chain Llm +3
AI & RAG

This workflow automates the full lifecycle of a vehicle insurance claim — from an incoming Gmail email to a signed, watermarked PDF decision letter delivered back to the claimant.

Gmail Trigger, N8N Nodes Pdf Api Hub, Google Sheets +4
AI & RAG

A sophisticated n8n workflow that transforms your email management with AI-powered classification, automatic responses, and intelligent organization.

Output Parser Autofixing, Output Parser Structured, Gmail Trigger +5
AI & RAG

AI Agents Vs AI Workflow. Uses lmChatOpenAi, gmailTrigger, gmail, gmailTool. Event-driven trigger; 30 nodes.

OpenAI Chat, Gmail Trigger, Gmail +7