Skip to content

Output Model Types

datamodel-code-generator supports multiple output model types. This page compares them to help you choose the right one for your project.

Quick Comparison

Model Type Validation Serialization Performance Use Case
Pydantic v2 Runtime Built-in Fast New projects, APIs, data validation
Pydantic v2 dataclass Runtime Built-in Fast Pydantic validation with dataclass syntax
dataclasses None Manual Fastest Simple data containers, no validation needed
TypedDict Static only Dict-compatible N/A Type hints for dicts, JSON APIs
msgspec Runtime Built-in Fastest High-performance serialization

Use --output-model-type pydantic_v2.BaseModel

Pydantic v2 is recommended for new projects. It offers better performance and a modern API.

datamodel-codegen --input schema.json --output-model-type pydantic_v2.BaseModel --output model.py
from pydantic import BaseModel, Field, RootModel


class Pet(BaseModel):
    id: int = Field(..., ge=0)
    name: str = Field(..., max_length=256)
    tag: str | None = None


class Pets(RootModel[list[Pet]]):
    root: list[Pet]

A oneOf or anyOf whose branches each require one property with one value (a tag) but that declares no discriminator is a plain union, which Pydantic validates by trying every branch. --infer-union-discriminators writes it as a discriminated union (Field(discriminator=...)), so an invalid payload reports the errors of the branch its tag names; see Tagged unions without a discriminator keyword.

When to use

  • New projects requiring data validation
  • APIs requiring data validation
  • Projects needing JSON Schema generation from models

Pydantic v2 dataclass

Use --output-model-type pydantic_v2.dataclass

Pydantic v2 dataclass combines the familiar dataclass syntax with Pydantic's validation capabilities.

datamodel-codegen --input schema.json --output-model-type pydantic_v2.dataclass --output model.py
from pydantic.dataclasses import dataclass
from typing import Optional


@dataclass
class Pet:
    id: int
    name: str
    tag: Optional[str] = None

When to use

  • Want to use dataclass syntax with Pydantic validation
  • Migrating from dataclasses but need validation
  • Prefer decorator-based class definition

Targeting a Pydantic version

Pydantic output depends only on the inputs and options, never on the Pydantic version installed next to datamodel-codegen. Use --target-pydantic-version to choose the oldest Pydantic release the generated code must run on. Pass --target-pydantic-version 2 when the generated code must also run on older Pydantic 2 releases. 2 means Pydantic 2.0.3 or later: generated code is not checked on 2.0.0 to 2.0.2.

When the target is unset:

  • Feature forms use the newest supported forms, the same as 2.12.
  • Config key naming is unchanged: --allow-population-by-field-name emits populate_by_name=True, as with 2. Only an explicit 2.11, 2.12 or 2.14 emits validate_by_name=True.
  • --use-missing-sentinel keeps the experimental pydantic.experimental.missing_sentinel import, as with 2.12.
Generated form 2 (Pydantic 2.0.3+) Unset, 2.11, 2.12, 2.14
Constrained strings with --use-annotated (2.1+) Annotated[str, Field(...)] Annotated[str, StringConstraints(...)]
pydantic_v2.dataclass aliases that are not identifiers (2.4+) Field(validation_alias=..., serialization_alias=...) assignment Field(alias=...)
Deprecated fields (2.7+) Field(json_schema_extra={'deprecated': True}) Field(deprecated=True)
Models used as dictionary keys (2.8+) Defined before the models that use them as keys Schema order
--alias-generator field aliases (2.8+) alias= on every BaseModel field alias= only where the generated alias differs
Type alias patterns that need Python's regex engine (2.8+) String pattern; every model that reaches the alias, directly or through other models, sets regex_engine="python-re" Compiled re.compile(...) pattern
pydantic_v2.dataclass aliases of other models (2.10+) Alias: TypeAlias = Model Alias = TypeAliasType("Alias", Model)
Explicit aliases naming a BaseModel attribute (2.10+) Rejected when the attribute starts with model_ Rejected only when it starts with model_validate or model_dump
--use-annotated fields whose type is defined later in the module (2.11+) name: Type = Field(...) assignment name: Annotated[Type, Field(...)]

