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.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 12 views 0 copies

Python code

9 lines
Python 3.9+
import 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

stdout
{'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

  1. Use `json.JSONDecoder` to find the first valid JSON object if the output contains extra text
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.