#11812·pydantic

duplicate use of the model's wrap serializer

Author: ornarieceCreated Apr 29, 2025Updated Sep 27, 2026
Labelsbug V2topic-validators-serializers

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

this was introduced by 2.11(.x?). this is probably related to #11505.

python
import pydantic
import pydantic_core


class MyModel(pydantic.RootModel):
    root: str

    @pydantic.model_serializer(mode="wrap")
    def my_wrapper_serializer(self, serializer: pydantic.SerializerFunctionWrapHandler):
        return "my_prefix:" + serializer(self)

    @classmethod
    def __get_pydantic_core_schema__(cls, source, handler):
        return pydantic_core.core_schema.union_schema(
            [
                handler(cls),
                # anything goes here, using duplicate schema for the example
                handler(cls),
            ]
        )


class MyParentModel(pydantic.BaseModel):
    nested: MyModel


if __name__ == "__main__":
    my_parent_model = MyParentModel.model_validate({'nested': 'value'})

    print(my_parent_model.model_dump())
    #>expected: {'nested': 'my_prefix:value'}
    #>actual:   {'nested': 'my_prefix:my_prefix:value'}

Example Code

python

Python, Pydantic & OS Version

pydantic version: 2.11.3
pydantic-core version: 2.33.1
pydantic-core build: profile=release pgo=false
python version: 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)]
platform: Windows-10-10.0.19045-SP0
related packages: mypy-1.15.0 typing_extensions-4.13.2
commit: unknown