From 5376799a2307f03c9fdac7fc5f702749d040a360 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Sun, 9 Apr 2023 15:32:49 +0100 Subject: [PATCH] Allow recovering from JSONDecoder errors in StructuredOutputParser (#2616) --- langchain/output_parsers/structured.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/langchain/output_parsers/structured.py b/langchain/output_parsers/structured.py index 9921e7c6..c77f865a 100644 --- a/langchain/output_parsers/structured.py +++ b/langchain/output_parsers/structured.py @@ -39,7 +39,10 @@ class StructuredOutputParser(BaseOutputParser): def parse(self, text: str) -> BaseModel: json_string = text.split("```json")[1].strip().strip("```").strip() - json_obj = json.loads(json_string) + try: + json_obj = json.loads(json_string) + except json.JSONDecodeError as e: + raise OutputParserException(f"Got invalid JSON object. Error: {e}") for schema in self.response_schemas: if schema.name not in json_obj: raise OutputParserException(