new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Pydantic · Concepts · all subjects

configuration

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Pydantic Settings validates from environment variables and secrets files

Pydantic Settings provides optional Pydantic features for loading a settings or config class from environment variables or secrets files. Settings are validated from these sources, so a ValidationError points at an environment value that didn't match its field.

ValidationError from settings points to environment value mismatch

When settings fail validation from environment variables or secrets files, a ValidationError is raised that points at the specific environment value that didn't match its field.

Pydantic model configuration with ConfigDict

Pydantic models support configuration values through the model_config attribute set to ConfigDict(...). Configuration options can control validation behavior, serialization, and other model properties.

Extra data handling with ConfigDict.extra

The extra configuration value controls how Pydantic handles extra data not corresponding to defined fields. The configuration can take three values: 'ignore' (default, providing extra data is ignored), 'forbid' (providing extra data raises an error), and 'allow' (extra data is allowed and stored in the __pydantic_extra__ dictionary attribute). The validation methods (e.g., model_validate()) have an optional extra argument that will override the extra configuration value for that validation call.

__pydantic_extra__ dictionary for allowed extra fields

When ConfigDict.extra is set to 'allow', extra fields provided during model initialization are stored in the __pydantic_extra__ dictionary attribute. The __pydantic_extra__ can explicitly be annotated to provide validation for extra fields.

from_attributes configuration for ORM mode

The from_attributes configuration value enables ORM mode in Pydantic v2. When enabled, Pydantic can validate arbitrary objects (such as ORM model instances) by extracting attributes corresponding to field names, instead of requiring dictionary input. This mode can be set on model_config or passed as a parameter to model_validate().

Frozen models with ConfigDict.frozen

Models can be configured to be immutable via model_config['frozen'] = True. When frozen is set, attempting to change the values of instance attributes will raise ValidationError. Note that Python immutability is not enforced and developers can still modify objects if they choose to do so.

Frozen models do not prevent mutable field mutations

While frozen models prevent direct attribute reassignment, mutable fields like dictionaries or lists within frozen models can still be modified. The frozen configuration only prevents reassigning the attribute itself, not mutating objects referenced by the attribute.

revalidate_instances configuration for model instances

By default, when passing model instances to model_validate(), they are assumed to be valid and are used as-is without revalidation. This behavior can be overridden by setting model_config['revalidate_instances'] = 'always' to force revalidation of model instances.

Give your agent this brain