mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-16 08:06:14 +00:00
core[patch]: Fix jsonOutputParser fails if a json value contains ``` inside it. (#19717)
- **Issue:** fix #19646 - @baskaryan, @eyurtsev PTAL
This commit is contained in:
parent
f7042321f1
commit
0571f886d1
@ -137,16 +137,24 @@ def parse_json_markdown(
|
|||||||
Returns:
|
Returns:
|
||||||
The parsed JSON object as a Python dictionary.
|
The parsed JSON object as a Python dictionary.
|
||||||
"""
|
"""
|
||||||
# Try to find JSON string within triple backticks
|
try:
|
||||||
match = re.search(r"```(json)?(.*)", json_string, re.DOTALL)
|
return _parse_json(json_string, parser=parser)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# 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 no match found, assume the entire string is a JSON string
|
||||||
if match is None:
|
if match is None:
|
||||||
json_str = json_string
|
json_str = json_string
|
||||||
else:
|
else:
|
||||||
# If match found, use the content within the backticks
|
# If match found, use the content within the backticks
|
||||||
json_str = match.group(2)
|
json_str = match.group(2)
|
||||||
|
return _parse_json(json_str, parser=parser)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_json(
|
||||||
|
json_str: str, *, parser: Callable[[str], Any] = parse_partial_json
|
||||||
|
) -> dict:
|
||||||
# Strip whitespace and newlines from the start and end
|
# Strip whitespace and newlines from the start and end
|
||||||
json_str = json_str.strip().strip("`")
|
json_str = json_str.strip().strip("`")
|
||||||
|
|
||||||
@ -154,9 +162,7 @@ def parse_json_markdown(
|
|||||||
json_str = _custom_parser(json_str)
|
json_str = _custom_parser(json_str)
|
||||||
|
|
||||||
# Parse the JSON string into a Python dictionary
|
# Parse the JSON string into a Python dictionary
|
||||||
parsed = parser(json_str)
|
return parser(json_str)
|
||||||
|
|
||||||
return parsed
|
|
||||||
|
|
||||||
|
|
||||||
def parse_and_check_json_markdown(text: str, expected_keys: List[str]) -> dict:
|
def parse_and_check_json_markdown(text: str, expected_keys: List[str]) -> dict:
|
||||||
|
@ -69,6 +69,10 @@ JSON_WITH_MARKDOWN_CODE_BLOCK = """```json
|
|||||||
}
|
}
|
||||||
```"""
|
```"""
|
||||||
|
|
||||||
|
JSON_WITH_PART_MARKDOWN_CODE_BLOCK = """
|
||||||
|
{\"valid_json\": "hey ```print(hello world!)``` hey"}
|
||||||
|
"""
|
||||||
|
|
||||||
JSON_WITH_MARKDOWN_CODE_BLOCK_AND_NEWLINES = """```json
|
JSON_WITH_MARKDOWN_CODE_BLOCK_AND_NEWLINES = """```json
|
||||||
{
|
{
|
||||||
"action": "Final Answer",
|
"action": "Final Answer",
|
||||||
@ -191,6 +195,11 @@ def test_parse_json_with_code_blocks() -> None:
|
|||||||
assert parsed == {"foo": "```bar```"}
|
assert parsed == {"foo": "```bar```"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_json_with_part_code_blocks() -> None:
|
||||||
|
parsed = parse_json_markdown(JSON_WITH_PART_MARKDOWN_CODE_BLOCK)
|
||||||
|
assert parsed == {"valid_json": "hey ```print(hello world!)``` hey"}
|
||||||
|
|
||||||
|
|
||||||
def test_parse_json_with_code_blocks_and_newlines() -> None:
|
def test_parse_json_with_code_blocks_and_newlines() -> None:
|
||||||
parsed = parse_json_markdown(JSON_WITH_MARKDOWN_CODE_BLOCK_AND_NEWLINES)
|
parsed = parse_json_markdown(JSON_WITH_MARKDOWN_CODE_BLOCK_AND_NEWLINES)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user