High severity intermediate · Fix: 2-5 min

ValidationError

pydantic.error_wrappers.ValidationError

What this error means
Anthropic's structured output parsing fails when the LLM response does not conform exactly to the expected Pydantic schema.

Stack trace

traceback
pydantic.error_wrappers.ValidationError: 1 validation error for OutputModel
field_name
  field required (type=value_error.missing)

During handling of the above exception, the Anthropic client raised a ValidationError when parsing the structured output.
QUICK FIX
Add explicit prompt instructions to return only raw JSON and align your Pydantic schema fields exactly with the expected output keys.

Why it happens

Anthropic's structured output expects the LLM to return JSON strictly matching the Pydantic model schema. If the LLM adds extra text, markdown fences, or omits required fields, Pydantic raises a ValidationError. This often happens if the prompt does not enforce strict JSON-only output or if the model is not instruction-tuned.

Detection

Catch pydantic.ValidationError exceptions during output parsing and log the raw LLM response to identify mismatches before the app crashes.

Causes & fixes

1

LLM response includes markdown fences or extra text around the JSON output

✓ Fix

Modify the prompt to instruct the model to return only raw JSON without markdown fences, or use a parser that strips fences automatically.

2

Pydantic model fields do not exactly match the keys in the LLM's JSON output

✓ Fix

Ensure the Pydantic model field names and types exactly match the expected JSON keys and data types returned by the LLM.

3

Using a base Anthropic model that does not reliably follow structured output instructions

✓ Fix

Switch to an instruction-tuned Anthropic model like claude-3-5-haiku-20241022 that better respects output format constraints.

Code: broken vs fixed

Broken - triggers the error
python
import os
from anthropic import Anthropic
from pydantic import BaseModel

class OutputModel(BaseModel):
    field_name: str

client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])

response = client.messages.create(
    model='claude-3-5-haiku-20241022',
    system='You are a helpful assistant.',
    messages=[{'role': 'user', 'content': 'Return data as JSON with field_name.'}]
)

# This line raises ValidationError because response includes markdown fences
output = OutputModel.parse_raw(response['completion'])  # Error here
Fixed - works correctly
python
import os
from anthropic import Anthropic
from pydantic import BaseModel

class OutputModel(BaseModel):
    field_name: str

client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])

prompt = (
    'Return ONLY raw JSON with field_name, no markdown fences or extra text.'
)

response = client.messages.create(
    model='claude-3-5-haiku-20241022',
    system='You are a helpful assistant.',
    messages=[{'role': 'user', 'content': prompt}]
)

output = OutputModel.parse_raw(response['completion'])  # Fixed: prompt enforces raw JSON
print(output)
Added explicit prompt instructions to return raw JSON only, preventing markdown fences and extra text that cause Pydantic parse errors.

Workaround

Wrap the Pydantic parse call in try/except ValidationError, then extract JSON from the raw string using regex and parse it manually with json.loads() as a fallback.

Prevention

Use Anthropic's structured output features or tool use to enforce schema-valid JSON responses at the API level, avoiding fragile post-processing parsing.

Python 3.9+ · anthropic >=0.20.0 · tested on 0.20.x
Verified 2026-04 · claude-3-5-haiku-20241022, claude-opus-4
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.