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]}")