AI-Powered Email Categorization and Labeling for Gmail - n8n Workflow

Streamline your inbox management using this advanced n8n workflow. Automatically analyze incoming Gmail messages with OpenAI to apply relevant labels like 'Inquiry' or 'Partnership'. Perfect for efficient n8n templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Individuals or teams drowning in unorganized email inboxes.

  • Users who need automated email triage and categorization.

  • Developers looking for advanced n8n templates combining Gmail and OpenAI.

  • Anyone seeking to implement custom AI logic within their n8n workflow infrastructure.

Overview

Managing a high volume of emails requires efficient categorization, but manual labeling is time-consuming. This specialized n8n workflow solves this by integrating a large language model (LLM) to perform the categorization automatically.

When a new email arrives, this n8n automation fetches the content, feeds it to the OpenAI n8n node, and receives a structured categorization (e.g., 'Partnership' or 'Inquiry'). It then uses the resulting label names to find the corresponding Gmail label IDs and applies them to the original message. This results in a clean, categorized inbox without human intervention. This entire process showcases the power of building complex logic using various n8n node types.

How it Works

This high-utility n8n workflow executes in several distinct phases:


  1. Trigger and Fetch: The process starts with the Gmail trigger n8n node, which constantly polls for new, incoming messages. Once triggered, the workflow immediately passes the message ID to a second Gmail n8n node, which retrieves the full message content necessary for AI analysis.

  2. AI Categorization: The content is passed into the LangChain Assign labels for message n8n node (a specialized LLM Chain). This chain is composed of the OpenAI Chat model (gpt-4o-mini) and a structured JSON Parser n8n node.

  3. Structured Output: The AI reads the email content and uses the predefined prompt and JSON schema to output a list of appropriate label names (limited to 'Inquiry', 'Partnership', or 'Notification').

  4. Data Preparation: A Set n8n node extracts this AI-assigned label array. Simultaneously, a separate Gmail n8n node fetches a complete list of all existing Gmail labels and their corresponding unique IDs.

  5. ID Matching: The workflow uses a Split Out n8n node to separate the assigned labels into individual items. These are then combined with the list of all available labels using the Merge corresponding labels n8n node, matching the label name with the official Gmail ID.

  6. Final Action: An Aggregate n8n node collects all the newly matched Gmail IDs into a final array structure. The workflow concludes by sending this ID array back to a Gmail n8n node, which executes the addLabels operation on the original email message ID, completing the n8n automation cycle.

Installation Guide

To successfully deploy this powerful n8n workflow, follow these steps:


  1. Import: Copy the provided JSON and import it into your n8n instance as a new n8n workflow.

  2. Credentials: You will need two sets of credentials:

Gmail OAuth2: Configure your Gmail credentials for the Gmail trigger and all subsequent Gmail n8n node operations. Ensure these credentials have adequate scopes to read messages and manage labels.
OpenAI API: Configure your OpenAI API Key for the OpenAI Chat n8n node.

  1. Gmail Labels Setup: Ensure the required labels ('Partnership', 'Inquiry', 'Notification') are already created in your Gmail account. Crucially, the names must match the configuration within the JSON Parser n8n node.

  2. Configuration Check: Review the system prompt in the Assign labels for message n8n node and the JSON Schema in the JSON Parser n8n node to customize or confirm the target label names.

  3. Activation: Save the n8n workflow and activate the Gmail trigger to begin automated polling.

Node Details

Gmail trigger: This is the starting n8n node. It monitors your inbox for new incoming messages, providing the initial message metadata.
Get message content (Gmail): Uses the message ID provided by the trigger to fetch the complete body and content of the incoming email, preparing it for AI analysis.
Assign labels for message (Chain LLM): The intelligence core of this n8n workflow. This LangChain n8n node orchestrates the LLM interaction and passes the email text for categorization based on a strict system prompt.
OpenAI Chat: The specific LLM used (configured as gpt-4o-mini) to perform the content classification.
JSON Parser (Output Parser Structured): Ensures the AI's textual output is converted into a reliable, structured JSON object with the required labels array key, enforcing the desired output format for this n8n automation.
Get all labels (Gmail): Fetches all user-defined labels from Gmail, which is necessary to retrieve the unique id required for updating messages.
Merge corresponding labels (Merge): A vital n8n node that matches the AI-assigned label names (strings) with the actual Gmail label IDs (the identifiers required by the Gmail API), enabling the final update step.
Add labels to message (Gmail): The final Gmail n8n node operation. It applies the aggregated list of official Gmail label IDs back to the original message, concluding the n8n workflow.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all
Created by

I make videos about workflow automation and other cool things.

Featured*