first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,334 @@
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { OutputFixingParser, StructuredOutputParser } from '@langchain/classic/output_parsers';
import { NodeOperationError, NodeConnectionTypes, sleep } from 'n8n-workflow';
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeParameters,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { z } from 'zod';
import { getBatchingOptionFields } from '@n8n/ai-utilities';
import { processItem } from './processItem';
const SYSTEM_PROMPT_TEMPLATE =
"Please classify the text provided by the user into one of the following categories: {categories}, and use the provided formatting instructions below. Don't explain, and only output the json.";
const configuredOutputs = (parameters: INodeParameters) => {
const categories = ((parameters.categories as IDataObject)?.categories as IDataObject[]) ?? [];
const fallback = (parameters.options as IDataObject)?.fallback as string;
const ret = categories.map((cat) => {
return { type: 'main', displayName: cat.category };
});
if (fallback === 'other') ret.push({ type: 'main', displayName: 'Other' });
return ret;
};
export class TextClassifier implements INodeType {
description: INodeTypeDescription = {
displayName: 'Text Classifier',
name: 'textClassifier',
icon: 'fa:tags',
iconColor: 'black',
group: ['transform'],
version: [1, 1.1],
description: 'Classify your text into distinct categories',
codex: {
categories: ['AI'],
subcategories: {
AI: ['Chains', 'Root Nodes'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.text-classifier/',
},
],
},
},
defaults: {
name: 'Text Classifier',
},
inputs: [
{ displayName: '', type: NodeConnectionTypes.Main },
{
displayName: 'Model',
maxConnections: 1,
type: NodeConnectionTypes.AiLanguageModel,
required: true,
},
],
outputs: `={{(${configuredOutputs})($parameter)}}`,
builderHint: {
inputs: {
ai_languageModel: { required: true },
},
message:
'Each category defined creates a separate output branch. Output 0 corresponds to the first category, output 1 to the second, and so on. Use .output(index).to() to connect from a specific category. @example textClassifier.output(0).to(nodeA) and textClassifier.output(1).to(nodeB)',
},
properties: [
{
displayName: 'Text to Classify',
name: 'inputText',
type: 'string',
required: true,
default: '',
description: 'Use an expression to reference data in previous nodes or enter static text',
typeOptions: {
rows: 2,
},
},
{
displayName: 'Categories',
name: 'categories',
placeholder: 'Add Category',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
name: 'categories',
displayName: 'Categories',
values: [
{
displayName: 'Category',
name: 'category',
type: 'string',
default: '',
description: 'Category to add',
required: true,
},
{
displayName: 'Description',
name: 'description',
type: 'string',
default: '',
description: "Describe your category if it's not obvious",
},
],
},
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
default: {},
placeholder: 'Add Option',
options: [
{
displayName: 'Allow Multiple Classes To Be True',
name: 'multiClass',
type: 'boolean',
default: false,
},
{
displayName: 'When No Clear Match',
name: 'fallback',
type: 'options',
default: 'discard',
description: 'What to do with items that dont match the categories exactly',
options: [
{
name: 'Discard Item',
value: 'discard',
description: 'Ignore the item and drop it from the output',
},
{
name: "Output on Extra, 'Other' Branch",
value: 'other',
description: "Create a separate output branch called 'Other'",
},
],
},
{
displayName: 'System Prompt Template',
name: 'systemPromptTemplate',
type: 'string',
default: SYSTEM_PROMPT_TEMPLATE,
description: 'String to use directly as the system prompt template',
typeOptions: {
rows: 6,
},
},
{
displayName: 'Enable Auto-Fixing',
name: 'enableAutoFixing',
type: 'boolean',
default: true,
description:
'Whether to enable auto-fixing (may trigger an additional LLM call if output is broken)',
},
getBatchingOptionFields({
show: {
'@version': [{ _cnd: { gte: 1.1 } }],
},
}),
],
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const batchSize = this.getNodeParameter('options.batching.batchSize', 0, 5) as number;
const delayBetweenBatches = this.getNodeParameter(
'options.batching.delayBetweenBatches',
0,
0,
) as number;
const llm = (await this.getInputConnectionData(
NodeConnectionTypes.AiLanguageModel,
0,
)) as BaseLanguageModel;
const categories = this.getNodeParameter('categories.categories', 0, []) as Array<{
category: string;
description: string;
}>;
if (categories.length === 0) {
throw new NodeOperationError(this.getNode(), 'At least one category must be defined');
}
const options = this.getNodeParameter('options', 0, {}) as {
multiClass: boolean;
fallback?: string;
systemPromptTemplate?: string;
enableAutoFixing: boolean;
};
const multiClass = options?.multiClass ?? false;
const fallback = options?.fallback ?? 'discard';
const schemaEntries = categories.map((cat) => [
cat.category,
z
.boolean()
.describe(
`Should be true if the input has category "${cat.category}" (description: ${cat.description})`,
),
]);
if (fallback === 'other')
schemaEntries.push([
'fallback',
z.boolean().describe('Should be true if none of the other categories apply'),
]);
const schema = z.object(Object.fromEntries(schemaEntries));
const structuredParser = StructuredOutputParser.fromZodSchema(schema);
const parser = options.enableAutoFixing
? OutputFixingParser.fromLLM(llm, structuredParser)
: structuredParser;
const multiClassPrompt = multiClass
? 'Categories are not mutually exclusive, and multiple can be true'
: 'Categories are mutually exclusive, and only one can be true';
const fallbackPrompt = {
other: 'If no categories apply, select the "fallback" option.',
discard: 'If there is not a very fitting category, select none of the categories.',
}[fallback];
const returnData: INodeExecutionData[][] = Array.from(
{ length: categories.length + (fallback === 'other' ? 1 : 0) },
(_) => [],
);
if (this.getNode().typeVersion >= 1.1 && batchSize > 1) {
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
const batchPromises = batch.map(async (_item, batchItemIndex) => {
const itemIndex = i + batchItemIndex;
const item = items[itemIndex];
return await processItem(
this,
itemIndex,
item,
llm,
parser,
categories,
multiClassPrompt,
fallbackPrompt,
);
});
const batchResults = await Promise.allSettled(batchPromises);
batchResults.forEach((response, batchItemIndex) => {
const index = i + batchItemIndex;
if (response.status === 'rejected') {
const error = response.reason as Error;
if (this.continueOnFail()) {
returnData[0].push({
json: { error: error.message },
pairedItem: { item: index },
});
return;
} else {
throw new NodeOperationError(this.getNode(), error.message);
}
} else {
const output = response.value;
const item = items[index];
categories.forEach((cat, idx) => {
if (output[cat.category]) returnData[idx].push(item);
});
if (fallback === 'other' && output.fallback)
returnData[returnData.length - 1].push(item);
}
});
// Add delay between batches if not the last batch
if (i + batchSize < items.length && delayBetweenBatches > 0) {
await sleep(delayBetweenBatches);
}
}
} else {
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const item = items[itemIndex];
try {
const output = await processItem(
this,
itemIndex,
item,
llm,
parser,
categories,
multiClassPrompt,
fallbackPrompt,
);
categories.forEach((cat, idx) => {
if (output[cat.category]) returnData[idx].push(item);
});
if (fallback === 'other' && output.fallback) returnData[returnData.length - 1].push(item);
} catch (error) {
if (this.continueOnFail()) {
returnData[0].push({
json: { error: error.message },
pairedItem: { item: itemIndex },
});
continue;
}
throw error;
}
}
}
return returnData;
}
}
@@ -0,0 +1,2 @@
export const SYSTEM_PROMPT_TEMPLATE =
"Please classify the text provided by the user into one of the following categories: {categories}, and use the provided formatting instructions below. Don't explain, and only output the json.";
@@ -0,0 +1,61 @@
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { HumanMessage } from '@langchain/core/messages';
import { ChatPromptTemplate, SystemMessagePromptTemplate } from '@langchain/core/prompts';
import type { OutputFixingParser, StructuredOutputParser } from '@langchain/classic/output_parsers';
import { NodeOperationError, type IExecuteFunctions, type INodeExecutionData } from 'n8n-workflow';
import { getTracingConfig } from '@utils/tracing';
import { SYSTEM_PROMPT_TEMPLATE } from './constants';
export async function processItem(
ctx: IExecuteFunctions,
itemIndex: number,
item: INodeExecutionData,
llm: BaseLanguageModel,
parser: StructuredOutputParser<any> | OutputFixingParser<any>,
categories: Array<{ category: string; description: string }>,
multiClassPrompt: string,
fallbackPrompt: string | undefined,
): Promise<Record<string, unknown>> {
const input = ctx.getNodeParameter('inputText', itemIndex) as string;
if (!input) {
throw new NodeOperationError(
ctx.getNode(),
`Text to classify for item ${itemIndex} is not defined`,
);
}
item.pairedItem = { item: itemIndex };
const inputPrompt = new HumanMessage(input);
const systemPromptTemplateOpt = ctx.getNodeParameter(
'options.systemPromptTemplate',
itemIndex,
SYSTEM_PROMPT_TEMPLATE,
) as string;
const escapedTemplate = (systemPromptTemplateOpt ?? SYSTEM_PROMPT_TEMPLATE)
.replace(/[{}]/g, (match) => match + match)
.replaceAll('{{categories}}', '{categories}');
const systemPromptTemplate = SystemMessagePromptTemplate.fromTemplate(
`${escapedTemplate}
{format_instructions}
${multiClassPrompt}
${fallbackPrompt}`,
);
const messages = [
await systemPromptTemplate.format({
categories: categories.map((cat) => cat.category).join(', '),
format_instructions: parser.getFormatInstructions(),
}),
inputPrompt,
];
const prompt = ChatPromptTemplate.fromMessages(messages);
const chain = prompt.pipe(llm).pipe(parser).withConfig(getTracingConfig(ctx));
return await chain.invoke(messages);
}
@@ -0,0 +1,199 @@
import { FakeChatModel } from '@langchain/core/utils/testing';
import { mock } from 'jest-mock-extended';
import type { IExecuteFunctions, INode } from 'n8n-workflow';
import { processItem } from '../processItem';
import { TextClassifier } from '../TextClassifier.node';
jest.mock('../processItem', () => ({
processItem: jest.fn(),
}));
describe('TextClassifier Node', () => {
let node: TextClassifier;
let mockExecuteFunction: jest.Mocked<IExecuteFunctions>;
beforeEach(() => {
jest.resetAllMocks();
node = new TextClassifier();
mockExecuteFunction = mock<IExecuteFunctions>();
mockExecuteFunction.logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
mockExecuteFunction.getInputData.mockReturnValue([{ json: { testValue: 'none' } }]);
mockExecuteFunction.getNode.mockReturnValue({
name: 'Text Classifier',
typeVersion: 1.1,
parameters: {},
} as INode);
mockExecuteFunction.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'categories.categories')
return [{ category: 'test', description: 'test category' }];
return defaultValue;
});
const fakeLLM = new FakeChatModel({});
mockExecuteFunction.getInputConnectionData.mockResolvedValue(fakeLLM);
});
describe('execute', () => {
it('should process items with correct parameters', async () => {
(processItem as jest.Mock).mockResolvedValue({ test: true });
const result = await node.execute.call(mockExecuteFunction);
expect(processItem).toHaveBeenCalledWith(
mockExecuteFunction,
0,
{ json: { testValue: 'none' } },
expect.any(FakeChatModel),
expect.any(Object),
[{ category: 'test', description: 'test category' }],
expect.any(String),
'If there is not a very fitting category, select none of the categories.',
);
expect(result).toEqual([[{ json: { testValue: 'none' } }]]);
});
it('should handle multiple input items', async () => {
mockExecuteFunction.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'categories.categories')
return [
{ category: 'test1', description: 'test category' },
{ category: 'test2', description: 'some other category' },
];
return defaultValue;
});
mockExecuteFunction.getInputData.mockReturnValue([
{ json: { item: 1 } },
{ json: { item: 2 } },
]);
(processItem as jest.Mock)
.mockResolvedValueOnce({ test1: true, test2: false })
.mockResolvedValueOnce({ test1: false, test2: true });
const result = await node.execute.call(mockExecuteFunction);
expect(processItem).toHaveBeenCalledTimes(2);
expect(result).toHaveLength(2);
expect(result[0][0].json).toEqual({ item: 1 });
expect(result[1][0].json).toEqual({ item: 2 });
});
it('should process items in batches when batchSize is set', async () => {
mockExecuteFunction.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'categories.categories')
return [{ category: 'test', description: 'test category' }];
if (param === 'batchSize') return 2;
return defaultValue;
});
mockExecuteFunction.getInputData.mockReturnValue([
{ json: { item: 1 } },
{ json: { item: 2 } },
{ json: { item: 3 } },
{ json: { item: 4 } },
]);
(processItem as jest.Mock)
.mockResolvedValueOnce({ test: true })
.mockResolvedValueOnce({ test: true })
.mockResolvedValueOnce({ test: true })
.mockResolvedValueOnce({ test: true });
const result = await node.execute.call(mockExecuteFunction);
expect(processItem).toHaveBeenCalledTimes(4);
expect(result[0]).toHaveLength(4);
expect(result[0]).toEqual([
{ json: { item: 1 } },
{ json: { item: 2 } },
{ json: { item: 3 } },
{ json: { item: 4 } },
]);
});
it('should respect delayBetweenBatches', async () => {
mockExecuteFunction.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'categories.categories')
return [{ category: 'test', description: 'test category' }];
if (param === 'options.batching.batchSize') return 2;
if (param === 'options.batching.delayBetweenBatches') return 100;
return defaultValue;
});
mockExecuteFunction.getInputData.mockReturnValue([
{ json: { item: 1 } },
{ json: { item: 2 } },
{ json: { item: 3 } },
{ json: { item: 4 } },
{ json: { item: 5 } },
{ json: { item: 6 } },
]);
(processItem as jest.Mock).mockResolvedValue({ test: true });
const startTime = Date.now();
await node.execute.call(mockExecuteFunction);
const endTime = Date.now();
expect(endTime - startTime).toBeGreaterThanOrEqual(200);
});
it('should handle errors in batch processing', async () => {
mockExecuteFunction.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'categories.categories')
return [{ category: 'test', description: 'test category' }];
if (param === 'batchSize') return 2;
return defaultValue;
});
mockExecuteFunction.getInputData.mockReturnValue([
{ json: { item: 1 } },
{ json: { item: 2 } },
{ json: { item: 3 } },
]);
(processItem as jest.Mock)
.mockResolvedValueOnce({ test: true })
.mockRejectedValueOnce(new Error('Batch error'))
.mockResolvedValueOnce({ test: true });
mockExecuteFunction.continueOnFail.mockReturnValue(true);
const result = await node.execute.call(mockExecuteFunction);
expect(result[0]).toHaveLength(3);
expect(result[0][1].json).toHaveProperty('error', 'Batch error');
});
it('should throw error when continueOnFail is false', async () => {
mockExecuteFunction.continueOnFail.mockReturnValue(false);
(processItem as jest.Mock).mockRejectedValue(new Error('Test error'));
await expect(node.execute.call(mockExecuteFunction)).rejects.toThrow('Test error');
});
it('should continue on failure when configured', async () => {
mockExecuteFunction.continueOnFail.mockReturnValue(true);
(processItem as jest.Mock).mockRejectedValue(new Error('Test error'));
const result = await node.execute.call(mockExecuteFunction);
expect(result).toEqual([[{ json: { error: 'Test error' }, pairedItem: { item: 0 } }]]);
});
});
});
@@ -0,0 +1,188 @@
import { ChatPromptTemplate, SystemMessagePromptTemplate } from '@langchain/core/prompts';
import { FakeChatModel } from '@langchain/core/utils/testing';
import { mock } from 'jest-mock-extended';
import type { IExecuteFunctions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import * as tracing from '@utils/tracing';
import { processItem } from '../processItem';
jest.mock('@utils/tracing', () => ({
getTracingConfig: jest.fn(() => ({})),
}));
jest.mock('@langchain/core/prompts', () => ({
ChatPromptTemplate: {
fromMessages: jest.fn(),
},
SystemMessagePromptTemplate: {
fromTemplate: jest.fn().mockReturnValue({
format: jest.fn(),
}),
},
}));
describe('processItem', () => {
let mockContext: jest.Mocked<IExecuteFunctions>;
let fakeLLM: FakeChatModel;
beforeEach(() => {
mockContext = mock<IExecuteFunctions>();
fakeLLM = new FakeChatModel({});
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'options.systemPromptTemplate') return 'Test system prompt';
return defaultValue;
});
jest.clearAllMocks();
});
it('should throw error for empty input text', async () => {
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return '';
return defaultValue;
});
await expect(
processItem(
mockContext,
0,
{ json: {} },
fakeLLM,
{ getFormatInstructions: () => 'format instructions' } as any,
[{ category: 'test', description: 'test category' }],
'multi class prompt',
undefined,
),
).rejects.toThrow(NodeOperationError);
});
it('should process item with correct parameters', async () => {
const mockParser = {
getFormatInstructions: () => '[format instructions]',
};
const mockChain = {
invoke: jest.fn().mockResolvedValue({ test: true }),
};
const mockPipe = jest.fn().mockReturnValue({
pipe: jest.fn().mockReturnValue({
withConfig: jest.fn().mockReturnValue(mockChain),
}),
});
const mockPrompt = {
pipe: mockPipe,
};
jest.mocked(ChatPromptTemplate.fromMessages).mockReturnValue(mockPrompt as any);
const result = await processItem(
mockContext,
0,
{ json: {} },
fakeLLM,
mockParser as any,
[{ category: 'test', description: 'test category' }],
'multi class prompt',
undefined,
);
expect(result).toEqual({ test: true });
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('inputText', 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith(
'options.systemPromptTemplate',
0,
expect.any(String),
);
expect(tracing.getTracingConfig).toHaveBeenCalledWith(mockContext);
});
it('should escape curly braces in custom system prompt template', async () => {
const templateWithBraces = 'Classify using format {"category": "value"}: {categories}';
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
if (param === 'inputText') return 'Test input';
if (param === 'options.systemPromptTemplate') return templateWithBraces;
return defaultValue;
});
const mockParser = {
getFormatInstructions: () => '[format instructions]',
};
const mockChain = {
invoke: jest.fn().mockResolvedValue({ category: 'test' }),
};
const mockPipe = jest.fn().mockReturnValue({
pipe: jest.fn().mockReturnValue({
withConfig: jest.fn().mockReturnValue(mockChain),
}),
});
jest.mocked(ChatPromptTemplate.fromMessages).mockReturnValue({ pipe: mockPipe } as any);
await processItem(
mockContext,
0,
{ json: {} },
fakeLLM,
mockParser as any,
[{ category: 'test', description: 'test category' }],
'multi class prompt',
undefined,
);
expect(SystemMessagePromptTemplate.fromTemplate).toHaveBeenCalledWith(
expect.stringContaining('{{"category": "value"}}'),
);
expect(SystemMessagePromptTemplate.fromTemplate).toHaveBeenCalledWith(
expect.stringContaining('{categories}'),
);
});
it('should handle fallback prompt', async () => {
const mockParser = {
getFormatInstructions: () => '[format instructions]',
};
const mockChain = {
invoke: jest.fn().mockResolvedValue({ category: 'test' }),
};
const mockPipe = jest.fn().mockReturnValue({
pipe: jest.fn().mockReturnValue({
withConfig: jest.fn().mockReturnValue(mockChain),
}),
});
const mockPrompt = {
pipe: mockPipe,
};
jest.mocked(ChatPromptTemplate.fromMessages).mockReturnValue(mockPrompt as any);
await processItem(
mockContext,
0,
{ json: {} },
fakeLLM,
mockParser as any,
[{ category: 'test', description: 'test category' }],
'multi class prompt',
'fallback prompt',
);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith(
'options.systemPromptTemplate',
0,
expect.any(String),
);
expect(tracing.getTracingConfig).toHaveBeenCalledWith(mockContext);
});
});