How to Parse an LLM Response in Python

This code parses a JSON string from an LLM response, stripping code fences and handling common issues like whitespace, returning a Python dictionary.

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

Python code

9 lines
Python 3.9+
import json
from typing import Any, Dict, List


def parse_llm_response(response: str) -> Dict[str, Any]:
    """Parse a JSON string from an LLM response, handling common edge cases."""
    # Remove code fences if present
    cleaned = response.strip()
    if cleaned.startswith("

Output

stdout
{'name': 'Alice', 'age': 30}

How it works

The function first strips whitespace and removes markdown code fences (json ...) if present. It then uses json.loads() to convert the cleaned string into a Python dictionary. If the JSON string contains trailing commas or other minor issues, a fallback with strict=False is attempted. This makes the parser resilient to common variations in LLM outputs.

Common mistakes

  • Not removing code fences before parsing
  • Assuming json.loads works on any text without cleaning
  • Forgetting to handle empty or None responses
  • Ignoring potential trailing commas in LLM output

Variations

  1. Use `json.loads(response, strict=False)` directly if trailing commas are expected
  2. Use a regex to strip code fences more precisely

Real-world use cases

  • Parsing structured outputs from an LLM API (like OpenAI's response_format) for downstream processing.
  • Extracting configuration data from a chatbot's reply to update system settings.
  • Converting a language model's JSON response into Python objects for a data pipeline.

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.