Merge pull request #667 from santhreal/fix/quiz-payload-raw-decode

fix(question): parse quiz JSON with raw_decode
This commit is contained in:
Bingxi Zhao
2026-07-23 22:26:22 +08:00
committed by GitHub
2 changed files with 15 additions and 3 deletions

View File

@@ -1299,11 +1299,12 @@ class QuestionPipeline:
try:
parsed = json.loads(text)
except json.JSONDecodeError:
obj = re.search(r"\{[\s\S]*\}", text)
if obj is None:
# First JSON object only; trailing brace-prose must not extend the slice.
start = text.find("{")
if start == -1:
return {}
try:
parsed = json.loads(obj.group(0))
parsed, _end = json.JSONDecoder().raw_decode(text[start:])
except json.JSONDecodeError:
return {}
return parsed if isinstance(parsed, dict) else {}

View File

@@ -984,3 +984,14 @@ def test_parse_exam_paper_to_templates_happy_path(monkeypatch, tmp_path: Path) -
assert templates[1].question_type == "choice"
assert trace["template_count"] == "2"
assert trace["question_file"].endswith("exam_questions.json")
def test_parse_quiz_payload_tolerates_trailing_brace_prose() -> None:
raw = (
'{"question":"What is 2+2?","correct_answer":"4",'
'"explanation":"basic arithmetic"} note: see {docs}'
)
parsed = QuestionPipeline._parse_quiz_payload(raw)
assert parsed["question"] == "What is 2+2?"
assert parsed["correct_answer"] == "4"
assert parsed["explanation"] == "basic arithmetic"