n8n Workflow
Expert

AI-Powered Gmail Labeling with OpenAI

Automate the categorization of your Gmail inbox. This workflow uses OpenAI to analyze incoming emails and automatically applies predefined labels like 'Inquiry' or 'Partnership'.

Overview

Automatically analyzes new Gmail messages with OpenAI and applies the correct labels, saving you time and keeping your inbox perfectly organized.

🧠 Description & Use Case

This workflow automates the process of organizing your Gmail inbox by using AI to analyze the content of incoming emails and apply the appropriate labels. It eliminates the need for manual sorting or complex filter rules, ensuring your emails are categorized efficiently.

🔄 How It Works:

  1. Trigger on New Email

    • The Gmail trigger node initiates the workflow whenever a new email is received in your inbox.
  2. Fetch Email Content

    • The Get message content node retrieves the full text of the new email using its message ID.
  3. AI-Powered Label Assignment

    • The email content is passed to the Assign labels for message (LLM Chain) node.
    • This chain leverages an OpenAI Chat model to analyze the text.
    • A JSON Parser ensures the AI's output is a structured list of predefined labels (e.g., ["Inquiry", "Partnership"]).
  4. Prepare and Match Labels

    • The Split out assigned labels node processes the list of labels suggested by the AI.
    • In parallel, the Get all labels node fetches a complete list of available labels from your Gmail account.
    • The Merge corresponding labels node matches the AI-suggested label names with the official labels from your account to find their corresponding IDs.
  5. Apply Labels to the Email

    • The Aggregate label IDs node gathers the matched label IDs into a single array.
    • Finally, the Add labels to message node applies these labels to the original email, completing the organization process.

✅ Real-World Use Cases:

  • Sales Lead Triage: Automatically label incoming emails as "Hot Lead," "Inquiry," or "Follow-Up" to help sales teams prioritize their efforts.
  • Customer Support Ticketing: Categorize support requests by topic (e.g., "Billing Issue," "Technical Bug," "Feature Request") for faster routing to the correct department.
  • Inbox Zero Strategy: Keep your primary inbox clean by automatically sorting newsletters, notifications, and promotional emails into their designated folders.
  • Project Management: Label all emails related to specific projects (e.g., "Project Alpha," "Q4 Campaign") to easily track communications.
  • Personal Finance: Automatically tag receipts, invoices, and bank statements for effortless financial tracking.

Instructions

Basic steps

  1. Download workflow JSON
  2. Import into n8n
  3. Configure node parameters as needed
  4. Test and enable the workflow

💡 Tips

After importing, verify all connectors and credentials match your environment.

Preview

Workflow Visualization

19 nodes
100%
Sticky NotestickyNoteSticky Note1stickyNoteSticky Note2stickyNoteSticky Note4stickyNoteSticky Note5stickyNoteSticky Note6stickyNoteSticky Note7stickyNoteSticky Note8stickyNoteGmail triggergmailTriggerGet message contentgmailAssign labels for message@n8n/langchain.chainLlmOpenAI Chat@n8n/langchain.lmChatOpenAiJSON Parser@n8n/langchain.outputParserStructuredSet label valuessetGet all labelsgmailSplit out assigned labelssplitOutMerge corresponding labelsmergeAggregate label IDsaggregateAdd labels to messagegmail

Nodes

1

Sticky Note

stickyNote

v1
2

Sticky Note1

stickyNote

v1
3

Sticky Note2

stickyNote

v1
4

Sticky Note4

stickyNote

v1
5

Sticky Note5

stickyNote

v1
6

Sticky Note6

stickyNote

v1
7

Sticky Note7

stickyNote

v1
8

Sticky Note8

stickyNote

v1
9

Gmail trigger

gmailTrigger

v1.2
10

Get message content

gmail

v2.1
11

Assign labels for message

@n8n/langchain.chainLlm

v1.5
12

OpenAI Chat

@n8n/langchain.lmChatOpenAi

v1.2
13

JSON Parser

@n8n/langchain.outputParserStructured

v1.2
14

Set label values

set

v3.4
15

Get all labels

gmail

v2.1
16

Split out assigned labels

splitOut

v1
17

