diff --git a/libs/core/langchain_core/utils/json.py b/libs/core/langchain_core/utils/json.py index 8aedfaf339b..472ef94b7a3 100644 --- a/libs/core/langchain_core/utils/json.py +++ b/libs/core/langchain_core/utils/json.py @@ -94,6 +94,8 @@ def parse_partial_json(s: str, *, strict: bool = False) -> Any: # If we're still inside a string at the end of processing, # we need to close the string. if is_inside_string: + if escaped: # Remoe unterminated escape character + new_chars.pop() new_chars.append('"') # Reverse the stack to get the closing characters. diff --git a/libs/core/tests/unit_tests/output_parsers/test_json.py b/libs/core/tests/unit_tests/output_parsers/test_json.py index 108b9f2a9cf..4198a467143 100644 --- a/libs/core/tests/unit_tests/output_parsers/test_json.py +++ b/libs/core/tests/unit_tests/output_parsers/test_json.py @@ -242,6 +242,7 @@ TEST_CASES_PARTIAL = [ ('{"foo": "bar", "bar":', '{"foo": "bar"}'), ('{"foo": "bar", "bar"', '{"foo": "bar"}'), ('{"foo": "bar", ', '{"foo": "bar"}'), + ('{"foo":"bar\\', '{"foo": "bar"}'), ]