PythiloLesson 17 of 20 · progress kept in this browser

Serialize Python values for JSON

A model can contain values that JSON has no native representation for, such as dates, UUIDs or sets. A normal Python dump preserves many of those useful Python objects. A JSON-mode dump converts them to a representation JSON can encode.

Choose the format at the boundary where it is needed. Keep date objects for calculations and produce text when writing a response or file. model_dump_json() returns JSON text, while model_dump(mode="json") returns JSON-compatible Python data. These are different return contracts even when they represent the same information.

from datetime import date
from pydantic import BaseModel
class Event(BaseModel):
    day: date
item = Event(day="2026-01-02")
print(type(item.model_dump()["day"]).__name__)
print(item.model_dump(mode="json"))
Your task

Implement event_json(raw) with Event(day: date, name: str). Return a JSON-compatible dictionary whose day is ISO date text. Reject an impossible date.

The first run loads pydantic into this browser.