activity-progression-api / app /json_utils.py
mjpsm's picture
Deploy Progression API (llama-cpp-python)
63d4e3a verified
Raw History Blame Contribute Delete
507 Bytes
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