tezvyn:

Pydantic Computed Fields: Serialize Derived Values

Source: pydantic.devadvanced

A Pydantic computed field makes a derived value, like an area from width and length, part of your model's serialized output. Use it to include calculated attributes when calling `.model_dump()`.

Pydantic's `@computed_field` treats a method like a real field during serialization, making derived values part of your data model. It's ideal for including values calculated from other fields or for caching expensive computations so they run only once. The main pitfall is omitting the `@property` decorator, which can break IDE autocompletion and confuse static type checkers.

Read the original → pydantic.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Pydantic Computed Fields: Serialize Derived Values · Tezvyn