Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 38 ยท Serialization

TypeAdapter, Dataclasses and TypedDict

Validate and serialize any Python type without a model using TypeAdapter, add validation to standard dataclasses and TypedDict, and choose between models and dataclasses.

So far every example used a BaseModel. But you will often want to validate something that is not a model: a list of integers from a file, a union of types, a TypedDict from a library, or an existing dataclass. Pydantic handles all of these with the same engine. This lesson closes the chapter on serialization by showing how to validate and dump any type.

TypeAdapter: validation for any type

TypeAdapter(T) wraps a type annotation and gives it the same methods a model has: validate_python, validate_json, dump_python, dump_json and json_schema.

Python Validating plain types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

TypeAdapter is the right tool for things such as "validate this JSON list of numbers", "parse an environment variable into a set of strings", or "check a function argument", without inventing a model class just for the purpose.

Create a TypeAdapter once (at module level) and reuse it. Building one compiles the validator, which costs far more than using it. Creating an adapter inside a loop or a request handler is a common performance mistake.

Adapters with models and unions

The type can be anything Pydantic understands, including a list of models or a discriminated union (Unions and Literals):

Python A list of models from JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is the standard way to parse a top-level JSON array, because a model has to be an object ({...}), not a list.

Dataclasses with validation

If you already use standard dataclasses, Pydantic can validate them without turning them into models. Import dataclass from pydantic.dataclasses (or wrap the standard one with a TypeAdapter).

Python pydantic.dataclasses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This gives you validation with the familiar dataclass syntax, positional arguments included, and still a normal dataclass underneath (dataclasses.asdict and fields work).

TypedDict

A TypedDict describes the keys of a plain dictionary. Python does not enforce it; Pydantic does, through a TypeAdapter:

Python Validating a TypedDict

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result is still a plain dict, which is exactly what you want when you must keep returning dictionaries (for instance to a library that expects them). On older Python versions, import TypedDict from typing_extensions for the best support.

Models, dataclasses or TypedDict?

BaseModel pydantic dataclass TypedDict + TypeAdapter
Instance type model object dataclass object plain dict
Validation on creation yes yes only through the adapter
model_dump, schema, copy built in via TypeAdapter via TypeAdapter
Config options model_config config=ConfigDict(...) __pydantic_config__
Best for most application data, APIs when dataclass syntax or compatibility is needed dictionary-based code you cannot change

When in doubt choose BaseModel: it has the largest feature set and the best documentation, and it is what FastAPI expects.

Serializing any value

TypeAdapter.dump_python(value, mode="json") and dump_json(value) work for any supported type, including dataclasses and typed dictionaries, with the same options as the model methods (Dumping to Dicts and JSON): include, exclude, by_alias, exclude_none, indent and more.

Python Dump a dataclass to JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you validate a JSON array of integers without defining a model?

    TypeAdapter wraps any type, including list[int], and offers validate_json.
  2. 2. Why should a TypeAdapter be created once at module level?

    Building the validator is the expensive part. Reuse the adapter.
  3. 3. What type of object does TypeAdapter(SomeTypedDict).validate_python(data) return?

    TypedDict instances are ordinary dictionaries; Pydantic validates them without changing their type.
  4. 4. Which import gives a dataclass that validates on creation?

    pydantic.dataclasses.dataclass is a drop-in replacement for the standard decorator that adds validation.

Summary

  • TypeAdapter(T) validates, dumps and describes any type: lists, dicts, unions, models, dataclasses, TypedDicts.
  • Create adapters once and reuse them; use them for top-level arrays and non-model data.
  • pydantic.dataclasses.dataclass adds validation to dataclasses; TypeAdapter validates TypedDict into plain dicts.
  • Prefer BaseModel unless you need dataclass syntax or must keep plain dictionaries.
  • The dump options you learned for models (include, exclude, by_alias, exclude_none) also work on adapters.

Next: head to the Pydantic track page for the following chapters: settings, custom validators, FastAPI and projects.