fix: parse first complete unfenced JSON value - #42015
Open
fritz-fujii wants to merge 1 commit into
Open
Conversation
When JSON is not wrapped in code fences there is no end marker, so anchoring on the last "}" / "]" can span a *later* value. Models sometimes emit more than one JSON value in a row, and the whole slice was handed to json.loads() at once, failing with "Extra data". Decode only the first complete value with JSONDecoder.raw_decode() on the unfenced path. Fenced content keeps its existing behavior. Fixes langgenius#42006 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KmXVqNv6rrbj26ConME7LW
3 tasks
Contributor
Pyrefly Type Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
parse_json_markdown()so multiple unfenced JSON values do not get concatenated into a singlejson.loads()call.The parser now consumes the first complete unfenced JSON value while preserving the existing fenced-content and scalar behavior.
Problem
Given:
the current implementation selects the first opening brace and the last closing brace, causing both values to be passed to
json.loads()and raisingJSONDecodeError: Extra data.This surfaces in the Question Classifier node when a model emits more than one JSON value in a row.
Fix
Use
json.JSONDecoder().raw_decode()for the unfenced JSON path so parsing stops after the first complete JSON value.Existing fenced JSON behavior is intentionally preserved — in particular,
test_parse_and_check_json_markdown_multiple_blocks_fails(multiple fenced blocks still fail) is left unchanged.Tests
Added regression coverage for multiple unfenced JSON values and verified the existing JSON-in-Markdown parser tests continue to pass.
New cases: first value is an object / an array / contains nested objects / contains brackets inside string values / is followed by prose, a malformed first value still raises, and the reported
parse_and_check_json_markdownfailure mode now resolves.tests/unit_tests/libs/test_json_in_md_parser.py: 20 passed (13 existing + 7 new).Checklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint godsFixes #42006
From Claude Code