Files
DeepTutor/deeptutor/utils/json_parser.py
Mohammad Hijjawi f07e262ebe fix: ignore code fences inside <think> reasoning when parsing JSON (#2717)
* fix: ignore code fences inside <think> reasoning when parsing JSON

A fence drafted inside a <think> block was picked as the payload before
the reasoning was stripped, so the real JSON after the block was lost.

* fix: decode fence-in-string JSON from the chosen block's offset

The fence-in-string fallback searched the response for the truncated
block text, which can also appear inside a <think> draft, so the draft
was decoded instead of the payload. Keep the chosen fence match's start
offset and decode from there, allowing raw newlines in string values.
2026-12-01 17:50:42 +08:00

235 lines
8.8 KiB
Python

#!/usr/bin/env python
"""
Robust JSON parsing utilities with automatic repair and markdown extraction.
Provides safe JSON parsing that handles:
- Markdown code block wrapping (```json...```)
- Malformed JSON (missing commas, trailing commas, etc.)
- Unescaped newlines and control characters
- Empty responses
"""
import json
import logging
import re
from typing import Any
_repair_json_fn: Any = None
try:
from json_repair import repair_json as _repair_json_import
except ImportError:
pass
else:
_repair_json_fn = _repair_json_import
# Keep a public alias so tests and callers can patch the repair hook directly.
repair_json = _repair_json_fn
logger = logging.getLogger(__name__)
_UNSET = object()
def _decode_longest_json_value(text: str) -> Any:
"""Return the longest top-level JSON value decodable from *text*.
LLM responses may surround the payload with prose on either side, and that
prose can itself contain small valid JSON fragments (e.g. schema examples
in a reasoning prelude — issues #673/#692). Decoding every candidate and
keeping the longest one picks the actual payload over such fragments.
Returns ``_UNSET`` when nothing decodes.
"""
decoder = json.JSONDecoder()
best: Any = _UNSET
best_length = 0
pos = 0
while True:
starts = [i for i in (text.find("{", pos), text.find("[", pos)) if i != -1]
if not starts:
return best
start = min(starts)
try:
parsed, consumed = decoder.raw_decode(text[start:])
except json.JSONDecodeError as err:
# Resume past the failure point: an opener inside the failed span
# could only yield a fragment of it, and repair handles those.
pos = start + max(1, err.pos)
continue
except RecursionError:
return best
if consumed > best_length:
best, best_length = parsed, consumed
pos = start + consumed
def parse_json_response(
response: str,
logger_instance: Any = None,
fallback: Any = _UNSET,
) -> Any:
"""
Safely parse JSON from LLM responses with automatic repair.
Implements a parsing strategy that preserves valid JSON first, then tries
markdown extraction, embedded values, and automated repair.
Args:
response: Raw string response from LLM
logger_instance: Logger instance for debugging (optional)
fallback: Value to return if all parsing fails.
Pass ``None`` explicitly to get ``None`` on failure;
omit the argument (or leave default) to get ``{}``.
Returns:
Parsed JSON object, or fallback value if parsing fails
Example:
>>> response = '```json\\n{"key": "value"}\\n```'
>>> data = parse_json_response(response)
>>> data
{'key': 'value'}
"""
log = logger_instance or logger
if fallback is _UNSET:
fallback = {}
# Handle empty response
if not response or not response.strip():
log.warning("LLM returned empty response")
return fallback
# Parse the complete response before looking for markdown fences. A valid
# JSON string value can itself contain fenced source code (#825).
try:
return json.loads(response)
except (json.JSONDecodeError, TypeError) as parse_error:
log.debug(f"Complete response JSON parse failed: {parse_error}")
# Explicit JSON fences take precedence over unlabeled examples. Consume
# every fence so a closing delimiter cannot masquerade as another opener.
extracted_response = response
# Offset in *response* where the chosen fenced block begins, if any.
block_start = -1
if "```" in response:
fence = re.compile(r"```([A-Za-z0-9_-]*)[ \t]*\n?(.*?)```", re.DOTALL)
matches = list(fence.finditer(response))
# A fence drafted inside <think> reasoning is not the payload; leave it
# for the <think> stripping below instead of picking it here.
think_spans = [
m.span()
for m in re.finditer(
r"<think\b[^>]*>.*?</think>", response, flags=re.DOTALL | re.IGNORECASE
)
]
candidates = [
m for m in matches if not any(start <= m.start() < end for start, end in think_spans)
]
blocks = [m for m in candidates if m[1].lower() == "json"]
if not blocks:
blocks = [m for m in candidates if not m[1]]
if blocks:
chosen = next((m for m in blocks if m[2].lstrip().startswith(("{", "["))), blocks[0])
extracted_response = chosen[2].strip()
block_start = chosen.start(2) + len(chosen[2]) - len(chosen[2].lstrip())
log.debug("Extracted JSON from markdown code block")
elif matches:
# Other languages are examples, not candidate response payloads.
extracted_response = fence.sub("", response)
# Parse an extracted fence before any <think> stripping.
try:
return json.loads(extracted_response)
except (json.JSONDecodeError, TypeError) as parse_error:
log.debug(f"Direct JSON parse failed: {parse_error}")
# Strategy 1b: strip chain-of-thought <think> reasoning that models like
# Qwen/DeepSeek emit before the JSON payload, then retry. Only reached once
# direct parsing has failed, so it never rewrites already-valid JSON. Left
# in place, a brace inside the reasoning is picked up by the raw_decode scan
# below and returned instead of the real object (see issue #673).
if "<think" in extracted_response.lower():
cleaned = re.sub(
r"<think\b[^>]*>.*?</think>",
"",
extracted_response,
flags=re.DOTALL | re.IGNORECASE,
)
# Also drop an unclosed leading <think> prelude up to the first opener.
cleaned = re.sub(
r"^\s*<think\b[^>]*>.*?(?=[{\[])",
"",
cleaned,
count=1,
flags=re.DOTALL | re.IGNORECASE,
)
cleaned = cleaned.strip()
if cleaned != extracted_response.strip():
if not cleaned:
log.warning("LLM response contained only <think> reasoning, no JSON payload")
return fallback
try:
return json.loads(cleaned)
except (json.JSONDecodeError, TypeError):
extracted_response = cleaned
# Prefer raw_decode before repair so brace-bearing prose around the payload
# cannot corrupt it (json-repair would wrap payload + trailing prose into
# an array). The longest decodable value wins, so a schema example inside
# a reasoning prelude never shadows the real payload behind it (#692).
if isinstance(extracted_response, str):
decoded = _decode_longest_json_value(extracted_response)
if decoded is not _UNSET:
return decoded
# A fence inside a JSON string value ends the fenced block early, so
# decode the value from where the chosen block starts in the full
# response. Use the match offset rather than searching for the block
# text, which can also occur earlier, e.g. in a <think> draft. Code
# in such a value often carries raw newlines, so decode non-strictly.
if block_start > 0 and response.startswith(("{", "["), block_start):
try:
return json.JSONDecoder(strict=False).raw_decode(response, block_start)[0]
except (json.JSONDecodeError, RecursionError):
pass
# Strategy 2: Try json-repair if available
if repair_json is None:
log.warning("json-repair library not installed, cannot repair malformed JSON")
log.debug(f"Response: {extracted_response[:200]}")
return fallback
try:
log.debug("Attempting JSON repair")
repaired = repair_json(extracted_response)
result = json.loads(repaired)
log.info("Successfully repaired malformed JSON")
return result
except Exception as repair_error:
# Most callers use this helper as best-effort parsing with an explicit
# fallback. Non-JSON prose is common in LLM/tool output and should not
# look like a backend failure when the caller can safely continue.
log.debug(f"JSON repair failed: {repair_error}")
log.debug(f"Response: {extracted_response[:200]}")
return fallback
def safe_json_loads(data: str, fallback: Any = _UNSET) -> Any:
"""
Simple wrapper for safe JSON loading.
Args:
data: JSON string
fallback: Value to return on failure (default: {})
Returns:
Parsed JSON or fallback value
"""
if fallback is _UNSET:
fallback = {}
try:
return json.loads(data)
except (json.JSONDecodeError, TypeError) as e:
logger.warning(f"JSON parse error: {e}")
return fallback