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

Conversation

Yash-Tobre
Copy link

Description:
Raise a more descriptive OutputParserException when JSON parsing results in a non-dict type. This improves debugging and aligns behavior with expectations when using expected_keys.

Issue:
Fixes #32233

Twitter handle:
@yashvtobre

Testing:

  • Ran make format and make lint from the root directory; both passed cleanly.
  • Attempted make test but no such target exists in the root Makefile.
  • Executed tests directly via pytest targeting the relevant test file, confirming all tests pass except for unrelated async test failures outside the scope of this change.

Notes:

  • No additional dependencies introduced.
  • Changes are backward compatible and isolated within the output parser module.

@Yash-Tobre Yash-Tobre requested a review from eyurtsev as a code owner July 25, 2025 01:01
Copy link

vercel bot commented Jul 25, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain ⬜️ Ignored Preview Aug 12, 2025 8:36pm

Copy link

codspeed-hq bot commented Jul 25, 2025

CodSpeed WallTime Performance Report

Merging #32236 will not alter performance

Comparing Yash-Tobre:fix/output-parser-dict-error (2100f05) with master (be83ce7)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

Copy link

codspeed-hq bot commented Jul 25, 2025

CodSpeed Instrumentation Performance Report

Merging #32236 will not alter performance

Comparing Yash-Tobre:fix/output-parser-dict-error (2100f05) with master (be83ce7)

Summary

✅ 14 untouched benchmarks

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves error handling in the JSON output parser by adding explicit validation to ensure JSON parsing results in a dictionary object. Previously, the parser would only check for valid JSON syntax but wouldn't validate the type, potentially causing issues when non-dict JSON (like numbers or strings) was parsed.

  • Adds type validation to raise OutputParserException when parsed JSON is not a dictionary
  • Includes a descriptive error message showing the actual type and content received
  • Adds comprehensive test coverage for the new validation behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
libs/core/langchain_core/utils/json.py Adds type validation after JSON parsing to ensure result is a dict
libs/core/tests/unit_tests/output_parsers/test_json.py Adds test case and imports for the new validation behavior

@mdrxy mdrxy changed the title fix(json): raise OutputParserException for non-dict JSON outputs fix(core): raise OutputParserException for non-dict JSON outputs Jul 28, 2025
@mdrxy mdrxy added the core Related to the package `langchain-core` label Jul 28, 2025
@mdrxy
Copy link
Collaborator

mdrxy commented Aug 11, 2025

@Yash-Tobre please resolve CI failures 🙏

@Yash-Tobre
Copy link
Author

@Yash-Tobre please resolve CI failures 🙏

On it!

@mdrxy mdrxy changed the title fix(core): raise OutputParserException for non-dict JSON outputs fix(core): raise OutputParserException for non-dict JSON outputs Aug 12, 2025
@mdrxy
Copy link
Collaborator

mdrxy commented Aug 12, 2025

@Yash-Tobre looks like tests are still failing

Copy link

vercel bot commented Aug 12, 2025

Deployment failed with the following error:

The provided GitHub repository does not contain the requested branch or commit reference. Please ensure the repository is not empty.

@Yash-Tobre
Copy link
Author

@Yash-Tobre looks like tests are still failing

There are a few little formatting issues- figuring them out. Hopefully should be done within the next 24 hours.

@mdrxy
Copy link
Collaborator

mdrxy commented Aug 12, 2025

@Yash-Tobre Have you tried running make lint and make format? That should resolve

@Yash-Tobre
Copy link
Author

@Yash-Tobre Have you tried running make lint and make format? That should resolve

Yes - on my end, it works perfectly fine; with all checks passed. Trying to figure it out.

@Yash-Tobre
Copy link
Author

@Yash-Tobre Have you tried running make lint and make format? That should resolve

I think it is resolved now - thank you for your patience.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here?

@@ -5,12 +5,17 @@
import pytest
from pydantic import BaseModel, Field

# Removed incorrect import of parse_and_check_json_markdown
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this comment here?

@@ -110,7 +111,8 @@ async def consumer() -> AsyncIterator[dict]:
# To verify that the producer and consumer are running in parallel, we
# expect the delta_time to be smaller than the sleep delay in the producer
# * # of items = 30 ms
assert math.isclose(delta_time, 0, abs_tol=0.020) is True, (
tolerance = 0.03 if sys.version_info[:2] in [(3, 9), (3, 11)] else 0.02
assert math.isclose(delta_time, 0, abs_tol=tolerance), (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the PR, no need to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the package `langchain-core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

StructuredOutputParser doesn't fail if json is invalid
2 participants