Skip to main content

How to Turn a pydantic Model into a Dictionary with model_dump

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A pydantic model checks your data, but other code often wants a plain dictionary: to save it, to pass it on with **, or to send it as JSON. model_dump() gives you that dictionary, with the values already converted. This guide uses it with pydantic 2.14.4 on Python 3.15.2.

Syntax​

Syntax
<dictionary> = <item>.model_dump()
<dictionary> = <item>.model_dump(exclude={"<field>"})
<json text> = <item>.model_dump_json()
PartWhat it means
<item>An instance of a pydantic model
model_dump()Returns a dict with one key per field
exclude={"<field>"}Optional. Leaves those fields out
model_dump_json()Returns the same data as JSON text

pydantic's serialization guide says that "Pydantic models (and model-like types such as dataclasses) will be (recursively) converted to dictionaries. This is achievable by using the model_dump() method".

The Example​

A Settings model has a theme and a font_size. The font size arrives as text, "14", and the script turns the checked settings back into a dictionary.

Prerequisites​

  • pydantic in your project, for example with uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.

Step 1: Write the Script​

dump.py
from pydantic import BaseModel


class Settings(BaseModel):
theme: str
font_size: int


settings = Settings(theme="dark", font_size="14")
data = settings.model_dump()

print(data)
print(type(data).__name__)

The dump.py script in a macOS-style code window: a Settings model with theme and font_size, settings created with font_size="14", data = settings.model_dump() and two prints.

Step 2: Run It​

Terminal
uv run python dump.py
Output
{'theme': 'dark', 'font_size': 14}
dict

The terminal after uv run python dump.py: the dictionary with theme dark and font_size 14, then dict.

The dictionary holds the checked values: font_size is the number 14, not the text "14" it came in as. The command works unchanged in PowerShell.

Leave Fields Out, or Get JSON​

Settings(theme="dark", font_size=14).model_dump(exclude={"theme"}) and .model_dump_json() printed:

Output
{'font_size': 14}
{"theme":"dark","font_size":14}

exclude dropped theme. model_dump_json() returned JSON text, with double quotes and no spaces.

Common Mistakes​

MistakeWhat happensDo this instead
Calling it on the class: Settings.model_dump()TypeError: BaseModel.model_dump() missing 1 required positional argument: 'self'Call it on an instance: settings.model_dump()
Expecting a JSON string from model_dump()It returns a Python dictUse model_dump_json() for text

Troubleshooting​

SymptomLikely causeFix
A field you didn't want is in the dictionaryAll fields are included by defaultPass exclude={"<field>"}

FAQ​

What happens to nested models?​

They become dictionaries too: the guide says models are "(recursively) converted to dictionaries".

Conclusion​

settings.model_dump() returned {'theme': 'dark', 'font_size': 14}, a plain dict with the checked values. Use exclude to drop fields and model_dump_json() for JSON text.

Additional Resources​

  • Serialization — model_dump, model_dump_json and their options.
  • Models — the models guide.