Debug Fix intermediate · 3 min read

How to handle Pydantic validation errors from LLM

Quick answer
Use try-except blocks around your Pydantic model parsing to catch ValidationError exceptions when processing LLM outputs. Validate and sanitize the LLM response before parsing to avoid malformed data causing errors.
ERROR TYPE model_behavior
⚡ QUICK FIX
Wrap your Pydantic model parsing in a try-except block to catch and handle ValidationError gracefully.

Why this happens

Pydantic validation errors occur when the data returned by an LLM does not conform to the expected schema defined by your Pydantic model. This often happens because LLMs generate text that may be incomplete, malformed, or have unexpected types or missing fields.

For example, if your model expects an integer but the LLM returns a string or null, pydantic.ValidationError will be raised during parsing.

Typical triggering code looks like this:

python
from pydantic import BaseModel, ValidationError

class User(BaseModel):
    id: int
    name: str

llm_response = {'id': 'abc', 'name': 'Alice'}  # id should be int, but is str

user = User(**llm_response)  # Raises ValidationError
output
pydantic.error_wrappers.ValidationError: 1 validation error for User
id
  value is not a valid integer (type=type_error.integer)

The fix

Wrap the Pydantic parsing in a try-except block to catch ValidationError. This allows you to handle errors gracefully, log them, or fallback to defaults.

Additionally, pre-validate or sanitize the LLM output if possible to reduce errors.

python
from pydantic import BaseModel, ValidationError
import os
from openai import OpenAI

class User(BaseModel):
    id: int
    name: str

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Generate user data as JSON"}]
)

import json

try:
    llm_output = response.choices[0].message.content
    data = json.loads(llm_output)  # parse JSON string
    user = User(**data)  # validate with Pydantic
    print(user)
except (json.JSONDecodeError, ValidationError) as e:
    print(f"Validation failed: {e}")
    # Handle error: fallback, retry, or sanitize data
output
Validation failed: 1 validation error for User
id
  value is not a valid integer (type=type_error.integer)

Preventing it in production

  • Implement retries with exponential backoff for transient LLM output errors.
  • Use strict JSON schema or Pydantic models to enforce output format.
  • Sanitize or preprocess LLM outputs before parsing (e.g., remove trailing commas, fix quotes).
  • Log validation errors with context for debugging.
  • Consider fallback defaults or user prompts to regenerate output if validation fails.

Key Takeaways

  • Always catch pydantic.ValidationError when parsing LLM outputs to avoid crashes.
  • Pre-validate and sanitize LLM responses before feeding them to Pydantic models.
  • Implement retries and fallbacks to handle malformed or unexpected LLM outputs gracefully.
Verified 2026-04 · gpt-4o
Verify ↗