Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Model Methods: Dump, Copy and Fields

A practical tour of the Pydantic v2 model API: model_dump, model_dump_json, model_copy, model_fields, model_fields_set, model_construct and equality, plus v1 name mapping.

Once you have a model instance, you will constantly move data in and out of it. This lesson is a practical tour of the methods and attributes you will use every day, with a short mapping from the old Pydantic v1 names, which you will still meet in older code and tutorials.

Looking inside a model

A model class knows its own structure. model_fields is a dictionary from field name to a FieldInfo object with the annotation, default, and metadata:

Python Inspecting fields

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.

model_fields_set is more useful than it looks. It lets you tell "the client sent null" from "the client sent nothing", which matters for PATCH-style API updates.

model_dump: from model to dict

model_dump() returns a plain dict (nested models become nested dicts). Its keyword options are worth knowing, and you will see them in more detail in Dumping to Dicts and JSON:

Python model_dump options

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.

model_dump_json: from model to JSON text

model_dump_json() returns a JSON str directly, and handles types that json.dumps cannot: dates, decimals, UUIDs, sets, enums and more.

Python JSON for free

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.

Compare with json.dumps(inv.model_dump()), which would fail with TypeError: Object of type date is not JSON serializable.

model_copy: change without mutating

model_copy() returns a new instance. update={...} replaces fields in the copy, and deep=True copies nested objects too. Because copying with update does not validate, it is quick, but you carry the responsibility for correct values.

Python Copy with changes

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.

model_construct: skip validation

Model.model_construct(**values) builds an instance without validating. It exists for performance when the data has already been validated (for example loaded back from your own cache). Wrong types go in silently, so avoid it for any outside data.

Equality and hashing

Two models are equal when they are of the same class and have equal field values. By default models are not hashable because they are mutable. Make a model frozen=True to get immutability and a hash, so it can sit in sets and be a dict key:

Python Equality and frozen models

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.

Pydantic v1 to v2 name map

You will find v1 code everywhere. The old names still work in v2 but emit deprecation warnings; use the new ones.

Pydantic v1 Pydantic v2
Model.parse_obj(d) Model.model_validate(d)
Model.parse_raw(s) Model.model_validate_json(s)
m.dict() m.model_dump()
m.json() m.model_dump_json()
m.copy() m.model_copy()
Model.construct() Model.model_construct()
Model.schema() Model.model_json_schema()
Model.__fields__ Model.model_fields
class Config: model_config = ConfigDict(...)
@validator @field_validator

If you inherit code that still uses class Config:, you can migrate it gradually. Pydantic also publishes a migration helper called bump-pydantic, and a full migration guide in its documentation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does model_fields_set contain?

    It records which fields the caller supplied, which lets you distinguish 'not sent' from 'sent as the default value'.
  2. 2. Why does json.dumps(model.model_dump()) often fail while model.model_dump_json() works?

    model_dump returns Python objects. model_dump_json knows how to turn dates, decimals, UUIDs and more into JSON.
  3. 3. What is true about model_copy(update={'x': 'bad'})?

    update values are trusted. Rebuild with model_validate if you need validation.
  4. 4. How do you make model instances hashable and immutable?

    frozen=True forbids assignment and generates hash from the field values.

Summary

  • model_fields describes the structure; model_fields_set shows which fields were provided.
  • model_dump() gives a dict, model_dump_json() gives a JSON string (and handles dates, decimals, UUIDs).
  • model_copy(update=..., deep=...) copies without validating; model_construct builds without validating.
  • Models compare by value; frozen models are immutable and hashable.
  • v1 names (dict, json, parse_obj, Config) are deprecated; use the model_* API and ConfigDict.

Next: Field Types in Pydantic