Parse the judge's JSON response, tolerating markdown fences.
(raw: str)
| 28 | |
| 29 | |
| 30 | def _parse_score_json(raw: str) -> Optional[dict]: |
| 31 | """Parse the judge's JSON response, tolerating markdown fences.""" |
| 32 | cleaned = raw.strip().strip("`").removeprefix("json").strip() |
| 33 | try: |
| 34 | result = json.loads(cleaned) |
| 35 | except json.JSONDecodeError: |
| 36 | # Try extracting JSON from markdown code block |
| 37 | m = re.search(r"\{[^{}]*\}", raw, re.DOTALL) |
| 38 | if not m: |
| 39 | return None |
| 40 | try: |
| 41 | result = json.loads(m.group()) |
| 42 | except json.JSONDecodeError: |
| 43 | return None |
| 44 | |
| 45 | if ( |
| 46 | isinstance(result, dict) |
| 47 | and isinstance(result.get("score"), int) |
| 48 | and 1 <= result["score"] <= 10 |
| 49 | and isinstance(result.get("reason"), str) |
| 50 | ): |
| 51 | return result |
| 52 | return None |
| 53 | |
| 54 | |
| 55 | def _call_judge( |