ValidationError
pydantic.error_wrappers.ValidationError
Stack trace
pydantic.error_wrappers.ValidationError: 1 validation error for OutputModel __root__ extra fields not permitted (type=value_error.extra)
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
The JSON schema uses additionalProperties to allow extra fields, but Pydantic does not support this keyword.
Remove additionalProperties from your JSON schema or explicitly define all expected fields in your Pydantic model to avoid extra fields.
The LLM output contains extra fields not defined in the Pydantic model, triggering validation errors.
Update your Pydantic model to include all possible fields or preprocess the LLM output to remove unexpected fields before validation.
Using Pydantic v1 which strictly forbids extra fields by default.
Set Pydantic model Config with extra = 'allow' to permit additional fields during validation.
Code: broken vs fixed
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 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) 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.