Skip to content

The agent engineering platform.

core: Handle unterminated escape character when parsing partial JSON (#29065)

Bruno Alvisio3eaf561561b3
diff --git a/libs/core/langchain_core/utils/json.py b/libs/core/langchain_core/utils/json.py
index 8aedfaf339..472ef94b7a 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 108b9f2a9c..4198a46714 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"}'),
]