diff --git a/packages/core/src/OutputParser.ts b/packages/core/src/OutputParser.ts index 1498f3f00d9cf38f706e2ce86f4c9c0f516ee508..4f9efc17cbf81277b63a824770a96955b9bb5737 100644 --- a/packages/core/src/OutputParser.ts +++ b/packages/core/src/OutputParser.ts @@ -56,11 +56,15 @@ class OutputParserError extends Error { function parseJsonMarkdown(text: string) { text = text.trim(); - //This code is more general than the previous version, and should be faster. const beginIndex = text.indexOf("["); const endIndex = text.lastIndexOf("]"); const jsonText = text.substring(beginIndex, endIndex + 1); try { + //Single JSON object case. + if(beginIndex === -1 || endIndex === -1){ + return [JSON.parse(text)]; + } + //Multiple JSON object case. return JSON.parse(jsonText); } catch (e) { throw new OutputParserError("Not a json markdown", { output: text }); diff --git a/packages/core/src/tests/OutputParser.test.ts b/packages/core/src/tests/OutputParser.test.ts index e11fc6aee19d605cb8ba9d75dd9689f943a5bec9..6ec61d4dc518fe27af06f9261da0e8b287329376 100644 --- a/packages/core/src/tests/OutputParser.test.ts +++ b/packages/core/src/tests/OutputParser.test.ts @@ -49,4 +49,42 @@ ${data_str} expect(parser.parse(JSON.stringify(data))).toEqual(real_answer); }); + + test("parses null single response", () => { + const parser = new SubQuestionOutputParser(); + const data_str = + "[\n" + + " {\n" + + ` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` + + ' "toolName": ""\n' + + " }\n" + + "]"; + const data = [ + { + subQuestion: + "Sorry, I don't have any relevant information to answer your question", + toolName: "", + }, + ]; + const real_answer = { parsedOutput: data, rawOutput: data_str }; + expect(parser.parse(data_str)).toEqual(real_answer); + }); + + test("Single JSON object case", () => { + const parser = new SubQuestionOutputParser(); + const data_str = + " {\n" + + ` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` + + ' "toolName": ""\n' + + " }\n"; + const data = [ + { + subQuestion: + "Sorry, I don't have any relevant information to answer your question", + toolName: "", + }, + ]; + const real_answer = { parsedOutput: data, rawOutput: data_str }; + expect(parser.parse(data_str)).toEqual(real_answer); + }); });