from datetime import datetime from typing import Annotated from pydantic import PlainSerializer # SQLite strips tzinfo on read-back, so naive datetimes from DB are actually UTC. # This serializer appends "Z" so browsers parse them correctly as UTC. UTCDatetime = Annotated[ datetime, PlainSerializer( lambda dt: (dt.isoformat() + "Z") if dt.tzinfo is None else dt.isoformat(), return_type=str, when_used="json", ), ]