Spaces:
Build error
Build error
| import json | |
| import re | |
| def extract_json(raw: str) -> dict: | |
| if not raw or not raw.strip(): | |
| raise ValueError("Empty LLM output") | |
| text = raw.strip() | |
| # Remove markdown fences | |
| text = re.sub(r"^```(?:json)?", "", text, flags=re.IGNORECASE).strip() | |
| text = re.sub(r"```$", "", text).strip() | |
| # Extract JSON object defensively | |
| start = text.find("{") | |
| end = text.rfind("}") | |
| if start == -1 or end == -1: | |
| raise ValueError("No JSON object found") | |
| return json.loads(text[start:end + 1]) | |