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:
-
Trigger on New Email
- The
Gmail triggernode initiates the workflow whenever a new email is received in your inbox.
- The
-
Fetch Email Content
- The
Get message contentnode retrieves the full text of the new email using its message ID.
- The
-
AI-Powered Label Assignment
- The email content is passed to the
Assign labels for message(LLM Chain) node. - This chain leverages an
OpenAI Chatmodel to analyze the text. - A
JSON Parserensures the AI's output is a structured list of predefined labels (e.g.,["Inquiry", "Partnership"]).
- The email content is passed to the
-
Prepare and Match Labels
- The
Split out assigned labelsnode processes the list of labels suggested by the AI. - In parallel, the
Get all labelsnode fetches a complete list of available labels from your Gmail account. - The
Merge corresponding labelsnode matches the AI-suggested label names with the official labels from your account to find their corresponding IDs.
- The
-
Apply Labels to the Email
- The
Aggregate label IDsnode gathers the matched label IDs into a single array. - Finally, the
Add labels to messagenode applies these labels to the original email, completing the organization process.
- The
✅ 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
- Download workflow JSON
- Import into n8n
- Configure node parameters as needed
- Test and enable the workflow
💡 Tips
After importing, verify all connectors and credentials match your environment.
Preview
Workflow Visualization
Nodes
Sticky Note
stickyNote
Sticky Note1
stickyNote
Sticky Note2
stickyNote
Sticky Note4
stickyNote
Sticky Note5
stickyNote
Sticky Note6
stickyNote
Sticky Note7
stickyNote
Sticky Note8
stickyNote
Gmail trigger
gmailTrigger
Get message content
gmail
Assign labels for message
@n8n/langchain.chainLlm
OpenAI Chat
@n8n/langchain.lmChatOpenAi
JSON Parser
@n8n/langchain.outputParserStructured
Set label values
set
Get all labels
gmail
Split out assigned labels
splitOut
Merge corresponding labels
merge
Aggregate label IDs
aggregate
Add labels to message
gmail
Statistics
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
Technical Specs
Compatibility
License
This workflow template follows MIT license, you can freely use, modify and distribute.
Please comply with relevant third-party service terms when using.