Spaces:
Sleeping
Sleeping
File size: 581 Bytes
95a30b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import re
def extract_json(text: str) -> str:
"""
Extracts JSON from LLM output, removing markdown triple backticks.
Falls back to grabbing first {...} block if no fenced block detected.
"""
# Try triple backticks with optional 'json'
match = re.search(r"``````", text, re.DOTALL)
if match:
return match.group(1).strip()
# Fallback: Try to find first JSON object in text without triple backticks
match = re.search(r"(\{.*\})", text, re.DOTALL)
if match:
return match.group(1).strip()
return text.strip()
|