How to Parse JSON from LLM Model Output Fence in Python
Extract and parse a JSON object from a language model's output that may be wrapped in triple-backtick fences with an optional language tag.
Python code
9 linesimport json
import re
def parse_json_from_fence(text):
"""
Extract JSON object from a model output that may be wrapped in
triple-backtick fences with optional language tag.
"""
# Match content inside
Output
{'name': 'Alice', 'age': 30}
{'name': 'Bob', 'age': 25}
How it works
The function uses a regular expression to match a JSON-like block that may be enclosed in triple backticks, optionally preceded by a language identifier. It extracts the content inside the fences, then uses json.loads to convert the string into a Python dictionary. The regex pattern (?:\w+)?\s*([\s\S]*?) is non-greedy and captures the actual JSON content even if the model adds extra text before or after the fences. This approach is robust for typical LLM outputs that wrap structured data in code blocks.
Common mistakes
- Assuming the output is always clean JSON without fences or surrounding text
- Using a greedy regex that captures extra newlines or stray characters
- Forgetting to handle cases where the model outputs multiple JSON blocks
- Not stripping leading/trailing whitespace before parsing
Variations
- Use `json.JSONDecoder` to find the first valid JSON object if the output contains extra text
- Fallback to direct `json.loads` if the input is already plain JSON without fences
Real-world use cases
- Parsing structured data from a chat model's response in a chatbot application.
- Extracting configuration or parameters from a prompt-based code generator.
- Interpreting the output of an AI assistant that returns JSON-formatted answers for downstream automation.
Sponsored
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.