{
  "id": "ggkJA0uoozwG7xjI",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Auto-label incoming Gmail emails using AI",
  "tags": [
    {
      "id": "Ii0eAkipojnzTClF",
      "name": "arlindeveloper",
      "createdAt": "2025-06-27T00:20:16.715Z",
      "updatedAt": "2025-06-27T00:20:16.715Z"
    }
  ],
  "nodes": [
    {
      "id": "43213c04-af84-46b3-95a1-e1ed1112f988",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -260,
        1640
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "65924fac-5776-4e4f-928a-d1180e730a9c",
      "name": "OpenAI Chat Model2",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        300,
        1880
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "887dac9a-7bae-4797-8967-1114660c194a",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        560,
        1880
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"email_label\": \"business\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "fa913085-1c4f-4d61-938c-9bbf24fe9e69",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -360,
        1380
      ],
      "parameters": {
        "color": 7,
        "width": 560,
        "height": 700,
        "content": "## 1. Gmail Trigger + Label Checker\n[Read more about Gmail Trigger node Editing](https://docs.n8n.io/integrations/builtin/trigger-nodes/n8n-nodes-base.gmailtrigger/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.gmailTrigger)\n[Read more about Filter node editing](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.filter/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.filter)\n\n\ud83d\udcec Incoming email check\nThis section triggers when a new email is received in Gmail.\nIt uses a filter to ignore any emails that already have labels, so only unlabeled messages are processed.\nYou can adjust the frequency of the trigger (e.g., every minute, hours or Custom using Cron \u23f1\ufe0f) in the Gmail node."
      },
      "typeVersion": 1
    },
    {
      "id": "a9c899ef-a4f5-4947-9d9b-5d579ae58827",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1020,
        1380
      ],
      "parameters": {
        "width": 620,
        "height": 1100,
        "content": "##  Sort Gmail messages by category with AI \n\n## \ud83d\udc65 Who's it for  \nThis workflow is perfect for individuals or teams who receive a high volume of emails \ud83d\udce5 and want to automatically organize them into Gmail labels \ud83c\udff7\ufe0f using AI. No coding required!\n\n## \ud83e\udd16 What it does  \nIt automatically processes new Gmail emails, skips those that already have labels, sends the content to an AI Agent powered by GPT-4o mini \ud83e\udde0, and applies a relevant label based on the content. All labels must exist in Gmail beforehand.\n\n## \u2699\ufe0f How it works  \n1. **\ud83d\udcec Gmail Trigger** \u2013 Activates on new email received.  \n2. **\ud83d\udeab Filter** \u2013 Skips emails that already have a label.  \n3. **\ud83e\udde0 AI Agent (GPT-4o mini)** \u2013 Analyzes the message and decides which label fits best.  \n4. **\ud83e\uddfe Structured Output Parser** \u2013 Formats the AI output into a clean JSON.  \n5. **\ud83d\udd00 Switch Node** \u2013 Routes each email to the correct label path based on the AI result.  \n6. **\ud83c\udff7\ufe0f Gmail Nodes** \u2013 Assign the Gmail label to the original email.\n\n## \ud83d\udccb Requirements  \n- Gmail account connected to n8n  \n- Pre-created labels in Gmail matching the AI categories  \n- OpenAI credentials with GPT-4o mini access  \n- n8n's AI Agent & Structured Output Parser nodes  \n\n## \ud83d\udee0\ufe0f How to set up  \n1. Open the workflow and adjust the **trigger interval** (e.g., every minute, hours or Custom using Cron \u23f1\ufe0f)  \n2. Check that the **Filter** skips emails with existing labels  \n3. Define your categories in the **AI Agent prompt** and make sure they match the Gmail labels  \n4. Configure the **Switch Node** conditions for each category  \n5. Ensure each **Gmail Label Node** applies the correct label  \n6. Save and activate the workflow \u2705\n\n## \ud83c\udfa8 How to customize the workflow  \n- Add or remove categories in the AI prompt & Switch Node   \n- Fine-tune prompt instructions to match your specific use case  \n"
      },
      "typeVersion": 1
    },
    {
      "id": "0415122e-6746-408c-a4eb-5d128c80a921",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        220,
        1380
      ],
      "parameters": {
        "color": 7,
        "width": 560,
        "height": 700,
        "content": "## 2. AI Agent (GPT-4o mini + Output Parser)\n[Read more about the AI Agent node editing](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.agent)\n[Read more about the Structured Output Parser node editing](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparserstructured/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.outputParserStructured)\n\n\ud83e\udde0 AI-powered categorization\nThe email content is sent to an AI Agent using GPT-4o mini to determine the appropriate label.\nMake sure the prompt includes your predefined categories, and that these labels already exist in Gmail.\nThe Structured Output Parser ensures the AI response is formatted like this:\n{ \"email_label\": \"Your Category Name\" }"
      },
      "typeVersion": 1
    },
    {
      "id": "446e3625-8388-44f6-b04e-2d2483f1d4d5",
      "name": "Sticky Note13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        1380
      ],
      "parameters": {
        "color": 7,
        "width": 760,
        "height": 1300,
        "content": "## 3. Apply labels based on AI output\n[Read more about Switch node editing](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.switch/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.switch)\n[Read more about Gmail node editing](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.gmail/message-operations/#add-label-to-a-message)\n\n\ud83c\udff7\ufe0f Apply labels based on AI output\nThe Switch node uses email_label from the AI response to route each email to the correct label path.\nEach branch corresponds to one category.\nThen, each Gmail node applies the label that matches the category.\n\u26a0\ufe0f Make sure each route and label name matches exactly."
      },
      "typeVersion": 1
    },
    {
      "id": "4b4a928d-5305-40e5-a040-8a2e8787cf30",
      "name": "Give a Label AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        340,
        1640
      ],
      "parameters": {
        "text": "=Topic: {{ $json.subject }}\nDescription: {{ $json.text }}\nSender: {{ $json.from.text }}",
        "options": {
          "systemMessage": "=**Your Role**\n\nYou are an intelligent email assistant responsible for sorting incoming messages. Your task is to analyze the content, subject, and sender of each email and assign it to the appropriate category.\n\n**Categories**\n\nCategories for sorting:\n\n-Work: Emails related to jobs, coworkers, meetings, projects, or professional tasks.\n\n-Personal: Messages from friends, family, or general personal life content.\n\n-Finance: Bank emails, invoices, receipts, bills, or payment confirmations.\n\n-Shopping: Order confirmations, promotions, receipts, or product updates from stores.\n\n-Travel: Booking confirmations, tickets, hotel or car rental details, itineraries.\n\n-Newsletters: Recurring marketing or informational emails from subscriptions or blogs.\n\n-Others: Relevant emails that don\u2019t clearly fit into any of the categories above.\n\n**Instructions**\n\n-For each email:\n\n-Analyze the content, subject, and sender\n\n-Assign it to the appropriate category\n\n-Add a label with the category\n\n-If you are unsure, do not label the message\n\n**Response Format**\n\nAlways return the result in JSON format:\n\n{\n  \"email_label\": \"Shopping\"\n}\n\nIMPORTANT \u2013 the response must use the exact label format as listed (capitalized with spaces).\n"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.8
    },
    {
      "id": "5301b42e-ba3c-4f3c-8f84-e4f9ee4ca724",
      "name": "Label Checker Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        -40,
        1640
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6c287523-4ae2-43c4-8f9a-faf756f01855",
              "operator": {
                "type": "string",
                "operation": "notContains"
              },
              "leftValue": "={{ $json.labelIds[0] }}",
              "rightValue": "Label"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "3ccb8cd6-a329-4507-8158-cbc81b2d31da",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        840,
        1940
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Work",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "10893f03-231e-4dba-bfdf-69530f908b4d",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Work"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Personal",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "23e430c2-ac8b-4fb5-bb5a-1fafb9e41b09",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Personal"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Finance",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "53698e8e-f038-405d-a1d3-274dba7fb2a2",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Finance"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Shopping",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "b3b7ffc6-d046-402f-8c9d-2e19d00b49a7",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Shopping"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Travel",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "4e0a670b-4017-4d83-b01a-78a79c55daf2",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Travel"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Newsletters",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "f0079792-cf4b-4c3e-8aed-48e2dddb4e05",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Newsletters"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Others",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a0c39fdd-3312-4a34-8cd4-41a1a10f9149",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Others"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "70f0255c-de96-44a0-bcd0-748c9f6e4fe4",
      "name": "Work",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        1560
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "525f3f9f-9bc7-40f1-b6a5-8c3edd75025f",
      "name": "Personal",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        1720
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "84059ca7-f0cb-4582-bfb7-75f2678c1dae",
      "name": "Finance",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        1880
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "eb7f8180-c834-4f2a-b102-7e5bbacaca3b",
      "name": "Shopping",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1160,
        2020
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "12ba969b-d0aa-475c-ab5e-e4151f8df96e",
      "name": "travel",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        2140
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "41bebc40-ba6a-448e-8e28-d74b9ed73de6",
      "name": "Newsletters",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        2320
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "9b3690d5-419c-4b4f-9e86-8720a94818d9",
      "name": "Others",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1320,
        2500
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "551de827-cf86-472f-935e-99539be78588",
  "connections": {
    "Switch": {
      "main": [
        [
          {
            "node": "Work",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Personal",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Finance",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Shopping",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "travel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Newsletters",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Others",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Label Checker Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "Give a Label AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Label Checker Filter": {
      "main": [
        [
          {
            "node": "Give a Label AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Give a Label AI Agent": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "Give a Label AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}