#11287·pydantic

`.model_dump()` coerces custom types to base class despite custom serializer

Author: groc-progCreated Jan 16, 2025Updated Sep 29, 2026
Labelsbug V2pending

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When dealing with custom types which inherit from a base type (for example a tuple), Pydantic's serialization logic always coerces the output to that underlying type when .model_dump() is called, even when adding a custom serializer for that class. The issue seems to only happen when inheriting from a base type.

Example Code

In the example code below I added a CustomType class which shows how the dumped model is serialized to a tuple. I would expect it to be serialized to a instance of CustomType instead.

python
from typing import Tuple

from pydantic import BaseModel
from pydantic_core import core_schema
from typing_extensions import Annotated


class CustomType(Tuple[int, int]):
    def __new__(cls, first: int = 0, second: int = 0):
        return tuple.__new__(cls, (first, second))

    def __repr__(self):
        return f"CustomType(first={self[0]}, second={self[1]})"


class CustomTypeAnnotation:
    @classmethod
    def __get_pydantic_core_schema__(cls, *_) -> core_schema.CoreSchema:
        def serialize(val: CustomType, handler, info):
            if info.mode_is_json():
                return [val[0], val[1]]

            return CustomType(val[0], val[1])

        return core_schema.json_or_python_schema(
            json_schema=core_schema.list_schema(items_schema=core_schema.int_schema()),
            python_schema=core_schema.is_instance_schema(CustomType),
            serialization=core_schema.wrap_serializer_function_ser_schema(serialize, info_arg=True),
        )


class Model(BaseModel):
    item: Annotated[CustomType, CustomTypeAnnotation]


model = Model(item=CustomType(10, 10))
assert model.model_dump_json() == '{"item":[10,10]}'

dumped = model.model_dump()
assert isinstance(dumped["item"], CustomType)

Python, Pydantic & OS Version

pydantic version: 2.12.5
        pydantic-core version: 2.41.5
          pydantic-core build: profile=release pgo=false
               python version: 3.13.11 (main, Dec  7 2025, 13:01:45) [GCC 15.2.1 20251112]
                     platform: Linux-6.18.3-arch1-1-x86_64-with-glibc2.42
             related packages: typing_extensions-4.15.0 pyright-1.1.407
                       commit: unknown