new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Pydantic · all subjects

serialization

3 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Three serialization modes in Pydantic

Pydantic provides three serialization modes: 1) To a Python dict made up of associated Python objects, 2) To a Python dict made up only of jsonable types, 3) To a JSON string. In all three modes, output can be customized by excluding specific fields, excluding unset fields, excluding default values, and excluding None values.

Serialization example with model_dump methods

Example showing Pydantic serialization in three ways: from datetime import datetime from pydantic import BaseModel class Meeting(BaseModel): when: datetime where: bytes why: str = 'No idea' m = Meeting(when='2020-01-01T12:00', where='home') print(m.model_dump(exclude_unset=True)) #> {'when': datetime.datetime(2020, 1, 1, 12, 0), 'where': b'home'} print(m.model_dump(exclude={'where'}, mode='json')) #> {'when': '2020-01-01T12:00:00', 'why': 'No idea'} print(m.model_dump_json(exclude_defaults=True)) #> {"when":"2020-01-01T12:00:00","where":"home"}

JSON parsing with sensible type conversion

Pydantic can parse and validate JSON in one step, allowing sensible data conversion (e.g., when parsing strings into datetime objects) while maintaining strict type validation. Since JSON parsing is implemented in Rust, it is also very performant.

Give your agent this brain