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,267 @@
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { PromptTemplate } from '@langchain/core/prompts';
import type { JSONSchema7 } from 'json-schema';
import {
jsonParse,
type INodeType,
type INodeTypeDescription,
type ISupplyDataFunctions,
type SupplyData,
NodeOperationError,
NodeConnectionTypes,
} from 'n8n-workflow';
import type { z } from 'zod';
import {
buildJsonSchemaExampleNotice,
inputSchemaField,
jsonSchemaExampleField,
schemaTypeField,
} from '@utils/descriptions';
import {
N8nOutputFixingParser,
N8nStructuredOutputParser,
} from '@utils/output_parsers/N8nOutputParser';
import { convertJsonSchemaToZod, generateSchemaFromExample } from '@utils/schemaParsing';
import { getConnectionHintNoticeField } from '@n8n/ai-utilities';
import { NAIVE_FIX_PROMPT } from './prompt';
export class OutputParserStructured implements INodeType {
description: INodeTypeDescription = {
displayName: 'Structured Output Parser',
name: 'outputParserStructured',
icon: 'fa:code',
iconColor: 'black',
group: ['transform'],
version: [1, 1.1, 1.2, 1.3],
defaultVersion: 1.3,
description: 'Return data in a defined JSON format',
defaults: {
name: 'Structured Output Parser',
},
codex: {
alias: ['json', 'zod'],
categories: ['AI'],
subcategories: {
AI: ['Output Parsers'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparserstructured/',
},
],
},
},
inputs: `={{
((parameters) => {
if (parameters?.autoFix) {
return [
{ displayName: 'Model', maxConnections: 1, type: "${NodeConnectionTypes.AiLanguageModel}", required: true }
];
}
return [];
})($parameter)
}}`,
outputs: [NodeConnectionTypes.AiOutputParser],
outputNames: ['Output Parser'],
properties: [
getConnectionHintNoticeField([NodeConnectionTypes.AiChain, NodeConnectionTypes.AiAgent]),
{ ...schemaTypeField, displayOptions: { show: { '@version': [{ _cnd: { gte: 1.2 } }] } } },
{
...jsonSchemaExampleField,
default: `{
"state": "California",
"cities": ["Los Angeles", "San Francisco", "San Diego"]
}`,
},
buildJsonSchemaExampleNotice({
showExtraProps: {
'@version': [{ _cnd: { gte: 1.3 } }],
},
}),
{
...inputSchemaField,
default: `{
"type": "object",
"properties": {
"state": {
"type": "string"
},
"cities": {
"type": "array",
"items": {
"type": "string"
}
}
}
}`,
},
{
displayName: 'JSON Schema',
name: 'jsonSchema',
type: 'json',
description: 'JSON Schema to structure and validate the output against',
default: `{
"type": "object",
"properties": {
"state": {
"type": "string"
},
"cities": {
"type": "array",
"items": {
"type": "string"
}
}
}
}`,
typeOptions: {
rows: 10,
},
required: true,
displayOptions: {
show: {
'@version': [{ _cnd: { lte: 1.1 } }],
},
},
},
{
displayName: 'Auto-Fix Format',
description:
'Whether to automatically fix the output when it is not in the correct format. Will cause another LLM call.',
name: 'autoFix',
type: 'boolean',
default: false,
},
{
displayName: 'Customize Retry Prompt',
name: 'customizeRetryPrompt',
type: 'boolean',
displayOptions: {
show: {
autoFix: [true],
},
},
default: false,
description:
'Whether to customize the prompt used for retrying the output parsing. If disabled, a default prompt will be used.',
},
{
displayName: 'Custom Prompt',
name: 'prompt',
type: 'string',
displayOptions: {
show: {
autoFix: [true],
customizeRetryPrompt: [true],
},
},
default: NAIVE_FIX_PROMPT,
typeOptions: {
rows: 10,
},
hint: 'Should include "{error}", "{instructions}", and "{completion}" placeholders',
description:
'Prompt template used for fixing the output. Uses placeholders: "{instructions}" for parsing rules, "{completion}" for the failed attempt, and "{error}" for the validation error message.',
},
],
hints: [
{
message:
'Fields that use $refs might have the wrong type, since this syntax is not currently supported',
type: 'warning',
location: 'outputPane',
whenToDisplay: 'afterExecution',
displayCondition:
'={{ $parameter["schemaType"] === "manual" && $parameter["inputSchema"]?.includes("$ref") }}',
},
],
builderHint: {
message:
'Output data is wrapped in an "output" key, e.g. { "output": { "state": "California", "cities": ["San Francisco"] } }',
inputs: {
ai_languageModel: {
required: true,
displayOptions: { show: { autoFix: [true] } },
},
},
},
};
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
const schemaType = this.getNodeParameter('schemaType', itemIndex, '') as 'fromJson' | 'manual';
// We initialize these even though one of them will always be empty
// it makes it easer to navigate the ternary operator
const jsonExample = this.getNodeParameter('jsonSchemaExample', itemIndex, '') as string;
let inputSchema: string;
// Enforce all fields to be required in the generated schema if the node version is 1.3 or higher
const jsonExampleAllFieldsRequired = this.getNode().typeVersion >= 1.3;
if (this.getNode().typeVersion <= 1.1) {
inputSchema = this.getNodeParameter('jsonSchema', itemIndex, '') as string;
} else {
inputSchema = this.getNodeParameter('inputSchema', itemIndex, '') as string;
}
const jsonSchema =
schemaType === 'fromJson'
? generateSchemaFromExample(jsonExample, jsonExampleAllFieldsRequired)
: jsonParse<JSONSchema7>(inputSchema);
const zodSchema = convertJsonSchemaToZod<z.ZodSchema<object>>(jsonSchema);
const nodeVersion = this.getNode().typeVersion;
const autoFix = this.getNodeParameter('autoFix', itemIndex, false) as boolean;
let outputParser;
try {
outputParser = await N8nStructuredOutputParser.fromZodJsonSchema(
zodSchema,
nodeVersion,
this,
);
} catch (error) {
throw new NodeOperationError(
this.getNode(),
'Error during parsing of JSON Schema. Please check the schema and try again.',
);
}
if (!autoFix) {
return {
response: outputParser,
};
}
const model = (await this.getInputConnectionData(
NodeConnectionTypes.AiLanguageModel,
itemIndex,
)) as BaseLanguageModel;
const prompt = this.getNodeParameter('prompt', itemIndex, NAIVE_FIX_PROMPT) as string;
if (prompt.length === 0 || !prompt.includes('{error}')) {
throw new NodeOperationError(
this.getNode(),
'Auto-fixing parser prompt has to contain {error} placeholder',
);
}
const parser = new N8nOutputFixingParser(
this,
model,
outputParser,
PromptTemplate.fromTemplate(prompt),
);
return {
response: parser,
};
}
}
@@ -0,0 +1,16 @@
export const NAIVE_FIX_PROMPT = `Instructions:
--------------
{instructions}
--------------
Completion:
--------------
{completion}
--------------
Above, the Completion did not satisfy the constraints given in the Instructions.
Error:
--------------
{error}
--------------
Please try again. Please only respond with an answer that satisfies the constraints laid out in the Instructions:`;
@@ -0,0 +1,953 @@
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { OutputParserException } from '@langchain/core/output_parsers';
import { mock, type MockProxy } from 'jest-mock-extended';
import { normalizeItems } from 'n8n-core';
import {
jsonParse,
NodeConnectionTypes,
NodeOperationError,
type INode,
type ISupplyDataFunctions,
type IWorkflowDataProxyData,
} from 'n8n-workflow';
import {
N8nStructuredOutputParser,
type N8nOutputFixingParser,
} from '@utils/output_parsers/N8nOutputParser';
import { OutputParserStructured } from '../OutputParserStructured.node';
import { NAIVE_FIX_PROMPT } from '../prompt';
describe('OutputParserStructured', () => {
let outputParser: OutputParserStructured;
const thisArg = mock<ISupplyDataFunctions>({
helpers: { normalizeItems },
});
const workflowDataProxy = mock<IWorkflowDataProxyData>({ $input: mock() });
beforeEach(() => {
outputParser = new OutputParserStructured();
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
});
describe('supplyData', () => {
describe('Version 1.1 and below', () => {
beforeEach(() => {
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.1 }));
});
it('should parse a complex nested schema', async () => {
const schema = `{
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"name": { "type": "string" },
"details": {
"type": "object",
"properties": {
"age": { "type": "number" },
"hobbies": { "type": "array", "items": { "type": "string" } }
}
}
}
},
"timestamp": { "type": "string", "format": "date-time" }
},
"required": ["user", "timestamp"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
user: {
name: 'Alice',
details: {
age: 30,
hobbies: ['reading', 'hiking'],
},
},
timestamp: '2023-04-01T12:00:00Z',
},
};
const parsersOutput = await response.parse(`Here's the complex output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should handle optional fields correctly', async () => {
const schema = `{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "number" },
"email": { "type": "string", "format": "email" }
},
"required": ["name"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
name: 'Bob',
email: 'bob@example.com',
},
};
const parsersOutput = await response.parse(`Here's the output with optional fields:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should handle arrays of objects', async () => {
const schema = `{
"type": "object",
"properties": {
"users": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "number" },
"name": { "type": "string" }
},
"required": ["id", "name"]
}
}
},
"required": ["users"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
users: [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
],
},
};
const parsersOutput = await response.parse(`Here's the array output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should handle empty objects', async () => {
const schema = `{
"type": "object",
"properties": {
"data": {
"type": "object"
}
},
"required": ["data"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
data: {},
},
};
const parsersOutput = await response.parse(`Here's the empty object output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should throw error for null values in non-nullable fields', async () => {
const schema = `{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "number" }
},
"required": ["name", "age"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
name: 'Charlie',
age: null,
},
};
await expect(
response.parse(
`Here's the output with null value:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow('Expected number, received null');
});
});
describe('Version 1.2 and above', () => {
beforeEach(() => {
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.2 }));
});
it('should parse output using schema generated from complex JSON example', async () => {
const jsonExample = `{
"user": {
"name": "Alice",
"details": {
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipCode": "12345"
}
}
},
"orders": [
{
"id": "ORD-001",
"items": ["item1", "item2"],
"total": 50.99
},
{
"id": "ORD-002",
"items": ["item3"],
"total": 25.50
}
],
"isActive": true
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: jsonParse(jsonExample),
};
const parsersOutput = await response.parse(`Here's the complex output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should validate enum values', async () => {
const inputSchema = `{
"type": "object",
"properties": {
"color": {
"type": "string",
"enum": ["red", "green", "blue"]
}
},
"required": ["color"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(inputSchema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const validOutput = {
output: {
color: 'green',
},
};
const invalidOutput = {
output: {
color: 'yellow',
},
};
await expect(
response.parse(`Valid output:
\`\`\`json
${JSON.stringify(validOutput)}
\`\`\`
`),
).resolves.toEqual(validOutput);
await expect(
response.parse(
`Invalid output:
\`\`\`json
${JSON.stringify(invalidOutput)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow();
});
it('should handle recursive structures', async () => {
const inputSchema = `{
"type": "object",
"properties": {
"name": { "type": "string" },
"children": {
"type": "array",
"items": { "$ref": "#" }
}
},
"required": ["name"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(inputSchema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
name: 'Root',
children: [
{
name: 'Child1',
children: [{ name: 'Grandchild1' }, { name: 'Grandchild2' }],
},
{
name: 'Child2',
},
],
},
};
const parsersOutput = await response.parse(`Here's the recursive structure output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should handle missing required properties', async () => {
const schema = `{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"required": ["name", "age"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = { output: { name: 'Mac' } };
await expect(
response.parse(
`Here's the output!
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow('Required');
});
it('should throw on wrong type', async () => {
const schema = `{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"required": ["name", "age"]
}`;
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = { output: { name: 'Mac', age: '27' } };
await expect(
response.parse(
`Here's the output!
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow('Expected number, received string');
});
it('should parse array output', async () => {
const schema = `{
"type": "object",
"properties": {
"myArr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"required": ["name", "age"]
}
}
},
"required": ["myArr"]
}`;
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
myArr: [
{ name: 'Mac', age: 27 },
{ name: 'Alice', age: 25 },
],
},
};
const parsersOutput = await response.parse(`Here's the output!
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
});
describe('Version 1.3', () => {
beforeEach(() => {
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.3 }));
});
describe('schema from JSON example', () => {
it('should make all fields required when generating schema from JSON example', async () => {
const jsonExample = `{
"user": {
"name": "Alice",
"email": "alice@example.com",
"profile": {
"age": 30,
"city": "New York"
}
},
"tags": ["work", "important"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
user: {
name: 'Bob',
email: 'bob@example.com',
profile: {
age: 25,
city: 'San Francisco',
},
},
tags: ['personal'],
},
};
const parsersOutput = await response.parse(`Here's the user data:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
it('should reject output missing required fields from JSON example', async () => {
const jsonExample = `{
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const incompleteOutput = {
output: {
name: 'Bob',
age: 25,
// Missing email field
},
};
await expect(
response.parse(
`Here's the incomplete output:
\`\`\`json
${JSON.stringify(incompleteOutput)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow('Required');
});
it('should require all fields in array items from JSON example', async () => {
const jsonExample = `{
"users": [
{
"id": 1,
"name": "Alice",
"metadata": {
"department": "Engineering",
"role": "Developer"
}
}
]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const incompleteArrayOutput = {
output: {
users: [
{
id: 2,
name: 'Bob',
metadata: {
department: 'Marketing',
// Missing role field
},
},
],
},
};
await expect(
response.parse(
`Here's the incomplete array output:
\`\`\`json
${JSON.stringify(incompleteArrayOutput)}
\`\`\`
`,
undefined,
(e) => e,
),
).rejects.toThrow('Required');
});
it('should raise schema fit error when passing in empty object', async () => {
const jsonExample = `{
"user": {
"name": "Alice",
"email": "alice@example.com",
"profile": {
"age": 30,
"city": "New York"
}
},
"tags": ["work", "important"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
// @ts-expect-error 2345
await expect(response.parse({})).rejects.toThrow(
"Model output doesn't fit required format",
);
});
});
describe('manual schema mode', () => {
it('should work with manually defined schema in version 1.3', async () => {
const inputSchema = `{
"type": "object",
"properties": {
"result": {
"type": "object",
"properties": {
"status": { "type": "string" },
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "number" },
"value": { "type": "string" }
},
"required": ["id", "value"]
}
}
},
"required": ["status", "data"]
}
},
"required": ["result"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(inputSchema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const outputObject = {
output: {
result: {
status: 'success',
data: [
{ id: 1, value: 'first' },
{ id: 2, value: 'second' },
],
},
},
};
const parsersOutput = await response.parse(`Here's the result:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(outputObject);
});
});
describe('complex nested structures', () => {
it('should handle deeply nested objects with required fields', async () => {
const jsonExample = `{
"company": {
"name": "TechCorp",
"departments": [
{
"name": "Engineering",
"teams": [
{
"name": "Backend",
"members": [
{
"id": 1,
"name": "Alice",
"skills": ["Python", "Docker"]
}
]
}
]
}
]
}
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
const complexOutput = {
output: {
company: {
name: 'StartupCorp',
departments: [
{
name: 'Product',
teams: [
{
name: 'Frontend',
members: [
{
id: 2,
name: 'Bob',
skills: ['React', 'TypeScript'],
},
{
id: 3,
name: 'Carol',
skills: ['Vue', 'CSS'],
},
],
},
],
},
],
},
},
};
const parsersOutput = await response.parse(`Here's the complex company data:
\`\`\`json
${JSON.stringify(complexOutput)}
\`\`\`
`);
expect(parsersOutput).toEqual(complexOutput);
});
});
});
});
describe('Auto-Fix', () => {
const model: BaseLanguageModel = jest.fn() as unknown as BaseLanguageModel;
beforeEach(() => {
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter.calledWith('jsonSchemaExample', 0).mockReturnValueOnce(`{
"user": {
"name": "Alice"
}
}`);
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.2 }));
thisArg.getInputConnectionData
.calledWith(NodeConnectionTypes.AiLanguageModel, 0)
.mockResolvedValueOnce(model);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('Configuration', () => {
it('should use default prompt when none specified', async () => {
thisArg.getNodeParameter.calledWith('autoFix', 0, false).mockReturnValueOnce(true);
thisArg.getNodeParameter
.calledWith('prompt', 0, NAIVE_FIX_PROMPT)
.mockReturnValueOnce(NAIVE_FIX_PROMPT);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
expect(response).toBeDefined();
});
it('should use custom prompt if one is provided', async () => {
thisArg.getNodeParameter.calledWith('autoFix', 0, false).mockReturnValueOnce(true);
thisArg.getNodeParameter
.calledWith('prompt', 0, NAIVE_FIX_PROMPT)
.mockReturnValueOnce(
'Some prompt with "{error}", "{instructions}", and "{completion}" placeholders',
);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
expect(response).toBeDefined();
});
it('should throw error when prompt template does not contain {error} placeholder', async () => {
thisArg.getNodeParameter.calledWith('autoFix', 0, false).mockReturnValueOnce(true);
thisArg.getNodeParameter
.calledWith('prompt', 0, NAIVE_FIX_PROMPT)
.mockReturnValueOnce('Invalid prompt without error placeholder');
await expect(outputParser.supplyData.call(thisArg, 0)).rejects.toThrow(
new NodeOperationError(
thisArg.getNode(),
'Auto-fixing parser prompt has to contain {error} placeholder',
),
);
});
it('should throw error when prompt template is empty', async () => {
thisArg.getNodeParameter.calledWith('autoFix', 0, false).mockReturnValueOnce(true);
thisArg.getNodeParameter.calledWith('prompt', 0, NAIVE_FIX_PROMPT).mockReturnValueOnce('');
await expect(outputParser.supplyData.call(thisArg, 0)).rejects.toThrow(
new NodeOperationError(
thisArg.getNode(),
'Auto-fixing parser prompt has to contain {error} placeholder',
),
);
});
});
describe('Parsing', () => {
let mockStructuredOutputParser: MockProxy<N8nStructuredOutputParser>;
beforeEach(() => {
mockStructuredOutputParser = mock<N8nStructuredOutputParser>();
jest
.spyOn(N8nStructuredOutputParser, 'fromZodJsonSchema')
.mockResolvedValue(mockStructuredOutputParser);
thisArg.getNodeParameter.calledWith('autoFix', 0, false).mockReturnValueOnce(true);
thisArg.getNodeParameter
.calledWith('prompt', 0, NAIVE_FIX_PROMPT)
.mockReturnValueOnce(NAIVE_FIX_PROMPT);
});
afterEach(() => {
jest.clearAllMocks();
});
function getMockedRetryChain(output: string) {
return jest.fn().mockReturnValue({
invoke: jest.fn().mockResolvedValue({
content: output,
}),
});
}
it('should successfully parse valid output without needing to fix it', async () => {
const validOutput = { name: 'Alice', age: 25 };
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
const result = await response.parse('{"name": "Alice", "age": 25}');
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse.mock.calls).toHaveLength(1);
});
it('should not retry on non-OutputParserException errors', async () => {
const error = new Error('Some other error');
mockStructuredOutputParser.parse.mockRejectedValueOnce(error);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
await expect(response.parse('Invalid JSON string')).rejects.toThrow(error);
expect(mockStructuredOutputParser.parse.mock.calls).toHaveLength(1);
});
it('should retry on OutputParserException and succeed', async () => {
const validOutput = { name: 'Bob', age: 28 };
mockStructuredOutputParser.parse
.mockRejectedValueOnce(new OutputParserException('Invalid JSON'))
.mockResolvedValueOnce(validOutput);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain(JSON.stringify(validOutput));
const result = await response.parse('Invalid JSON string');
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse.mock.calls).toHaveLength(2);
});
it('should handle failed retry attempt', async () => {
mockStructuredOutputParser.parse
.mockRejectedValueOnce(new OutputParserException('Invalid JSON'))
.mockRejectedValueOnce(new Error('Still invalid JSON'));
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain('Still not valid JSON');
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Still invalid JSON');
expect(mockStructuredOutputParser.parse.mock.calls).toHaveLength(2);
});
it('should throw non-OutputParserException errors immediately without retry', async () => {
const customError = new Error('Database connection error');
const retryChainSpy = jest.fn();
mockStructuredOutputParser.parse.mockRejectedValueOnce(customError);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = retryChainSpy;
await expect(response.parse('Some input')).rejects.toThrow('Database connection error');
expect(mockStructuredOutputParser.parse.mock.calls).toHaveLength(1);
expect(retryChainSpy).not.toHaveBeenCalled();
});
});
});
});