Permit arbitrary classes
A model contains a field whose value is an instance of a regular user-defined class.
Pydantic · Class
ConfigDict is Pydantic's TypedDict for configuring model behavior. Assign it to model_config to control validation and schema-related options.
ConfigDict is a TypedDict used to declare configuration for Pydantic models. Assign it to a model's model_config attribute to control validation and schema behavior, such as allowing arbitrary types or enabling strict validation.
class ConfigDict(TypedDict, total=False): ...A TypedDict containing configuration options for Pydantic models.
from pydantic import BaseModel, ConfigDict
class Pet:
def __init__(self, name: str):
self.name = name
class Model(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
pet: Pet
model = Model(pet=Pet("Milo"))
print(model.pet.name)Allows a model to accept an instance of an arbitrary user-defined class as a field value.
A model contains a field whose value is an instance of a regular user-defined class.
A model must reject common coercions and apply stricter field validation.
A model's declared defaults must be checked instead of being trusted automatically.
ConfigDict is a TypedDict that holds configuration for Pydantic behavior. A model typically uses it through a class-level model_config assignment, for example model_config = ConfigDict(strict=True).
Follow-up: How would you configure only one field rather than the entire model? Use field-level configuration mechanisms rather than putting the option in the model-wide ConfigDict.
Set arbitrary_types_allowed=True in ConfigDict when a model field should accept an instance of a user-defined class that Pydantic does not model itself. The class instance is then accepted as an arbitrary type.
Follow-up: Does this option make Pydantic validate the internals of that class? No; allowing the type does not provide the class with Pydantic's normal field validation.
ConfigDict can enable strict mode for a model by setting strict=True. This changes validation so fields do not use the usual permissive conversions where strict validation applies.
Follow-up: How can strictness be scoped more narrowly? Configure strict behavior at the field or validation-call level when model-wide strictness is too broad.
BaseModelValidationErrorwith_configmodel_config