Some constructs have no older form and are written for every target. When the target is older than the release that accepts and applies such a construct, the generation warns once, for the output it writes, with the release the generated models need and the models and fields that need it:

UserWarning: The generated models need pydantic>=2.5, newer than the target Pydantic version 2: regex_engine needs 2.5
(Model).

The warning does not change the output or the exit status. An unset target, which stands for the newest release, never warns, and 2.14 has nothing to warn about.

Written for every target Needs Pydantic Written when
Field(union_mode=...) 2.2; 2.0 and 2.1 ignore it --union-mode on a union field
A pattern on a union of str and bytes 2.2; 2.0 and 2.1 reject it at import --use-annotated or --field-constraints with a pattern beside string and format: binary branches that --collapse-root-models inlines
coerce_numbers_to_str config key 2.4; older releases ignore it config in --extra-template-data
regex_engine config key 2.5; older releases ignore it a pattern needs Python's regex engine, or config in --extra-template-data
use_attribute_docstrings config key 2.7; older releases ignore it --use-attribute-docstrings, or config in --extra-template-data
A pattern compiled with re.compile 2.8 the combined pattern facets of XML Schema input
A type statement 2.10 --use-type-alias and the other aliases of pydantic_v2.BaseModel output with --target-python-version 3.12 or later
A TypeAliasType alias of a model or alias 2.10 --use-type-alias with pydantic_v2.BaseModel before Python 3.12 (pydantic_v2.dataclass writes Alias: TypeAlias = Model)
validate_by_name, validate_by_alias and serialize_by_alias config keys 2.11; older releases ignore them config in --extra-template-data (--allow-population-by-field-name follows the target)
A pattern on bytes 2.14; 2.11 to 2.13 ignore it and older releases reject it at import --use-annotated or --field-constraints with format: binary and pattern

The warning reads what the built-in templates write: a custom template that writes other constructs is not covered, and neither is a key of a pydantic_v2.dataclass config that has no row above. Regex syntax that only newer versions of the Rust engine accept, such as \b{start}, which Pydantic 2.0 to 2.4 rejects, is not covered either.

Config key naming Unset, 2 2.11, 2.12, 2.14
--allow-population-by-field-name populate_by_name=True validate_by_name=True

2.12 also enables options that need Pydantic 2.12, such as --use-missing-sentinel.

--use-missing-sentinel import Unset, 2.12 2.14
MISSING (2.14 deprecates the experimental path) from pydantic.experimental.missing_sentinel import MISSING from pydantic import MISSING
bytes patterns the Rust regex engine rejects Unset, 2, 2.11, 2.12 2.14
Model regex engine (2.14 compiles the patterns of plain and optional bytes fields, 2.11 to 2.13 ignore them) Default engine regex_engine="python-re"

The bytes rule is not applied to an unset target, because python-re changes how the other patterns of the model match on every Pydantic release. A bytes pattern with syntax such as a backreference then fails at import on Pydantic 2.14 unless --target-pydantic-version 2.14 is passed, and on Pydantic 2.12 for a bytes | MISSING field (--use-missing-sentinel), whose pattern 2.12 compiles as well. Pydantic 2.0 to 2.10 rejects any rendered bytes pattern at import. See Regular Expression Patterns.

pydantic_v2.dataclass required fields written as = Field(...) Unset, 2, 2.11, 2.12 2.14
Position when the schema lists them after a field with a default of the same class (2.13+ rejects that order in inspect.signature) Schema order Before the fields with defaults

A required pydantic_v2.dataclass field that needs Field(...), for an alias, a description or a constraint, is written as an assignment, so it can follow a field with a default:

@dataclass
class Pet:
    plain: str
    nick: str | None = "x"
    first_name: str = Field(..., alias="first-name")

