File size: 507 Bytes
63d4e3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import json
import re


def extract_json(text: str) -> dict | None:
    """
    Match the JSON extraction logic used during checkpoint evaluation:
    find the first decodable JSON object in the generated text.
    """
    decoder = json.JSONDecoder()

    for match in re.finditer(r"\{", text):
        try:
            obj, _ = decoder.raw_decode(text[match.start():])
            if isinstance(obj, dict):
                return obj
        except json.JSONDecodeError:
            pass

    return None