File size: 731 Bytes
63645ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import annotations
import json
import re

def extract_json(text: str) -> dict:
    """
    Robustly extract JSON from LLM response.
    Handles: raw JSON, markdown code fences, leading/trailing prose.
    """
    text = text.strip()
    text = re.sub(r"```(?:json)?\s*", "", text).replace("```", "")
    try:
        return json.loads(text)
    except json.JSONDecodeError:
        pass
    for pattern in (r"\{[\s\S]*\}", r"\[[\s\S]*\]"):
        match = re.search(pattern, text)
        if match:
            try:
                return json.loads(match.group())
            except json.JSONDecodeError:
                pass
    raise ValueError(f"No valid JSON found in response. First 200 chars: {text[:200]}")