Strict input contracts
Rejecting unexpected request or configuration fields at validation time.
Pydantic · Pipe
model_config assigns a ConfigDict to a Pydantic model, controlling validation and schema behavior. Options include handling extra fields and enabling strict validation.
Assign a ConfigDict to model_config inside a BaseModel subclass to control validation and schema behavior. Configuration options can change how extra input is handled, whether strict validation is enabled, and how fields are populated.
model_config = ConfigDict(...)| Name | Type | Description |
|---|---|---|
ConfigDict | TypedDict | A TypedDict containing model behavior settings such as extra-field handling and strict validation. |
A configured Pydantic model; ConfigDict itself is a TypedDict used to specify model configuration.
from pydantic import BaseModel, ConfigDict
class User(BaseModel):
model_config = ConfigDict(extra='forbid')
name: str
user = User(name='John')
print(user)This model rejects unexpected initialization fields while validating the declared name.
Rejecting unexpected request or configuration fields at validation time.
Applying strict validation consistently across every field in a model.
Allowing models to accept values whose types are outside Pydantic's standard supported set.
Customizing generated JSON Schema metadata.
model_config is a class-level configuration value on a Pydantic model. It is assigned a ConfigDict containing options that affect model behavior, such as handling extra data or enabling strict mode.
Follow-up: How would you reject unknown fields? Set model_config = ConfigDict(extra='forbid') so unexpected initialization data is rejected.
The extra option determines whether unexpected initialization fields are ignored, accepted, or rejected. The documented default is 'ignore'.
Follow-up: What happens with extra='ignore'? Extra input is discarded and does not appear in the resulting model, while declared fields are still validated.
Set strict=True in the model's ConfigDict to configure strict mode for all fields. This changes validation so coercion is not broadly applied according to the model's strict-validation rules.
Follow-up: Can strictness be configured for only one field? Yes; use field-level validation configuration when model-wide strict mode is unnecessarily broad.
BaseModelConfigDictextrastrictarbitrary_types_allowed