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.
Python code
9 linesimport 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
{'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
- Use `json.loads(response, strict=False)` directly if trailing commas are expected
- 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
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.