aie3-autograder / extractjson.py
Dobin Yim
modular files
c97d8e1
raw
history blame
429 Bytes
import json
import os
from langchain_core.messages import AIMessage
from typing import List, Dict, Tuple
import re
def extract_json(message: AIMessage) -> List[dict]:
text = message.content
pattern = r"```json(.*?)```"
matches = re.findall(pattern, text, re.DOTALL)
try:
return [json.loads(match.strip()) for match in matches]
except Exception:
raise ValueError(f"Failed to parse: {message}")