High severity intermediate · Fix: 5-10 min

ValidationError

pydantic.error_wrappers.ValidationError

What this error means
Pydantic raises ValidationError because the JSON schema includes additionalProperties which Pydantic does not support, causing structured output parsing to fail.

Stack trace

traceback
pydantic.error_wrappers.ValidationError: 1 validation error for OutputModel
__root__
  extra fields not permitted (type=value_error.extra)
QUICK FIX
Set your Pydantic model Config with extra = 'allow' to permit additional fields and avoid validation errors.

Why it happens

Pydantic's JSON schema generation and validation do not support the additionalProperties keyword, which is often used in JSON schemas to allow extra fields. When a schema includes additionalProperties, Pydantic rejects extra fields by default, causing validation errors during output parsing.

Detection

Catch ValidationError exceptions during output parsing and log the raw LLM output to detect unexpected extra fields that violate the Pydantic schema constraints.

Causes & fixes

1

The JSON schema uses additionalProperties to allow extra fields, but Pydantic does not support this keyword.

✓ Fix

Remove additionalProperties from your JSON schema or explicitly define all expected fields in your Pydantic model to avoid extra fields.

2

The LLM output contains extra fields not defined in the Pydantic model, triggering validation errors.

✓ Fix

Update your Pydantic model to include all possible fields or preprocess the LLM output to remove unexpected fields before validation.

3

Using Pydantic v1 which strictly forbids extra fields by default.

✓ Fix

Set Pydantic model Config with extra = 'allow' to permit additional fields during validation.

Code: broken vs fixed

Broken - triggers the error
python
from pydantic import BaseModel

class OutputModel(BaseModel):
    name: str
    age: int

# This will raise ValidationError if extra fields are present
output = OutputModel.parse_raw('{"name": "Alice", "age": 30, "extra": "field"}')  # Raises ValidationError
Fixed - works correctly
python
from pydantic import BaseModel

class OutputModel(BaseModel):
    name: str
    age: int

    class Config:
        extra = 'allow'  # Allow additional fields to prevent ValidationError

output = OutputModel.parse_raw('{"name": "Alice", "age": 30, "extra": "field"}')
print(output)
Added Config with extra = 'allow' in the Pydantic model to permit additional fields and prevent ValidationError when extra fields appear in the JSON.

Workaround

Wrap the parsing call in try/except ValidationError, then manually strip unexpected keys from the raw JSON string before re-parsing with Pydantic.

Prevention

Design your Pydantic models to explicitly include all expected fields or configure them to allow extra fields, and avoid relying on additionalProperties in JSON schemas when using Pydantic.

Python 3.9+ · pydantic >=1.0.0 · tested on 1.10.x
Verified 2026-04
Verify ↗

Community Notes

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