feat(Structured Output Parser Node): Refactor Output Parsers and Improve Error Handling (#11148)

This commit is contained in:
oleg
2024-10-22 10:46:58 +02:00
committed by GitHub
parent 4dde772814
commit 45274f2e7f
20 changed files with 1061 additions and 383 deletions

View File

@@ -1,15 +1,11 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { OutputFixingParser } from 'langchain/output_parsers';
import type { BaseOutputParser } from '@langchain/core/output_parsers';
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { logWrapper } from '../../../utils/logWrapper';
import { NodeConnectionType } from 'n8n-workflow';
import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow';
import {
N8nOutputFixingParser,
type N8nStructuredOutputParser,
} from '../../../utils/output_parsers/N8nOutputParser';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class OutputParserAutofixing implements INodeType {
@@ -75,12 +71,12 @@ export class OutputParserAutofixing implements INodeType {
const outputParser = (await this.getInputConnectionData(
NodeConnectionType.AiOutputParser,
itemIndex,
)) as BaseOutputParser;
)) as N8nStructuredOutputParser;
const parser = OutputFixingParser.fromLLM(model, outputParser);
const parser = new N8nOutputFixingParser(this, model, outputParser);
return {
response: logWrapper(parser, this),
response: parser,
};
}
}

View File

@@ -0,0 +1,120 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import { normalizeItems } from 'n8n-core';
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
import { ApplicationError, NodeConnectionType } from 'n8n-workflow';
import { N8nOutputFixingParser } from '../../../../utils/output_parsers/N8nOutputParser';
import type { N8nStructuredOutputParser } from '../../../../utils/output_parsers/N8nOutputParser';
import { OutputParserAutofixing } from '../OutputParserAutofixing.node';
describe('OutputParserAutofixing', () => {
let outputParser: OutputParserAutofixing;
let thisArg: MockProxy<IExecuteFunctions>;
let mockModel: MockProxy<BaseLanguageModel>;
let mockStructuredOutputParser: MockProxy<N8nStructuredOutputParser>;
beforeEach(() => {
outputParser = new OutputParserAutofixing();
thisArg = mock<IExecuteFunctions>({
helpers: { normalizeItems },
});
mockModel = mock<BaseLanguageModel>();
mockStructuredOutputParser = mock<N8nStructuredOutputParser>();
thisArg.getWorkflowDataProxy.mockReturnValue(mock<IWorkflowDataProxyData>({ $input: mock() }));
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
thisArg.getInputConnectionData.mockImplementation(async (type: NodeConnectionType) => {
if (type === NodeConnectionType.AiLanguageModel) return mockModel;
if (type === NodeConnectionType.AiOutputParser) return mockStructuredOutputParser;
throw new ApplicationError('Unexpected connection type');
});
});
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;
};
// Ensure the response contains the output-fixing parser
expect(response).toBeDefined();
expect(response).toBeInstanceOf(N8nOutputFixingParser);
const result = await response.parse('{"name": "Alice", "age": 25}');
// Validate that the parser succeeds without retry
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(1); // Only one call to parse
});
it('should throw an error when both structured parser and fixing parser fail', async () => {
mockStructuredOutputParser.parse
.mockRejectedValueOnce(new Error('Invalid JSON')) // First attempt fails
.mockRejectedValueOnce(new Error('Fixing attempt failed')); // Second attempt fails
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain('{}');
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Fixing attempt failed');
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2);
});
it('should reject on the first attempt and succeed on retry with the parsed content', async () => {
const validOutput = { name: 'Bob', age: 28 };
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain(JSON.stringify(validOutput));
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
const result = await response.parse('Invalid JSON string');
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second succeeds
});
it('should handle non-JSON formatted response from fixing parser', async () => {
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain('This is not JSON');
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Unexpected token'));
// Expect the structured parser to throw an error on invalid JSON from retry
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Unexpected token');
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second tries and fails
});
});

View File

@@ -1,51 +0,0 @@
import { BaseOutputParser, OutputParserException } from '@langchain/core/output_parsers';
export class ItemListOutputParser extends BaseOutputParser<string[]> {
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
private numberOfItems: number | undefined;
private separator: string;
constructor(options: { numberOfItems?: number; separator?: string }) {
super();
if (options.numberOfItems && options.numberOfItems > 0) {
this.numberOfItems = options.numberOfItems;
}
this.separator = options.separator ?? '\\n';
if (this.separator === '\\n') {
this.separator = '\n';
}
}
async parse(text: string): Promise<string[]> {
const response = text
.split(this.separator)
.map((item) => item.trim())
.filter((item) => item);
if (this.numberOfItems && response.length < this.numberOfItems) {
// Only error if to few items got returned, if there are to many we can autofix it
throw new OutputParserException(
`Wrong number of items returned. Expected ${this.numberOfItems} items but got ${response.length} items instead.`,
);
}
return response.slice(0, this.numberOfItems);
}
getFormatInstructions(): string {
const instructions = `Your response should be a list of ${
this.numberOfItems ? this.numberOfItems + ' ' : ''
}items separated by`;
const numberOfExamples = this.numberOfItems ?? 3;
const examples: string[] = [];
for (let i = 1; i <= numberOfExamples; i++) {
examples.push(`item${i}`);
}
return `${instructions} "${this.separator}" (for example: "${examples.join(this.separator)}")`;
}
}

