diff --git a/packages/@n8n/nodes-langchain/nodes/output_parser/OutputParserItemList/test/OutputParserItemList.node.test.ts b/packages/@n8n/nodes-langchain/nodes/output_parser/OutputParserItemList/test/OutputParserItemList.node.test.ts index 31e96077c4..c8ac869169 100644 --- a/packages/@n8n/nodes-langchain/nodes/output_parser/OutputParserItemList/test/OutputParserItemList.node.test.ts +++ b/packages/@n8n/nodes-langchain/nodes/output_parser/OutputParserItemList/test/OutputParserItemList.node.test.ts @@ -35,6 +35,7 @@ describe('OutputParserItemList', () => { const { response } = await outputParser.supplyData.call(thisArg, 0); expect(response).toBeInstanceOf(N8nItemListOutputParser); + expect((response as any).numberOfItems).toBe(3); }); it('should create a parser with custom number of items', async () => { @@ -50,6 +51,20 @@ describe('OutputParserItemList', () => { expect((response as any).numberOfItems).toBe(5); }); + it('should create a parser with unlimited number of items', async () => { + thisArg.getNodeParameter.mockImplementation((parameterName) => { + if (parameterName === 'options') { + return { numberOfItems: -1 }; + } + + throw new ApplicationError('Not implemented'); + }); + + const { response } = await outputParser.supplyData.call(thisArg, 0); + expect(response).toBeInstanceOf(N8nItemListOutputParser); + expect((response as any).numberOfItems).toBeUndefined(); + }); + it('should create a parser with custom separator', async () => { thisArg.getNodeParameter.mockImplementation((parameterName) => { if (parameterName === 'options') { diff --git a/packages/@n8n/nodes-langchain/utils/output_parsers/N8nItemListOutputParser.ts b/packages/@n8n/nodes-langchain/utils/output_parsers/N8nItemListOutputParser.ts index f24238690b..d12f24bf0a 100644 --- a/packages/@n8n/nodes-langchain/utils/output_parsers/N8nItemListOutputParser.ts +++ b/packages/@n8n/nodes-langchain/utils/output_parsers/N8nItemListOutputParser.ts @@ -3,16 +3,21 @@ import { BaseOutputParser, OutputParserException } from '@langchain/core/output_ export class N8nItemListOutputParser extends BaseOutputParser { lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items']; - private numberOfItems: number = 3; + private numberOfItems: number | undefined; private separator: string; constructor(options: { numberOfItems?: number; separator?: string }) { super(); - if (options.numberOfItems && options.numberOfItems > 0) { - this.numberOfItems = options.numberOfItems; + + const { numberOfItems = 3, separator = '\n' } = options; + + if (numberOfItems && numberOfItems > 0) { + this.numberOfItems = numberOfItems; } - this.separator = options.separator ?? '\\n'; + + this.separator = separator; + if (this.separator === '\\n') { this.separator = '\n'; } @@ -39,7 +44,7 @@ export class N8nItemListOutputParser extends BaseOutputParser { this.numberOfItems ? this.numberOfItems + ' ' : '' }items separated by`; - const numberOfExamples = this.numberOfItems; + const numberOfExamples = this.numberOfItems ?? 3; // Default number of examples in case numberOfItems is not set const examples: string[] = []; for (let i = 1; i <= numberOfExamples; i++) {