bug(utils): parse_json_markdown drops a valid payload when the text contains a backtick
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Start in graphon/utils/json_in_md_parser.py, in parse_json_markdown. The start loop picks the first marker kind found anywhere in the text, so backticks win over braces, and the end search uses rfind over the whole string. Run the three reproduction strings from the issue, then tests/utils/test_json_in_md_parser.py. Done when those inputs return the first JSON value, fenced blocks still take priority, and the existing array-unwrapping test still passes.
Written by the indexing model from the issue text.
Description
Summary
parse_json_markdown rejects a valid payload whenever a backtick appears anywhere in the text after it. A single inline-code span — extremely common in model output — is enough to turn a well-formed JSON object at offset 0 into could not find json block in the output.
Steps to reproduce
Pure stdlib, no runtime or provider involved, against main (57806a745305b2a7da43aa966ed2f3d093f728a1):
from graphon.utils.json_in_md_parser import parse_json_markdown
parse_json_markdown('{"a": 1} and `code`')
# JSONDecodeError: Expecting value: line 1 column 1 (char 0)
parse_json_markdown('{"a": 1} then a lone ` backtick')
# ValueError: could not find json block in the output.
parse_json_markdown('{"a": 1} then {"b": 2}')
# JSONDecodeError: Extra data: line 1 column 10 (char 9)
The same payload without the trailing text parses normally:
parse_json_markdown('{"a": 1}') # {"a": 1}
Root cause
Two independent defects in the start/end discovery, both of which slice by text search instead of parsing:
-
Start offset. The loop
for start_marker in ["```json", "```", "``", "", "{", "["]keeps the first marker **kind** that occurs anywhere, not the earliest position. Because ```` is tried before{, a backtick later in the text wins over a brace that appears earlier. The offset then lands on the backtick, and the payload is never considered. -
End offset.
rfind(end_marker, start_index)takes the last closing character in the whole string. With two payloads, or a stray}in the prose after the payload, the slice runs past the first value andjson.loadsfails on the extra data.
Expected behavior
The docstring already states the contract: "Extract and parse the first JSON object or array embedded in markdown text." Trailing prose, a second payload, or a backtick inside a string should not prevent that.
Scope note
I deliberately preserved the existing array-unwrapping behavior: '[{"a": 1}]' currently returns {"a": 1} (the brace is found before the bracket), and tests/utils/test_json_in_md_parser.py asserts exactly that. Changing it is a separate discussion about the return contract, not part of this fix.
Fix
A PR follows: pick the payload start by preferring braces over backticks (fences keep priority so a fenced block still beats prose braces), and parse one value with json.JSONDecoder().raw_decode instead of slicing to the last end marker. Seven existing tests keep passing and five cases are added.
- Dominant language
- Python
- Stars
- 90
- Forks
- 47
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 19
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from langgenius/graphon
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
langgenius/graphon#443 ·
Maintainers usually reply within 2 days
-
Complete LLM results disappear from streamed answersPossibly taken @WH-2099 claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
langgenius/graphon#389 · 1 comment · 1 assignee ·
Maintainers usually reply within 2 days
-
project#graphon
Difficulty 4/5 3-5 days Newbie friendliness 40/100
langgenius/graphon#442 ·
Maintainers usually reply within 2 days
-
project#graphon
Difficulty 3/5 1-2 days Newbie friendliness 50/100
langgenius/graphon#438 · 1 comment ·
Maintainers usually reply within 2 days
-
project#graphon
Difficulty 5/5 Over a week Newbie friendliness 8/100
langgenius/graphon#436 · 3 comments ·
Maintainers usually reply within 2 days
All issues in langgenius/graphon
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
[BUG]: Management endpoints /v1/memory/stats and /v1/memory/counts bypass API key authenticationOpen
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
docling-project/docling-serve#788 ·
Maintainers usually reply within 1 day
-
Mend: dependency security vulnerability untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
opensearch-project/cross-cluster-replication#1805 ·
Maintainers usually reply within 1 day
-
load_advbench() re-raises HTTPError as the bare class, so a failed download ends in a TypeErrorOpenneeds-triage
Difficulty 1/5 Under an hour Newbie friendliness 82/100
Maintainers usually reply within 2 days