From 01dca1e438f577622dc8b36f1e20c5cc2b8644d9 Mon Sep 17 00:00:00 2001 From: Manuel Saelices Date: Fri, 7 Jul 2023 20:49:37 +0200 Subject: [PATCH] Add context to an output parsing error on Pydantic schema to improve exception handling (#7344) ## Changes - [X] Fill the `llm_output` param when there is an output parsing error in a Pydantic schema so that we can get the original text that failed to parse when handling the exception ## Background With this change, we could do something like this: ``` output_parser = PydanticOutputParser(pydantic_object=pydantic_obj) chain = ConversationChain(..., output_parser=output_parser) try: response: PydanticSchema = chain.predict(input=input) except OutputParserException as exc: logger.error( 'OutputParserException while parsing chatbot response: %s', exc.llm_output, ) ``` --------- Co-authored-by: Bagatur --- langchain/output_parsers/pydantic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain/output_parsers/pydantic.py b/langchain/output_parsers/pydantic.py index 52619dd493..eacee30e11 100644 --- a/langchain/output_parsers/pydantic.py +++ b/langchain/output_parsers/pydantic.py @@ -28,7 +28,7 @@ class PydanticOutputParser(BaseOutputParser[T]): except (json.JSONDecodeError, ValidationError) as e: name = self.pydantic_object.__name__ msg = f"Failed to parse {name} from completion {text}. Got: {e}" - raise OutputParserException(msg) + raise OutputParserException(msg, llm_output=text) def get_format_instructions(self) -> str: schema = self.pydantic_object.schema()