Pydantic Computed Fields: Serialize Derived Values

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().
Why it exists
Pydantic models define a data shape. But often, you need to expose values that are derived from that data, not stored directly. Before computed fields, you would have to manually add these derived values after serialization, which is clumsy and error-prone.
The mental model
Think of @computed_field as a promotion. It takes a regular Python property—a method that acts like an attribute—and promotes it to be a full-fledged member of the Pydantic model's exportable data. It is the bridge between your object's internal logic and its external JSON representation.
How it works
You apply the @computed_field decorator to a method on your Pydantic BaseModel. For best results and IDE support, you should stack it on top of a @property or @cached_property decorator. When you call .model_dump() or .model_dump_json(), Pydantic executes that method and includes its return value in the output. You can also pass arguments to @computed_field to control its behavior, like setting an alias for serialization or using repr=False to hide it from the model's string representation.
When to use it
Use computed fields when you need to include a value in your serialized output that is calculated from other fields, like a full_name from first_name and last_name. It's also perfect for expensive operations when combined with @cached_property, ensuring a costly database lookup or calculation runs only once per instance.
When not to use it
Avoid computed fields for values that are fundamental inputs to your model. If a value is required to create the object and isn't derived from other inputs, it should be a regular Pydantic field. Also, be wary of creating complex, hidden dependencies inside computed fields that make the model's behavior hard to predict.
One canonical example
A Rectangle model has width and length as integer fields. A method area(self) which returns self.width * self.length is decorated first with @property and then with @computed_field. When you create an instance like Rectangle(width=5, length=10) and call .model_dump(), the output dictionary is {'width': 5, 'length': 10, 'area': 50}, automatically including the calculated area.
Interview question
What is the primary purpose of using a Pydantic @computed_field?
- a.To define a field that must be explicitly passed as an argument during model instantiation.
- b.To automatically include a value derived from other fields in the model's serialized output.Correct
- c.To enable runtime validation of complex business logic within a model's methods.
- d.To prevent a model's internal properties from being exposed in its external representation.
Why? this is the answer
A computed field promotes a derived value to be part of the model's exportable data, automatically including it in the serialized output. It is not for defining fundamental inputs, which should be regular Pydantic fields.
Just read this? Test yourself on what you have been reading.
Read the original → pydantic.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles