Skip to content

Instantly share code, notes, and snippets.

View tikendraw's full-sized avatar
🎯
Focusing

Tikendra Sahu tikendraw

🎯
Focusing
View GitHub Profile
@tikendraw
tikendraw / parse json code blocks for text blobs.py
Created September 21, 2024 06:44
Extract json Code blocks for pydantic class with fallback
import re
import json
import ast
from pydantic import ValidationError
def extract_code_block(text):
# This regex looks for ```json or ``` followed by { ... } (JSON or dict-like structure)
code_block = re.findall(r'```(?:json)?\s*({.*?})\s*```', text, re.DOTALL)