resume-model-api / model /llm /extraction.py
MossaicMan's picture
Upload 33 files
345000b verified
raw
history blame contribute delete
553 Bytes
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])