With the target unset or older than 2.14, such a class keeps the order of datamodel-code-generator 0.83.0. It validates and constructs on Pydantic 2.4 and later, the first release that accepts the non-identifier alias, but on Pydantic 2.13 and later its signature cannot be read: inspect.signature(Pet) raises ValueError: non-default argument follows default argument, which affects tools that read the signature. On Python 3.10 with Pydantic 2.13 and later, a dataclass that inherits from such a class fails at import with the same error, because Python 3.10 reads the signature while it creates the subclass. Pass --target-pydantic-version 2.14 in either case: first_name is then written before nick. The generator warns about each class that keeps such an order, in its own fields or through its bases.

The order is not changed for an unset or older target, because positional arguments follow the field order: Pet("p", "a", "b") sets nick="a" in schema order and first_name="a" with 2.14. Keyword arguments work with both orders.

With --use-annotated a required field keeps its metadata in Annotated and comes before the fields with defaults on every target. A non-identifier alias of such a field is written as an assignment with 2 (see the 2.4 row above); the field then stays with the required fields, after those without an assignment. A required field that a subclass adds after an inherited field with a default is written with kw_only=True for every target.

Patterns that need Python's regex engine, such as lookaround assertions (see Regular Expression Patterns), rely on regex_engine="python-re", which Pydantic added in 2.5. With every target, a model that has such a pattern fails at import on Pydantic 2.0 to 2.4. A type alias (--use-type-alias, and the root schemas of pydantic_v2.dataclass output) has no config of its own:

  • With 2 its pattern stays a string, and every generated BaseModel or pydantic_v2.dataclass that reaches the alias, directly or through other models and their base classes, sets regex_engine="python-re", so the generated models validate on Pydantic 2.5 and later. A RootModel that reaches the alias is written as RootModel[Any] and keeps the alias in its root annotation, because Pydantic builds the generic before the config applies.
  • With 2, nothing selects the engine outside the generated models. A hand-written model that embeds a generated model or alias must set regex_engine="python-re" itself, and an alias validated on its own needs TypeAdapter(Alias, config=ConfigDict(regex_engine="python-re")). On Pydantic 2.8 to 2.10 this is a difference from an unset target, because no target lies between 2 and 2.11.
  • Unset, or with 2.11 and later, the alias carries a compiled re.compile(...) pattern, which needs Pydantic 2.8 and validates on its own.

The combined pattern constraints of XML Schema input stay compiled for every target, 2 included, and need Pydantic 2.8; see XML Schema limitations.

With 2 and --use-annotated, a BaseModel, RootModel or pydantic_v2.dataclass field whose type names a model defined later in the same module is written as an assignment, as it is without --use-annotated. Pydantic before 2.11 cannot evaluate such an annotation while it creates the class, and then mishandles the Field() inside Annotated[...]: depending on the release it drops the alias and the description, ignores validate_default, or fails on a constraint. Required dataclass fields written this way move after the other required fields. Fields that reference only their own model or earlier models, and output without deferred annotations (--disable-future-imports), keep the Annotated form.

For a field written this way, type checkers that read Field() assignments, such as pyright, take the alias as the constructor argument name and treat a positional default such as Field(None, alias=...) as a required argument. Add --use-default-kwarg to emit Field(default=None, ...) instead.

With 2, --alias-generator output names every alias in Field(alias=...). Type checkers that read the alias as the constructor argument name, such as pyright, then expect the alias instead of the field name; at runtime populate_by_name=True still accepts both.


dataclasses

Use --output-model-type dataclasses.dataclass

Python's built-in dataclasses for simple data containers without runtime validation.

datamodel-codegen --input schema.json --output-model-type dataclasses.dataclass --output model.py
from dataclasses import dataclass
from typing import Optional


@dataclass
class Pet:
    id: int
    name: str
    tag: Optional[str] = None

Options for dataclasses

Option Description
--frozen-dataclasses Generate immutable dataclasses (frozen=True)
--keyword-only Require keyword arguments (kw_only=True, Python 3.10+)
--dataclass-arguments Custom decorator arguments as JSON

