Skip to content
masterclass Buddy

Pydantic · Function

model_serializer

@model_serializer customizes how a whole Pydantic model is serialized. Use plain mode to replace the output, or wrap mode to modify Pydantic's normal output.

Last updated

Syntax

def model_serializer(f: _ModelPlainSerializerT, /) -> _ModelPlainSerializerT
def model_serializer(*, mode: Literal['wrap'], when_used: WhenUsed = 'always', return_type: TypeForm[Any] = ...) -> Callable
def model_serializer(*, mode: Literal['plain'] = ..., when_used: WhenUsed = 'always', return_type: TypeForm[Any] = ...) -> Callable

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • field_serializer
  • PlainSerializer
  • WrapSerializer
  • SerializationInfo
  • SerializerFunctionWrapHandler
  • BaseModel.model_dump

Source: Functional Serializers | Pydantic Docs. Checked 2026-11-03.