View File

@@ -6,9 +6,9 @@ import {
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { logWrapper } from '../../../utils/logWrapper';
import { N8nItemListOutputParser } from '../../../utils/output_parsers/N8nItemListOutputParser';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { ItemListOutputParser } from './ItemListOutputParser';
export class OutputParserItemList implements INodeType {
description: INodeTypeDescription = {
@@ -86,10 +86,10 @@ export class OutputParserItemList implements INodeType {
separator?: string;
};
const parser = new ItemListOutputParser(options);
const parser = new N8nItemListOutputParser(options);
return {
response: logWrapper(parser, this),
response: parser,
};
}
}

View File

@@ -0,0 +1,123 @@
import { mock } from 'jest-mock-extended';
import { normalizeItems } from 'n8n-core';
import {
ApplicationError,
type IExecuteFunctions,
type IWorkflowDataProxyData,
} from 'n8n-workflow';
import { N8nItemListOutputParser } from '../../../../utils/output_parsers/N8nItemListOutputParser';
import { OutputParserItemList } from '../OutputParserItemList.node';
describe('OutputParserItemList', () => {
let outputParser: OutputParserItemList;
const thisArg = mock<IExecuteFunctions>({
helpers: { normalizeItems },
});
const workflowDataProxy = mock<IWorkflowDataProxyData>({ $input: mock() });
beforeEach(() => {
outputParser = new OutputParserItemList();
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
thisArg.getNodeParameter.mockReset();
});
describe('supplyData', () => {
it('should create a parser with default options', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return {};
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
expect(response).toBeInstanceOf(N8nItemListOutputParser);
});
it('should create a parser with custom number of items', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return { numberOfItems: 5 };
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
expect(response).toBeInstanceOf(N8nItemListOutputParser);
expect((response as any).numberOfItems).toBe(5);
});
it('should create a parser with custom separator', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return { separator: ',' };
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
expect(response).toBeInstanceOf(N8nItemListOutputParser);
expect((response as any).separator).toBe(',');
});
});
describe('parse', () => {
it('should parse a list with default separator', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return {};
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
const result = await (response as N8nItemListOutputParser).parse('item1\nitem2\nitem3');
expect(result).toEqual(['item1', 'item2', 'item3']);
});
it('should parse a list with custom separator', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return { separator: ',' };
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
const result = await (response as N8nItemListOutputParser).parse('item1,item2,item3');
expect(result).toEqual(['item1', 'item2', 'item3']);
});
it('should limit the number of items returned', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return { numberOfItems: 2 };
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
const result = await (response as N8nItemListOutputParser).parse(
'item1\nitem2\nitem3\nitem4',
);
expect(result).toEqual(['item1', 'item2']);
});
it('should throw an error if not enough items are returned', async () => {
thisArg.getNodeParameter.mockImplementation((parameterName) => {
if (parameterName === 'options') {
return { numberOfItems: 5 };
}
throw new ApplicationError('Not implemented');
});
const { response } = await outputParser.supplyData.call(thisArg, 0);
await expect(
(response as N8nItemListOutputParser).parse('item1\nitem2\nitem3'),
).rejects.toThrow('Wrong number of items returned');
});
});
});

View File