The dataclass_arguments of one model's --extra-template-data, such as {"Pet": {"dataclass_arguments": {"frozen": true}}}, are merged over the arguments these options give it.

# Frozen, keyword-only dataclasses
datamodel-codegen --input schema.json --output-model-type dataclasses.dataclass \
  --frozen-dataclasses --keyword-only --target-python-version 3.10

When to use

  • Simple data structures without validation needs
  • Performance-critical code where validation overhead matters
  • Interoperability with code expecting dataclasses

TypedDict

Use --output-model-type typing.TypedDict

TypedDict provides static type checking for dictionary structures.

datamodel-codegen --input schema.json --output-model-type typing.TypedDict --output model.py
from typing import TypedDict, NotRequired


class Pet(TypedDict):
    id: int
    name: str
    tag: NotRequired[str]

When to use

  • Working with JSON APIs where data remains as dicts
  • Static type checking without runtime overhead
  • Gradual typing of existing dict-based code

Boundary payloads and partial updates

TypedDict is a good fit for data at an HTTP boundary. A PATCH payload can distinguish three cases:

  • the key is missing, so the existing value should stay unchanged
  • the key is present with null, so the value should be cleared
  • the key is present with a value, so the value should be replaced

Use --strict-nullable when the difference between a missing key and a nullable value matters:

datamodel-codegen --input user-patch.schema.json \
  --output-model-type typing.TypedDict \
  --strict-nullable \
  --target-python-version 3.13 \
  --output model.py
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "UserPatch",
  "type": "object",
  "required": ["id"],
  "properties": {
    "id": {"type": "string", "readOnly": true},
    "nickname": {"type": ["string", "null"]},
    "bio": {"type": "string"}
  },
  "additionalProperties": false
}
from typing import NotRequired
from typing_extensions import TypedDict


class UserPatch(TypedDict, closed=True):
    id: str
    nickname: NotRequired[str | None]
    bio: NotRequired[str]

Here nickname: NotRequired[str | None] means the key may be omitted, and None is still a real value when the key is present. This follows PEP 655, which added Required and NotRequired for per-key presence in TypedDict.

If the schema marks response-only fields with readOnly, combine this with --use-frozen-field to generate ReadOnly for TypedDict output:

datamodel-codegen --input user-patch.schema.json \
  --output-model-type typing.TypedDict \
  --strict-nullable \
  --use-frozen-field \
  --target-python-version 3.13 \
  --output model.py
from typing import NotRequired, ReadOnly
from typing_extensions import TypedDict


class UserPatch(TypedDict, closed=True):
    id: ReadOnly[str]
    nickname: NotRequired[str | None]
    bio: NotRequired[str]

ReadOnly comes from PEP 705. The closed=True output comes from PEP 728 and represents additionalProperties: false for type checkers that understand closed TypedDicts.

Background

The distinction between missing keys, None, and unset update arguments is covered in Koudai Aono's PyCon US 2026 talk Beyond Optional in Real-World Projects. In datamodel-code-generator today, TypedDict and msgspec preserve that boundary shape most directly. Pydantic models and dataclasses are usually a better fit after the boundary has been normalized into application data.


msgspec

Use --output-model-type msgspec.Struct

msgspec offers high-performance serialization with validation.

pip install msgspec
datamodel-codegen --input schema.json --output-model-type msgspec.Struct --output model.py
from msgspec import Struct, UNSET, UnsetType


class Pet(Struct):
    id: int
    name: str
    tag: str | UnsetType = UNSET

--untyped-value-type has no effect on msgspec.Struct output: msgspec decodes neither pydantic.JsonValue nor a recursive alias, and decodes Any into JSON values already, so untyped values stay Any. The other output types write them as the given type (see --untyped-value-type).

Discriminated unions

msgspec only decodes a union of structs when every struct is tagged, and a tagged struct cannot also have a field named like its tag_field. By default the output keeps the 0.83.0 form, in which a variant whose discriminator value comes from the discriminator mapping, an enum or the schema name keeps the discriminator as a field:

