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

Pydantic · Pipe

arbitrary_types_allowed

Allows model fields to use ordinary Python classes that Pydantic does not know how to validate. When enabled, Pydantic checks the supplied value's instance type rather than validating the object's internals.

How it works

Set arbitrary_types_allowed to True in a model's ConfigDict when a field should accept instances of a regular Python class. Pydantic then performs a simple instance check rather than validating that class's internal attributes.

Syntax

model_config = ConfigDict(arbitrary_types_allowed=True)

Parameters

NameTypeDescription
arbitrary_types_allowedboolBoolean configuration setting controlling whether arbitrary Python classes may be used as field types.

Returns

A model configured to accept arbitrary class instances as field values.

Key points

  • The option controls whether fields may be annotated with arbitrary Python classes.
  • Pydantic disables arbitrary field types unless the model explicitly opts in.
  • Enable the behavior by assigning a ConfigDict with arbitrary_types_allowed=True to the model.
  • A regular class can be used as a field type after the option is enabled.

Example: Allow an arbitrary class as a field 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("Fluffy"))
assert model.pet.name == "Fluffy"

The model accepts a Pet instance without requiring Pet to be a Pydantic model.

When to use it

Domain objects

A field must hold a domain object implemented as a normal Python class.

Third-party types

A model integrates a class supplied by an external library.

Interview questions

QHow do you allow a regular Python class as a Pydantic field type?

Set model_config = ConfigDict(arbitrary_types_allowed=True) on the model. This permits fields annotated with ordinary Python classes, such as Pet, and validates them by checking their type.

Follow-up: What happens to attributes inside the arbitrary object? They are not recursively validated by Pydantic; only the outer instance check is performed.

QWhat is the default value and what does enabling the option change?

The default is False, so arbitrary class types are rejected unless the model enables this configuration option. With it enabled, a value must be an instance of the annotated class.

Follow-up: How would you preserve validation of the object's internal data? Define that data in a Pydantic model or add explicit validation outside this configuration option.

QWhen is this configuration useful?

Use ConfigDict(arbitrary_types_allowed=True) in the model configuration. The option is useful when integrating domain objects or third-party classes that Pydantic does not model itself.

Follow-up: Does enabling it make every value valid? No; values still need to be instances of the declared arbitrary type.

  • BaseModel
  • ConfigDict
  • ValidationError

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