Skip to main content

How to Reject Unknown Fields in pydantic with extra="forbid"

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

By default, a pydantic model quietly drops any field it doesn't know. That hides typos: emial instead of email disappears without a word. Setting extra="forbid" turns every unknown field into an error. This guide shows the difference with pydantic 2.14.4 on Python 3.15.2.

Syntax​

Syntax
from pydantic import BaseModel, ConfigDict


class <Model>(BaseModel):
model_config = ConfigDict(extra="forbid")

<field>: <type>
PartWhat it means
model_configThe model's settings. It's a class attribute, not a field
ConfigDict(...)Builds those settings
extra="forbid"Any field that isn't declared raises a ValidationError. The other choices are "ignore" (the default) and "allow"

The ConfigDict reference describes extra as: "Whether to ignore, allow, or forbid extra data during model initialization. Defaults to 'ignore'."

The Example​

A User model has two fields, name and age. The script creates one user correctly, then passes a third field, emial, as a typing slip for email would.

Prerequisites​

  • pydantic in your project, for example with uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.

Step 1: Write the Script​

strict_user.py
from pydantic import BaseModel, ConfigDict, ValidationError


class User(BaseModel):
model_config = ConfigDict(extra="forbid")

name: str
age: int


print(User(name="Ada", age=36))

try:
User(name="Ada", age=36, emial="ada@example.com")
except ValidationError as error:
print(error)

The strict_user.py script in a macOS-style code window: a User model with model_config = ConfigDict(extra="forbid"), name and age fields, one valid user and one with an extra emial field inside try and except ValidationError.

Step 2: Run It​

Terminal
uv run python strict_user.py
Output
name='Ada' age=36
1 validation error for User
emial
Extra inputs are not permitted [type=extra_forbidden, input_value='ada@example.com', input_type=str]
For further information visit https://errors.pydantic.dev/2.14/v/extra_forbidden

The terminal after uv run python strict_user.py: the valid user, then 1 validation error for User naming emial with Extra inputs are not permitted.

The valid user printed normally. The second one failed, and the error names the unknown field, emial, and the value it carried. The command works unchanged in PowerShell.

Without forbid​

Here's the same second user against a User model with no model_config, which uses the default, "ignore":

Output
name='Ada' age=36

No error, and the email is gone. With ConfigDict(extra="allow") instead, the model keeps it as an extra attribute:

Output
name='Ada' age=36 emial='ada@example.com'

Common Mistakes​

MistakeWhat happensDo this instead
Leaving the defaultA misspelled field is dropped without a warningSet extra="forbid" when every field is known in advance, such as in a settings file

Troubleshooting​

SymptomLikely causeFix
Extra inputs are not permitted [type=extra_forbidden ...]The data has a field the model doesn't declareFix the field's spelling, or add the field to the model

FAQ​

Does forbid apply to models nested inside this one?​

No. Each model has its own model_config. With extra="forbid" on User only, a nested Address model still dropped an unknown postcode without an error, and printed name='Ada' address=Address(city='London'). A shared base class sets it once: when Address inherited from a Strict(BaseModel) class with extra="forbid", the same postcode failed with Extra inputs are not permitted.

When is ignore the better choice?​

When the data comes from a source that adds fields you don't use, such as another team's API. Forbid would then fail on every new field they add.

Conclusion​

model_config = ConfigDict(extra="forbid") made User refuse the misspelled emial with Extra inputs are not permitted, where the default would have dropped it without a word.

Additional Resources​