Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pydantic · Class

ConfigDict

ConfigDict is Pydantic's TypedDict for configuring model behavior. Assign it to model_config to control validation and schema-related options.

How it works

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.

Syntax

class ConfigDict(TypedDict, total=False): ...

Returns

A TypedDict containing configuration options for Pydantic models.

Key points

  • ConfigDict is a TypedDict whose purpose is to represent Pydantic model configuration.
  • Setting strict=True in the model configuration enables strict mode across the model's fields.
  • Assign ConfigDict to model_config to allow fields containing arbitrary user-defined types.
  • The configuration includes an option controlling whether default values are checked during validation.
  • The default-value validation option is disabled by default.

Common mistakes

  • ConfigDict itself describes configuration; it does not turn arbitrary classes into Pydantic models.
  • Default values are not necessarily validated unless the relevant configuration option is enabled.

Example: Allow an arbitrary type

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.

When to use it

Permit arbitrary classes

A model contains a field whose value is an instance of a regular user-defined class.

Enable strict mode

A model must reject common coercions and apply stricter field validation.

Validate defaults

A model's declared defaults must be checked instead of being trusted automatically.

Interview questions

QWhat is ConfigDict and how is it applied?

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.

QWhen would you use arbitrary_types_allowed?

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.

QHow do you configure strict 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.

  • BaseModel
  • ValidationError
  • with_config
  • model_config

Source: Configuration - Pydantic. Checked 2026-10-15.