fix(api): anchor JSON extraction on brackets in parse_json_markdown (#41959)

This commit is contained in:
Harsh Kashyap 2026-09-08 08:32:14 +00:00 committed by GitHub
parent 2cdfe24c18
commit 67f7a5517a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 7 deletions

View File

@ -6,23 +6,35 @@ from core.llm_generator.output_parser.errors import OutputParserError
def parse_json_markdown(json_string: str):
# Get json from the backticks/braces
json_string = json_string.strip()
starts = ["```json", "```", "``", "`", "{", "["]
ends = ["```", "``", "`", "}", "]"]
parsed: dict = {}
# Anchor on the JSON brackets themselves: from the first "{" or "[" to the
# last "}" or "]". This works whether or not the JSON is wrapped in code
# fences, and - unlike marker-priority search - is not confused by
# backticks inside JSON string values or in surrounding prose.
start_candidates = [i for i in (json_string.find("{"), json_string.find("[")) if i != -1]
if start_candidates:
start_index = min(start_candidates)
end_index = max(json_string.rfind("}"), json_string.rfind("]"))
if end_index != -1 and start_index < end_index:
end_index += 1
extracted_content = json_string[start_index:end_index].strip()
return json.loads(extracted_content)
# Fallback for fenced content without brackets (e.g. a fenced scalar).
starts = ["```json", "```", "``", "`"]
ends = ["```", "``", "`"]
end_index = -1
start_index = 0
parsed: dict = {}
for s in starts:
start_index = json_string.find(s)
if start_index != -1:
if json_string[start_index] not in ("{", "["):
start_index += len(s)
start_index += len(s)
break
if start_index != -1:
for e in ends:
end_index = json_string.rfind(e, start_index)
if end_index != -1:
if json_string[end_index] in ("}", "]"):
end_index += 1
break
if start_index != -1 and end_index != -1 and start_index < end_index:
extracted_content = json_string[start_index:end_index].strip()

View File

@ -107,3 +107,20 @@ def test_parse_and_check_json_markdown_handles_think_fenced_and_raw_variants():
for src in cases:
obj = parse_and_check_json_markdown(src, ["keywords", "category_id", "category_name"])
assert obj == expected
def test_parse_json_markdown_backtick_inside_string_value():
"""Backticks inside JSON string values must not be mistaken for code fences."""
src = '{"code": "use `print` function", "n": 1}'
assert parse_json_markdown(src) == {"code": "use `print` function", "n": 1}
def test_parse_json_markdown_backtick_in_surrounding_prose():
"""Backticks in prose before the JSON must not break extraction."""
src = 'Here is `the` result: {"a": 1}'
assert parse_json_markdown(src) == {"a": 1}
def test_parse_json_markdown_fenced_scalar_still_supported():
"""Fenced content without brackets still parses via the fence fallback."""
assert parse_json_markdown('```json\n"hello"\n```') == "hello"