Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading