Parsing LLM JSON into Pydantic Models
What you'll learn: How to safely convert raw JSON strings from LLMs into validated Pydantic objects while gracefully handling parsing and validation errors.
The Core Problem
When an LLM returns structured output as JSON, you receive it as a raw string. Before you can use this data reliably in your application, you need to:
- Parse the string into Python objects
- Validate that the structure matches your expectations
- Handle cases where the LLM generated invalid or malformed JSON
Think of this like receiving a form from a user—you don't trust it blindly; you validate each field before processing.
The Parsing Process
You'll use Pydantic's model_validate_json() method to parse and validate in one step:
from pydantic import BaseModel, ValidationError
class UserProfile(BaseModel):
name: str
age: int
email: str
# Raw JSON string from LLM
llm_output = '{"name": "Alice", "age": 30, "email": "alice@example.com"}'
try:
profile = UserProfile.model_validate_json(llm_output)
print(f"Parsed: {profile.name}, {profile.age}")
except ValidationError as e:
print(f"Validation failed: {e}")
except Exception as e:
print(f"Parsing failed: {e}")
Graceful Error Handling
Always wrap parsing in try-except blocks. Common failures include:
- Malformed JSON: LLM included extra text or invalid syntax
- Validation errors: Types don't match (string instead of int)
- Missing required fields: LLM omitted expected data
Handle these failures by logging the error, retrying with a refined prompt, or falling back to a default response.
Key Takeaway: Use model_validate_json() with try-except blocks to transform LLM JSON strings into validated Pydantic objects, always preparing for parsing and validation failures that require graceful recovery strategies.