Skip to content

fix(core): raise OutputParserException for non-dict JSON outputs #32236

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0072311
fix(json): raise OutputParserException for non-dict JSON outputs
Yash-Tobre Jul 24, 2025
57dbd3a
Update libs/core/tests/unit_tests/output_parsers/test_json.py
Yash-Tobre Jul 25, 2025
3f45685
Update libs/core/tests/unit_tests/output_parsers/test_json.py
Yash-Tobre Jul 25, 2025
1531fba
Merge branch 'master' into fix/output-parser-dict-error
Yash-Tobre Aug 6, 2025
5d3fbd4
Merge branch 'master' into fix/output-parser-dict-error
mdrxy Aug 11, 2025
5b59929
Fix lint errors and update output parser tests as requested
Yash-Tobre Aug 11, 2025
a56cdf8
Fix lint issues in test_json.py
Yash-Tobre Aug 12, 2025
5d1cdc8
Fix lint issues in test_json.py
Yash-Tobre Aug 12, 2025
2d37610
Fix lint issues in test_json.py fixing spacing issues
Yash-Tobre Aug 12, 2025
1933de1
Fix lint issues in test_json.py, part 3
Yash-Tobre Aug 12, 2025
ece236e
Merge branch 'master' into fix/output-parser-dict-error
Yash-Tobre Aug 12, 2025
92b6883
Fix lint issues in test_json.py, blank space error
Yash-Tobre Aug 12, 2025
873b3b2
adding formatting changes
Yash-Tobre Aug 12, 2025
a471947
adding formatting changes, recurring blank space error ver1.4
Yash-Tobre Aug 12, 2025
5a51e00
Merge branch 'master' into fix/output-parser-dict-error
Yash-Tobre Aug 12, 2025
c599516
Added a tolerence check for different versions.
Yash-Tobre Aug 12, 2025
4031df6
Rearranged the imports.
Yash-Tobre Aug 12, 2025
9ab7363
reformatted the assertion statement
Yash-Tobre Aug 12, 2025
f30cacf
reformatted the memory_stream_file
Yash-Tobre Aug 12, 2025
ddabc32
reformatted the imports, again using ruff
Yash-Tobre Aug 12, 2025
8143de6
reformatted the imports, again using ruff
Yash-Tobre Aug 12, 2025
bb726d9
reformatted the imports, added a blank space
Yash-Tobre Aug 12, 2025
ea7c05f
blank space modification in streamer
Yash-Tobre Aug 12, 2025
396237c
blank space mod using ruff again!
Yash-Tobre Aug 12, 2025
1bdbf55
Merge branch 'master' into fix/output-parser-dict-error
Yash-Tobre Aug 12, 2025
2100f05
Merge branch 'master' into fix/output-parser-dict-error
Yash-Tobre Aug 12, 2025
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
5 changes: 5 additions & 0 deletions libs/core/langchain_core/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
12 changes: 11 additions & 1 deletion libs/core/tests/unit_tests/output_parsers/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import pytest
from pydantic import BaseModel, Field

from langchain_core.exceptions import OutputParserException
from langchain.output_parsers.json import parse_and_check_json_markdown
from langchain.schema import OutputParserException


from langchain_core.output_parsers.json import (
SimpleJsonOutputParser,
)
Expand Down Expand Up @@ -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,
Expand Down
Loading