Skip to content

Commit

Permalink
Handle unterminated escape character when parsing partial JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
balvisio committed Jan 7, 2025
1 parent 187131c commit 261d1c0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/core/langchain_core/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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.
Expand Down
1 change: 1 addition & 0 deletions libs/core/tests/unit_tests/output_parsers/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def test_parse_json_with_python_dict() -> None:
('{"foo": "bar", "bar":', '{"foo": "bar"}'),
('{"foo": "bar", "bar"', '{"foo": "bar"}'),
('{"foo": "bar", ', '{"foo": "bar"}'),
('{"foo":"bar\\', '{"foo": "bar"}'),
]


Expand Down

0 comments on commit 261d1c0

Please sign in to comment.