Skip to main content

How to Create a pydantic Model with BaseModel

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A pydantic model is a class that checks its own data. You list the fields and their types, and pydantic converts what it can, such as the text "412" into the number 412, and refuses what it can't. This guide builds a small model with pydantic 2.14.4 on Python 3.15.2.

Syntax​

Syntax
from pydantic import BaseModel


class <Model>(BaseModel):
<field>: <type>
<field>: <type>


<item> = <Model>(<field>=<value>, <field>=<value>)
PartWhat it means
class <Model>(BaseModel)A new model that inherits pydantic's checking from BaseModel
<field>: <type>One field and the type its value must have, such as title: str
<Model>(<field>=<value>, ...)Creates an item. Values go in as keyword arguments

The models guide puts it in one sentence: "Models are simply classes which inherit from BaseModel and define fields as annotated attributes."

The Example​

A Book has a title, which is text, and a number of pages. The script creates a book with the page count written as text, "412", as it would arrive from a form or a file.

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​

book.py
from pydantic import BaseModel


class Book(BaseModel):
title: str
pages: int


book = Book(title="Dune", pages="412")
print(book)
print(book.pages + 1)

The book.py script in a macOS-style code window: a Book model with title: str and pages: int, a book created with pages="412", and two prints.

Step 2: Run It​

Terminal
uv run python book.py
Output
title='Dune' pages=412
413

The terminal after uv run python book.py: title='Dune' pages=412, then 413.

The text "412" became the number 412: there are no quotes around it in the first line, and book.pages + 1 added up to 413. The command works unchanged in PowerShell.

When the Data Is Wrong​

pydantic raises a ValidationError that names the field. With pages="many":

Output
1 validation error for Book
pages
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='many', input_type=str]
For further information visit https://errors.pydantic.dev/2.14/v/int_parsing

And with no pages at all:

Output
1 validation error for Book
pages
Field required [type=missing, input_value={'title': 'Dune'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.14/v/missing

Both came from wrapping the call in try: and except ValidationError as error: print(error).

Common Mistakes​

MistakeWhat happensDo this instead
Passing values by position: Book("Dune", 412)TypeError: BaseModel.__init__() takes 1 positional argument but 3 were givenName each field: Book(title="Dune", pages=412)
Leaving out a field that has no defaultField required [type=missing ...]Pass a value, or give the field a default: pages: int = 0

Troubleshooting​

SymptomLikely causeFix
unable to parse string as an integerThe text isn't a whole number, such as "many" or "12.5"Send a number, or change the field's type

FAQ​

Does pydantic convert every type it's given?​

It converts values that clearly mean the type, such as "412" for an int. Text like "many" fails. Strict mode turns the conversion off, so that only a real int is accepted.

How do I read a field?​

As an attribute: book.title, book.pages.

Conclusion​

class Book(BaseModel) with two annotated fields gave a model that turned "412" into 412 and named the field in its error whenever a value didn't fit.

Additional Resources​