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

Pydantic · Pipe

model_config

model_config assigns a ConfigDict to a Pydantic model, controlling validation and schema behavior. Options include handling extra fields and enabling strict validation.

How it works

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.

Syntax

model_config = ConfigDict(...)

Parameters

NameTypeDescription
ConfigDictTypedDictA TypedDict containing model behavior settings such as extra-field handling and strict validation.

Returns

A configured Pydantic model; ConfigDict itself is a TypedDict used to specify model configuration.

Key points

  • ConfigDict is the typed configuration container used to express a model's Pydantic behavior.
  • The extra option defaults to ignoring unknown initialization data.
  • With extra='ignore', supplied fields not declared by the model are omitted from the created instance.
  • A model-wide strict policy is enabled by putting strict=True in its configuration.
  • Configuration can control the title used for the generated JSON Schema; absent an override, the model name supplies it.

Common mistakes

  • The extra setting controls unexpected initialization data; its default is 'ignore', so unrecognized fields are discarded unless configured otherwise.
  • Alias-related population is not automatically enabled: configure the relevant option when callers must use the field's declared name.

Example: Forbid extra fields

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.

When to use it

Strict input contracts

Rejecting unexpected request or configuration fields at validation time.

Model-wide strictness

Applying strict validation consistently across every field in a model.

Arbitrary types

Allowing models to accept values whose types are outside Pydantic's standard supported set.

Schema customization

Customizing generated JSON Schema metadata.

Interview questions

QWhat is model_config used for?

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.

QHow does the extra configuration option work?

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.

QHow do you enable strict validation for a model?

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.

  • BaseModel
  • ConfigDict
  • extra
  • strict
  • arbitrary_types_allowed

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