Merge corresponding labels

merge

v3.1
18

Aggregate label IDs

aggregate

v1
19

Add labels to message

gmail

v2.1

Statistics

Total nodes:19
Disabled nodes:0
Node types:10
Connections:10
View full JSON structure
{
  "nodes": [
    {
      "id": "814e3849-1ae1-4124-bdfc-b72017e9d7c2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 420.4803040774015,
        "height": 240.5794370832273,
        "content": "## Add AI labels to Gmail messages\nWith this workflow you can automatically set labels for your Gmail message according to its content. \n\nIn this workflow available are 3 labels: \"Partnership\", \"Inquiry\" and \"Notification\". Feel free to adjust labels according to your needs. \n\n**Please remember to set label names both in your Gmail account and workflow.**"
      },
      "typeVersion": 1
    },
    {
      "id": "e83fa311-b5ba-427e-a98e-573394b882dd",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        260
      ],
      "parameters": {
        "width": 421.0932411886662,
        "height": 257.429163787146,
        "content": "## ⚠️ Note\n\n1. Complete video guide for this workflow is available [on my YouTube](https://youtu.be/a8Dhj3Zh9vQ). \n2. Remember to add your credentials and configure nodes (covered in the video guide).\n3. If you like this workflow, please subscribe to [my YouTube channel](https://www.youtube.com/@workfloows) and/or [my newsletter](https://workfloows.com/).\n\n**Thank you for your support!**"
      },
      "typeVersion": 1
    },
    {
      "id": "4c20d029-750f-476b-9348-6e250ea64d52",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        0
      ],
      "parameters": {
        "width": 238.4602598584674,
        "height": 348.5873725349161,
        "content": "### Gmail Trigger\nReceive data from Gmail about new incoming message. \n\n⚠️ Set polling interval according to your needs."
      },
      "typeVersion": 1
    },
    {
      "id": "22923079-80ce-4495-b0f0-da7122195c56",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1160,
        380
      ],
      "parameters": {
        "width": 241.5397401415323,
        "height": 319.3323098457962,
        "content": "###\n\n\n\n\n\n\n\n\n\n\n### JSON schema\nEdit JSON schema and label names according to your needs.\n\n⚠️ **Label names in system prompt and JSON schema should be the same.**"
      },
      "typeVersion": 1
    },
    {
      "id": "40735a58-daaa-43ac-9658-706c3cf0cbba",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1900,
        20
      ],
      "parameters": {
        "width": 226.1423387262064,
        "height": 347.0476323933831,
        "content": "### Merge labels\nCombine labels retrieved from Gmail account and assigned by AI together."
      },
      "typeVersion": 1
    },
    {
      "id": "87e0f9e2-a2ff-46cf-896a-138b1bde2d0e",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2160,
        20
      ],
      "parameters": {
        "width": 452.4841395315019,
        "height": 347.0476323933831,
        "content": "### Aggregarte labels and add to message\nCreate array of label IDs and add to the desired email message in Gmail."
      },
      "typeVersion": 1
    },
    {
      "id": "1d533664-e5e8-4dc8-afac-bfc5996e4bf9",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        700,
        0
      ],
      "parameters": {
        "width": 238.4602598584674,
        "height": 348.5873725349161,
        "content": "### Get message content\nBased on Gmail message ID retrieve body content of the email and pass it to AI chain."
      },
      "typeVersion": 1
    },
    {
      "id": "e613ca64-50ae-4d7c-b0fc-15812dadcd68",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        960,
        0
      ],
      "parameters": {
        "width": 378.5766127379356,
        "height": 348.5873725349161,
        "content": "### Assign labels\nLet the AI decide which labels suit the best content of the message.\n\n⚠️ **Remember to edit system prompt** - modify label names and instructions according to your needs."
      },
      "typeVersion": 1
    },
    {
      "id": "a2005e70-6774-45ce-b9c6-742786f49964",
      "name": "Gmail trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        500,
        180
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "SPECSn66s6QHmld9",
          "name": "Gmail account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4b6f7d21-6155-42b7-93ff-2f530df3692f",
      "name": "Get message content",
      "type": "n8n-nodes-base.gmail",
      "position": [
        760,
        180
      ],
      "webhookId": "b773894c-18c6-454d-9271-6de10be1b7c4",
      "parameters": {
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "SPECSn66s6QHmld9",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "ad577660-d9f4-4031-ad16-7021a02bb18e",
      "name": "Assign labels for message",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        1020,
        180
      ],
      "parameters": {
        "text": "={{ $('Gmail trigger').item.json.text }}",
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.5
    },
    {
      "id": "1e11d30f-4c73-4fd0-a365-aeb43bee4252",
      "name": "OpenAI Chat",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1000,
        400
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "PrK67ozsBFqSIYG9",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4a504b64-fb28-44fb-a80a-6f5e5c5a1949",
      "name": "JSON Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1240,
        400
      ],
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"labels\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"string\",\n        \"enum\": [\"Inquiry\", \"Partnership\", \"Notification\"]\n      }\n    }\n  },\n  \"required\": [\"labels\"]\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "f5ac1b01-0980-4ee4-b4f5-5057258eab70",
      "name": "Set label values",
      "type": "n8n-nodes-base.set",
      "position": [
        1400,
        180
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "72d11a72-6693-447c-b7ca-4ba1a3579075",
              "name": "labels",
              "type": "array",
              "value": "={{ $json.output.labels }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e368e343-728e-4e2f-a37f-5e203000d090",
      "name": "Get all labels",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1680,
        60
      ],
      "webhookId": "dec6f574-f47c-4b5d-86b9-2b0f6c957145",
      "parameters": {
        "resource": "label",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "SPECSn66s6QHmld9",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "48ce8351-5d04-4697-b68d-bb84286e0b2b",
      "name": "Split out assigned labels",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1680,
        280
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "labels"
      },
      "typeVersion": 1
    },
    {
      "id": "cc1aa3ac-7427-4761-aacd-caf16c64d7fb",
      "name": "Merge corresponding labels",
      "type": "n8n-nodes-base.merge",
      "position": [
        1960,
        180
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "advanced": true,
        "mergeByFields": {
          "values": [
            {
              "field1": "name",
              "field2": "labels"
            }
          ]
        }
      },
      "typeVersion": 3.1
    },
    {
      "id": "97fefda6-5936-42a7-a30a-8de4149aa445",
      "name": "Aggregate label IDs",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        2220,
        180
      ],
      "parameters": {
        "options": {},
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "id"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7befd422-7243-43af-9b34-21c05a069013",
      "name": "Add labels to message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2440,
        180
      ],
      "webhookId": "4f345fc9-2afd-478b-be3b-d3d28f0fbc82",
      "parameters": {
        "labelIds": "={{ $json.id }}",
        "messageId": "={{ $('Gmail trigger').item.json[\"id\"] }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "SPECSn66s6QHmld9",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "connections": {
    "JSON Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Assign labels for message",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat": {
      "ai_languageModel": [
        [
          {
            "node": "Assign labels for message",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Gmail trigger": {
      "main": [
        [
          {
            "node": "Get message content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get all labels": {
      "main": [
        [
          {
            "node": "Merge corresponding labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set label values": {
      "main": [
        [
          {
            "node": "Get all labels",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split out assigned labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate label IDs": {
      "main": [
        [
          {
            "node": "Add labels to message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get message content": {
      "main": [
        [
          {
            "node": "Assign labels for message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign labels for message": {
      "main": [
        [
          {
            "node": "Set label values",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split out assigned labels": {
      "main": [
        [
          {
            "node": "Merge corresponding labels",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge corresponding labels": {
      "main": [
        [
          {
            "node": "Aggregate label IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Actions

Share
Categories:
Tags:
setgmailmergestickynotegmailtriggersplitoutaggregate

Technical Specs

Complexity:
Expert
Node Count:
19 nodes
Published:11 months ago
Updated:5 days ago

Compatibility

Platform:
N8N Cloud & Self-hosted
Min Version:
N8N v1.0.0+
Supports cloud and self-hosted deployment

License

This workflow template follows MIT license, you can freely use, modify and distribute.

Please comply with relevant third-party service terms when using.

Related Workflows