From d420b7146bb61095229dda5bae5cd11a4c2b7f23 Mon Sep 17 00:00:00 2001 From: Lan <913088741@qq.com> Date: Sun, 17 Nov 2024 14:27:53 +0800 Subject: [PATCH] chore(langchain): add throw err for JsonOutputFunctionsParser.parse (#7204) Co-authored-by: jacoblee93 --- langchain/src/output_parsers/openai_functions.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/langchain/src/output_parsers/openai_functions.ts b/langchain/src/output_parsers/openai_functions.ts index 88614a896d3d..d20c5690f8e9 100644 --- a/langchain/src/output_parsers/openai_functions.ts +++ b/langchain/src/output_parsers/openai_functions.ts @@ -9,6 +9,7 @@ import { BaseCumulativeTransformOutputParser, type BaseCumulativeTransformOutputParserInput, BaseLLMOutputParser, + OutputParserException, } from "@langchain/core/output_parsers"; import { parsePartialJson } from "@langchain/core/output_parsers"; import { Optional } from "../types/type-utils.js"; @@ -154,12 +155,18 @@ export class JsonOutputFunctionsParser< } async parse(text: string): Promise { - const parsedResult = JSON.parse(text); - if (this.argsOnly) { + try { + const parsedResult = JSON.parse(text); + if (this.argsOnly) { + return parsedResult; + } + parsedResult.arguments = JSON.parse(parsedResult.arguments); return parsedResult; + } catch (e) { + throw new OutputParserException( + `Failed to parse. Text: "${text}". Error: ${e}` + ); } - parsedResult.arguments = JSON.parse(parsedResult.arguments); - return parsedResult; } getFormatInstructions(): string {