class Cat(Struct):
    petType: Literal["cat"]
    meow: str | UnsetType = UNSET

Cat(petType='cat'), cat.petType and decoding Cat on its own work, but decoding the union Cat | Dog raises msgspec's TypeError. That form only tags a variant whose discriminator property is a single literal such as a const, or one that a second discriminated union uses, and its tag_field is the generated field name, which differs from the property name when the field is renamed. A property named pet-kind is tagged by pet_kind, which msgspec looks for in the payload instead. --tag-msgspec-by-payload-name tags those structs by the property name of the document (pet-kind) and changes nothing else.

--use-msgspec-tagged-unions writes a union as tagged structs as a whole instead, with the property name of the document as tag_field:

class Cat(Struct, tag_field="petType", tag="cat"):
    meow: str | UnsetType = UNSET

The union then decodes, while petType is no longer a constructor argument or an attribute, and Cat decoded on its own accepts a payload without petType. When a variant cannot take exactly one tag for the union, or a base or derived struct of a variant keeps the property as a field, the union tags none of its variants, the ones with a const included, and a warning names the reason. A variant that another union tags keeps that tag.

A union that declares no discriminator is tagged when every branch requires one property holding one string or integer value written on the branch or in the schema its $ref names. With --infer-union-discriminators the tag is also read from the allOf members of a branch, and a null branch does not count, so those unions decode too; without it they are written untagged and warn that msgspec cannot decode them, as in 0.96.0.

Enums that list null

msgspec only decodes an Enum whose members are all str or all int values. By default the output keeps the 0.83.0 form of an enum that lists null without type: string or nullable: true, such as type: [integer, "null"] with enum: [1, 2, null], which is an Enum with a None member:

class Size(Enum):
    int_1 = 1
    int_2 = 2
    NoneType_None = None

Size.int_1 works, but decoding a struct with a Size field raises msgspec's TypeError. --use-msgspec-nullable-enum-alias writes it as a type: string or nullable enum is written instead, an Enum without the None member and an alias that adds None:

class SizeEnum(Enum):
    int_1 = 1
    int_2 = 2


Size: TypeAlias = SizeEnum | None

msgspec then decodes the members and null, while code that used Size.int_1 or Size.NoneType_None uses SizeEnum.int_1 and None. The default of a field of such an enum is then its raw value, also with --set-default-enum-member, and an enum that lists only null becomes None. An enum whose class has a value type base, the IntEnum of a type: integer enum or a --use-subclass-enum class such as (int, Enum), cannot hold a None member, so without the option it is written without that member and, as its one type rejects null, without the alias.

When to use

  • High-performance JSON/MessagePack serialization
  • Memory-efficient data structures
  • APIs with strict performance requirements

Enums that accept values they do not list

APIs add enum values over time. A closed Enum class rejects a payload that carries a value the generated models do not know yet, so --open-enums opens enums: extensible the enums whose schema marks them open (x-extensible-enum: true, or x-ms-enum with modelAsString: true), all every enum of two or more values except one whose schema closes it (x-extensible-enum: false, modelAsString: false).

Without more options an open enum is written as Literal[...] joined with the type of its schema, where --enum-field-as-literal all would write the Literal:

class JobStatus(RootModel[Literal["queued", "running", "done"] | str]):
    root: Literal["queued", "running", "done"] | str

A listed value parses as itself, the same value the Literal gives, and any other value of the type is accepted, by the rules of that type (int takes what a type: integer field takes). Keywords beside the enum, such as maxLength, are kept on the type for Pydantic; msgspec takes them only on a whole field, so there a value the enum does not list is not checked against them. The JSON schema of the models says anyOf: [{enum: [...]}, {type: string}]. This form works for every output model type and needs no code of your own; code that read .value of a member or compared with is JobStatus.queued compares with the string instead. An option that asks for an Enum class (--enum-field-as-literal none, an enum entry of --enum-field-as-literal-map, x-enum-field-as-literal: false) does not close an open enum: it is still the Literal form. The dated presets from 20261113 set open-enums = "extensible"; --open-enums none turns it off.

