By Kevin McAleer, 7 Minutes
Page last updated November 24, 2026

Ahoy there makers! By the end of this lesson you will be able to explain what Pydantic does, and run a first model that accepts a good sensor message and rejects a bad one.
Picture a Raspberry Pi Pico in your greenhouse, sending a temperature reading to your laptop every few seconds. Most of the time the message is fine. Sometimes the battery sags, a wire wobbles and you get "hot" where a number should be, or a reading is missing entirely. If your code trusts every message, one bad reading crashes your dashboard at 3am. Pydantic is the library that stops that from happening.
Pydantic checks that data has the shape you promised. You describe the data once, as a Python class with type hints, and Pydantic does the rest: it checks every field, converts values where it safely can (the string "21.5" becomes the float 21.5), and gives you a clear error when it cannot.
This course teaches Pydantic v2, the current major version. We will build up from a single sensor reading to a small web API that validates robot data, and we will run every example for real.
flowchart LR
A["Pico sends JSON"] --> B["Pydantic model"]
B -->|valid| C["Typed Python object"]
B -->|invalid| D["ValidationError with details"]
C --> E["Dashboard, database or API"]
After completing this course, you will:
ValidationError and report what was wrong and whereField, field_validator and model_validatorpydantic-settingsfastapi package. The FastAPI course goes deeper if you want itEvery lesson teaches one idea, shows a complete program you can copy and run, and ends with exercises and a short quiz. Code blocks are real programs, and the output shown underneath each one was copied from an actual run, so if yours differs, something is worth investigating.
This is computer-side Python, not MicroPython. Pydantic is too big to run on a Pico, so the Pico sends data and your laptop, Raspberry Pi or server validates it.
You will meet older tutorials that use the Pydantic v1 names. Here is the translation, once, so you can read them:
| Pydantic v1 (old) | Pydantic v2 (this course) |
|---|---|
Model.parse_obj(data) |
Model.model_validate(data) |
Model.parse_raw(text) |
Model.model_validate_json(text) |
model.dict() |
model.model_dump() |
model.json() |
model.model_dump_json() |
@validator("field") |
@field_validator("field") with @classmethod |
@root_validator |
@model_validator |
class Config: inside the model |
model_config = ConfigDict(...) |
Teachers: This course suits students who already write Python classes, around age 14 and up. Each lesson takes 30 to 45 minutes. Each learner needs only a computer with Python and internet access to install packages. Lessons 1 to 6 and 10 to 12 need no hardware at all. If school machines block
pip, ask your IT team to allow a virtual environment or use a shared one prepared in advance.
Letβs build the greenhouse example. Save this as first_model.py:
from pydantic import BaseModel, ValidationError
class Reading(BaseModel):
sensor: str
temperature_c: float
battery_mv: int
good = '{"sensor": "pico-greenhouse", "temperature_c": 21.5, "battery_mv": 3920}'
bad = '{"sensor": "pico-greenhouse", "temperature_c": "hot", "battery_mv": 3920}'
reading = Reading.model_validate_json(good)
print(reading)
print(reading.temperature_c + 1)
try:
Reading.model_validate_json(bad)
except ValidationError as err:
print(err)
Running it prints:
sensor='pico-greenhouse' temperature_c=21.5 battery_mv=3920
22.5
1 validation error for Reading
temperature_c
Input should be a valid number, unable to parse string as a number [type=float_parsing, input_value='hot', input_type=str]
For further information visit https://errors.pydantic.dev/2.14/v/float_parsing
What it does: the class lists three fields and their types. model_validate_json reads the JSON text, checks it against those types and hands you a proper object. Because temperature_c is now a real float, reading.temperature_c + 1 works.
Why this matters: the error names the field (temperature_c), the problem and the bad value ('hot'). That is exactly what you need to fix a sensor, and you wrote no checking code at all.
Common mistakes:
humidity: int field to Reading, then run the good message again. What error do you get, and why?battery_mv in the good message to the string "3920". Does it still pass? Print type(reading.battery_mv)."temperature_c": null. Which error type does Pydantic report?ModuleNotFoundError: No module named 'pydantic'.python3 -m pip install pydantic.Why: Each Python install and virtual environment has its own packages, so a package installed elsewhere is invisible here. Lesson 2 shows how to set this up properly.
AttributeError: type object 'Reading' has no attribute 'parse_raw' or a warning about deprecated methods.model_validate_json.Quick quiz 1 of 4
What does Pydantic use to decide what a valid value looks like?
Quick quiz 2 of 4
What happens when you validate "temperature_c": "hot" against a float field?
Quick quiz 3 of 4
Where should Pydantic run in a Pico sensor project?
Quick quiz 4 of 4
Which v2 method replaces the v1 parse_obj?
parse_obj became model_validate in v2, and dict() became model_dump().
Comments