Skip to content

Commit

Permalink
handle json parsing errors (#5371)
Browse files Browse the repository at this point in the history
adds tests cases, consolidates a lot of PRs
  • Loading branch information
hwchase17 authored and vowelparrot committed May 31, 2023
1 parent 8b13392 commit 6bf778c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
16 changes: 12 additions & 4 deletions langchain/output_parsers/json.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
from __future__ import annotations

import json
import re
from typing import List

from langchain.schema import OutputParserException


def parse_json_markdown(json_string: str) -> dict:
# Remove the triple backticks if present
json_string = json_string.replace("```json", "").replace("```", "")
# Try to find JSON string within triple backticks
match = re.search(r"```(json)?(.*?)```", json_string, re.DOTALL)

# If no match found, assume the entire string is a JSON string
if match is None:
json_str = json_string
else:
# If match found, use the content within the backticks
json_str = match.group(2)

# Strip whitespace and newlines from the start and end
json_string = json_string.strip()
json_str = json_str.strip()

# Parse the JSON string into a Python dictionary
parsed = json.loads(json_string)
parsed = json.loads(json_str)

return parsed

Expand Down
27 changes: 27 additions & 0 deletions tests/unit_tests/output_parsers/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@
}
"""

TEXT_BEFORE = """Thought: I need to use the search tool
Action:
```
{
"foo": "bar"
}
```"""

TEXT_AFTER = """```
{
"foo": "bar"
}
```
This should do the trick"""

TEXT_BEFORE_AND_AFTER = """Action: Testing
```
{
"foo": "bar"
}
```
This should do the trick"""

TEST_CASES = [
GOOD_JSON,
JSON_WITH_NEW_LINES,
Expand All @@ -72,6 +97,8 @@
TICKS_WITH_NEW_LINES_EVERYWHERE,
NO_TICKS,
NO_TICKS_WHITE_SPACE,
TEXT_BEFORE,
TEXT_AFTER,
]


Expand Down

0 comments on commit 6bf778c

Please sign in to comment.