From f5e2f701156fd7fca1f98b3227b4bba54d5a7bd9 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Sat, 13 May 2023 21:46:33 -0700 Subject: [PATCH] Harrison/json new line (#4646) Co-authored-by: David Chen --- langchain/output_parsers/pydantic.py | 2 +- tests/unit_tests/output_parsers/test_pydantic_parser.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/langchain/output_parsers/pydantic.py b/langchain/output_parsers/pydantic.py index 4b0fa53d54..52619dd493 100644 --- a/langchain/output_parsers/pydantic.py +++ b/langchain/output_parsers/pydantic.py @@ -22,7 +22,7 @@ class PydanticOutputParser(BaseOutputParser[T]): json_str = "" if match: json_str = match.group() - json_object = json.loads(json_str) + json_object = json.loads(json_str, strict=False) return self.pydantic_object.parse_obj(json_object) except (json.JSONDecodeError, ValidationError) as e: diff --git a/tests/unit_tests/output_parsers/test_pydantic_parser.py b/tests/unit_tests/output_parsers/test_pydantic_parser.py index aefd3c0a79..8c12049960 100644 --- a/tests/unit_tests/output_parsers/test_pydantic_parser.py +++ b/tests/unit_tests/output_parsers/test_pydantic_parser.py @@ -21,6 +21,7 @@ class TestModel(BaseModel): additional_fields: Optional[str] = Field( description="Additional fields", default=None ) + for_new_lines: str = Field(description="To be used to test newlines") # Prevent pytest from trying to run tests on TestModel @@ -30,7 +31,8 @@ TestModel.__test__ = False # type: ignore[attr-defined] DEF_RESULT = """{ "action": "Update", "action_input": "The PydanticOutputParser class is powerful", - "additional_fields": null + "additional_fields": null, + "for_new_lines": "not_escape_newline:\n escape_newline: \\n" }""" # action 'update' with a lowercase 'u' to test schema validation failure. @@ -44,6 +46,7 @@ DEF_EXPECTED_RESULT = TestModel( action=Actions.UPDATE, action_input="The PydanticOutputParser class is powerful", additional_fields=None, + for_new_lines="not_escape_newline:\n escape_newline: \n", )