@@ -1,8 +1,4 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import { OutputParserException } from '@langchain/core/output_parsers';
import type { JSONSchema7 } from 'json-schema';
import { StructuredOutputParser } from 'langchain/output_parsers';
import get from 'lodash/get';
import {
jsonParse,
type IExecuteFunctions,
@@ -12,77 +8,17 @@ import {
NodeOperationError,
NodeConnectionType,
} from 'n8n-workflow';
import { z } from 'zod';
import type { z } from 'zod';
import {
inputSchemaField,
jsonSchemaExampleField,
schemaTypeField,
} from '../../../utils/descriptions';
import { logWrapper } from '../../../utils/logWrapper';
import { N8nStructuredOutputParser } from '../../../utils/output_parsers/N8nOutputParser';
import { convertJsonSchemaToZod, generateSchema } from '../../../utils/schemaParsing';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
const STRUCTURED_OUTPUT_KEY = '__structured__output';
const STRUCTURED_OUTPUT_OBJECT_KEY = '__structured__output__object';
const STRUCTURED_OUTPUT_ARRAY_KEY = '__structured__output__array';
export class N8nStructuredOutputParser<T extends z.ZodTypeAny> extends StructuredOutputParser<T> {
async parse(text: string): Promise<z.infer<T>> {
try {
const parsed = (await super.parse(text)) as object;
return (
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_OBJECT_KEY]) ??
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_ARRAY_KEY]) ??
get(parsed, STRUCTURED_OUTPUT_KEY) ??
parsed
);
} catch (e) {
// eslint-disable-next-line n8n-nodes-base/node-execute-block-wrong-error-thrown
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
}
}
static async fromZedSchema(
zodSchema: z.ZodSchema<object>,
nodeVersion: number,
): Promise<StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>> {
let returnSchema: z.ZodSchema<object>;
if (nodeVersion === 1) {
returnSchema = z.object({
[STRUCTURED_OUTPUT_KEY]: z
.object({
[STRUCTURED_OUTPUT_OBJECT_KEY]: zodSchema.optional(),
[STRUCTURED_OUTPUT_ARRAY_KEY]: z.array(zodSchema).optional(),
})
.describe(
`Wrapper around the output data. It can only contain ${STRUCTURED_OUTPUT_OBJECT_KEY} or ${STRUCTURED_OUTPUT_ARRAY_KEY} but never both.`,
)
.refine(
(data) => {
// Validate that one and only one of the properties exists
return (
Boolean(data[STRUCTURED_OUTPUT_OBJECT_KEY]) !==
Boolean(data[STRUCTURED_OUTPUT_ARRAY_KEY])
);
},
{
message:
'One and only one of __structured__output__object and __structured__output__array should be present.',
path: [STRUCTURED_OUTPUT_KEY],
},
),
});
} else {
returnSchema = z.object({
output: zodSchema.optional(),
});
}
return N8nStructuredOutputParser.fromZodSchema(returnSchema);
}
}
export class OutputParserStructured implements INodeType {
description: INodeTypeDescription = {
displayName: 'Structured Output Parser',
@@ -205,9 +141,13 @@ export class OutputParserStructured implements INodeType {
const zodSchema = convertJsonSchemaToZod<z.ZodSchema<object>>(jsonSchema);
const nodeVersion = this.getNode().typeVersion;
try {
const parser = await N8nStructuredOutputParser.fromZedSchema(zodSchema, nodeVersion);
const parser = await N8nStructuredOutputParser.fromZodJsonSchema(
zodSchema,
nodeVersion,
this,
);
return {
response: logWrapper(parser, this),
response: parser,
};
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Error during parsing of JSON Schema.');

View File

@@ -1,8 +1,13 @@
import type { IExecuteFunctions, INode, IWorkflowDataProxyData } from 'n8n-workflow';
import { mock } from 'jest-mock-extended';
import { normalizeItems } from 'n8n-core';
import type { z } from 'zod';
import type { StructuredOutputParser } from 'langchain/output_parsers';
import {
jsonParse,
type IExecuteFunctions,
type INode,
type IWorkflowDataProxyData,
} from 'n8n-workflow';
import type { N8nStructuredOutputParser } from '../../../../utils/output_parsers/N8nStructuredOutputParser';
import { OutputParserStructured } from '../OutputParserStructured.node';
describe('OutputParserStructured', () => {
@@ -11,139 +16,451 @@ describe('OutputParserStructured', () => {
helpers: { normalizeItems },
});
const workflowDataProxy = mock<IWorkflowDataProxyData>({ $input: mock() });
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.1 }));
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
beforeEach(() => {
outputParser = new OutputParserStructured();
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
});
describe('supplyData', () => {
it('should parse a valid JSON schema', async () => {
const schema = `{
"type": "object",
"properties": {
"name": {
"type": "string"
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" }
},
"age": {
"type": "number"
}
},
"required": ["name", "age"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
};
const outputObject = { output: { name: 'Mac', age: 27 } };
const parsersOutput = await response.parse(`Here's the 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('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
};
const outputObject = { output: { name: 'Mac' } };
await expect(
response.parse(`Here's the output!
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`),
).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('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
};
const outputObject = { output: { name: 'Mac', age: '27' } };
await expect(
response.parse(`Here's the output!
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`),
).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": ["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'],
},
"required": ["name", "age"]
}
}
},
"required": ["myArr"]
}`;
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
};
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)}
\`\`\`
`);
},
timestamp: '2023-04-01T12:00:00Z',
},
};
const parsersOutput = await response.parse(`Here's the complex output:
\`\`\`json
${JSON.stringify(outputObject)}
\`\`\`
`);
expect(parsersOutput).toEqual(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);
});
});
});
});