Retain unknown fields
Accept payloads that may contain forward-compatible or vendor-specific keys while preserving those values for later processing.
Pydantic · Pipe
The extra configuration controls how Pydantic handles input keys that are not declared model fields. Choose ignore, forbid, or allow; the default is ignore.
The extra configuration controls what happens to input keys that are not declared as model fields. Set it through ConfigDict to ignore those keys, reject them with validation errors, or preserve them in __pydantic_extra__.
model_config = ConfigDict(extra='ignore' | 'forbid' | 'allow')| Name | Type | Description |
|---|---|---|
extra | Literal['ignore', 'forbid', 'allow'] | Controls whether extra input is ignored, accepted, or rejected during model initialization. Supported values are ignore, forbid, and allow; the default is ignore. |
ignore, forbid, and allow.ignore is used when no explicit extra policy is supplied.forbid converts unknown input into a validation error.allow preserves unknown values under the model's __pydantic_extra__ attribute.forbid policy makes unknown keys validation failures rather than merely ignoring them.from pydantic import BaseModel, ConfigDict
class User(BaseModel):
model_config = ConfigDict(extra='allow')
name: str
user = User(name='Ada', role='admin')
assert user.__pydantic_extra__ == {'role': 'admin'}Unknown input is accepted and retained in the model's extra-data dictionary.
Accept payloads that may contain forward-compatible or vendor-specific keys while preserving those values for later processing.
Enforce a strict input contract at API or configuration boundaries by rejecting unexpected keys.
Build models that intentionally tolerate evolving payloads without exposing unknown keys as declared fields.
extra configuration control?extra defines how Pydantic handles input that does not correspond to a declared field. Its supported policies are ignore, forbid, and allow, with ignore as the default.
Follow-up: What happens under allow? Extra values are stored on the instance in __pydantic_extra__.
With extra='ignore', extra input is accepted during initialization but omitted from the resulting model. This is the default behavior.
Follow-up: How would you reject such input? Set model_config = ConfigDict(extra='forbid'), which causes a ValidationError.
extra='allow' do?extra='allow' accepts undeclared input and stores it in the model's __pydantic_extra__ dictionary. Declared fields continue to be handled as normal model fields.
Follow-up: How can a model inspect retained unknown values? Read the instance's __pydantic_extra__ attribute.
BaseModelConfigDictValidationError__pydantic_extra__