bug(utils): parse_json_markdown drops a valid payload when the text contains a backtick

Open Beginner friendly
#380 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

@WH-2099 is already working on this.

Since Nov 17, 2026.

  • #381 by @mosifu — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

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:

  1. 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.

  2. 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 and json.loads fails 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/graphon

All issues in langgenius/graphon

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.