This commit is contained in:
Yash Vishwanath Tobre 2025-07-28 19:08:49 -04:00 committed by GitHub
commit 96dfb89639
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions

View File

@ -187,6 +187,11 @@ def parse_and_check_json_markdown(text: str, expected_keys: list[str]) -> dict:
except json.JSONDecodeError as e:
msg = f"Got invalid JSON object. Error: {e}"
raise OutputParserException(msg) from e
if not isinstance(json_obj, dict):
raise OutputParserException(
f"Expected JSON object (dict), but got: {type(json_obj).__name__}. Raw content: {json_obj}",
llm_output=text
)
for key in expected_keys:
if key not in json_obj:
msg = (

View File

@ -5,7 +5,10 @@ from typing import Any
import pytest
from pydantic import BaseModel, Field
# Removed incorrect import of parse_and_check_json_markdown
from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers.json import (
SimpleJsonOutputParser,
)
@ -210,6 +213,13 @@ def test_parse_json_with_code_blocks_and_newlines() -> None:
"action_input": '```bar\n<div id="1" class="value">\n\ttext\n</div>```',
}
def test_parse_non_dict_json_output():
text = "```json\n1\n```"
with pytest.raises(OutputParserException) as exc_info:
parse_and_check_json_markdown(text, expected_keys=["foo"])
assert "Expected JSON object (dict)" in str(exc_info.value)
TEST_CASES_ESCAPED_QUOTES = [
JSON_WITH_ESCAPED_DOUBLE_QUOTES_IN_NESTED_JSON,