To keep the Enum classes, give the base classes: an open enum written as an Enum class takes the base --open-enum-base-class-map names for its class, Enum, StrEnum or IntEnum, and an open enum whose class has no entry is the Literal form. Each base is a member-less subclass of that class in your project whose _missing_ keeps a value it does not list; the generator writes no runtime code of its own. For example:

"""Open enum bases: an Enum that keeps a value it does not list as a member of its own."""

from __future__ import annotations

from enum import Enum, IntEnum, StrEnum


class OpenStrEnum(StrEnum):
    """A StrEnum that keeps a string it does not list."""

    @classmethod
    def _missing_(cls, value: object) -> OpenStrEnum | None:
        if not isinstance(value, str):
            return None
        member = str.__new__(cls, value)
        member._name_ = value
        member._value_ = value
        return cls._value2member_map_.setdefault(value, member)


class OpenIntEnum(IntEnum):
    """An IntEnum that keeps an integer it does not list."""

    @classmethod
    def _missing_(cls, value: object) -> OpenIntEnum | None:
        if not isinstance(value, int) or isinstance(value, bool):
            return None
        member = int.__new__(cls, value)
        member._name_ = f"VALUE_{value}"
        member._value_ = value
        return cls._value2member_map_.setdefault(value, member)


class OpenEnum(Enum):
    """An Enum that keeps a string it does not list; after a value type (str, int), a value of that type."""

    @classmethod
    def _missing_(cls, value: object) -> OpenEnum | None:
        member_type = cls._member_type_
        if not isinstance(value, str if member_type is object else member_type) or isinstance(value, bool):
            return None
        member = object.__new__(cls) if member_type is object else member_type.__new__(cls, value)
        member._name_ = str(value)
        member._value_ = value
        return cls._value2member_map_.setdefault(value, member)

with, in pyproject.toml:

[tool.datamodel-codegen]
open-enums = "all"
open-enum-base-class-map = {StrEnum = "myapi.open_enum.OpenStrEnum", IntEnum = "myapi.open_enum.OpenIntEnum", Enum = "myapi.open_enum.OpenEnum"}

class JobStatus(StrEnum) is then written as class JobStatus(OpenStrEnum): "queued" still parses to JobStatus.queued, while "paused" parses to a member of its own that dumps as "paused". OpenEnum is the entry for Enum, also under --use-subclass-enum, which writes class JobStatus(str, OpenEnum); Python 3.10 has no StrEnum, so string enums of a 3.10 target use the Enum entry. Each entry must subclass its key class and add no data type of its own, or the class fails at import. The cache keeps one member per value; a server that receives arbitrary values may drop the setdefault and accept members that are not equal.

What the class form validates depends on the output model type:

  • Pydantic v2 (models and dataclasses) validates through _missing_; the JSON schema of the models still lists only the known values.
  • msgspec honours _missing_ from msgspec 0.18.2 on; 0.18.0 and 0.18.1 reject the value.
  • dataclasses and TypedDict validate nothing; the class only lets you build JobStatus("paused") yourself.

Choosing the Right Type

graph TD
    A[Need runtime validation?] -->|Yes| B[Need best performance?]
    A -->|No| C[Need type hints for dicts?]
    B -->|Yes| D[msgspec.Struct]
    B -->|No| G[Prefer dataclass syntax?]
    G -->|Yes| H[pydantic_v2.dataclass]
    G -->|No| I[pydantic_v2.BaseModel]
    C -->|Yes| J[typing.TypedDict]
    C -->|No| K[dataclasses.dataclass]

Decision Guide

  1. API with validation → Pydantic v2
  2. Validation with dataclass syntax → Pydantic v2 dataclass
  3. High-performance serialization → msgspec
  4. Simple data containers → dataclasses
  5. Dict-based JSON handling → TypedDict

See Also