Backtestspydanticrun-20261013-104308#11862

Issue #11862 Merged fix PR #13506 Run 13 Oct 2026 · run-20261013-104308 pydantic/pydantic

Cannot use model_dump(include=...) with @model_serializer (raises PydanticOmit)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Patch B fixes the root cause. A `SerializationInfo` passed as the second argument to the wrap handler was being used as a filter key, which caused `PydanticOmit` when `include` was set. Patch B now treats it as if no index key was given. As a result, the issue's exact example (`handler(self, info)` with `include={'bar'}`) works and returns the expected filtered output. Its tests would fail before the change and pass after it. They cover the core-level wrap serializer, `model_serializer`, and `field_serializer`, across include and exclude and both python and json modes. It also updates the `SerializerFunctionWrapHandler` protocol type to match the accepted usage.

Patch A does not make the issue's code work. It only swaps the confusing `PydanticOmit` for a `TypeError`. Its type check runs whenever an `index_key` is supplied, before any filter lookup. So `handler(value, info)` would now raise even when no `include`/`exclude` is given. That breaks a pattern that previously worked and that pydantic's own `WrapSerializer` docstring used; Patch A has to rewrite that docstring example, which confirms the breakage. Its single test only checks the new error message for an odd `bytes` key and does not exercise the reported scenario.

On correctness, regression risk and test relevance, Patch B is clearly better.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

4m 53s

Wall-clock time in the container

Full test suite

No new failures

2,976 passed · 235 skipped · 3,211 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

64vs 80 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Attempting to use the include parameter to model_dump when the model has a @model_serializer method causes a PydanticOmit exception to be returned.

Example Code

from pydantic import BaseModel, model_serializer

class Foo(BaseModel):
    bar:int
    baz:int

    @model_serializer(mode='wrap')
    def _serialize(self, next_serializer, info):
        return next_serializer(self, info)

f = Foo(bar=123, baz=456)

print(f.model_dump(include={'bar'}))

Output:

Traceback (most recent call last):
  File "/home/phemmer/.config/JetBrains/PyCharm2025.1/scratches/scratch_12.py", line 9, in _serialize
    return next_serializer(self, info)
pydantic_core._pydantic_core.PydanticOmit: PydanticOmit()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/phemmer/.config/JetBrains/PyCharm2025.1/scratches/scratch_12.py", line 13, in <module>
    print(f.model_dump(include={'bar'}))
          ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/phemmer/.local/lib/python3.13/site-packages/pydantic/main.py", line 463, in model_dump
    return self.__pydantic_serializer__.to_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self,
        ^^^^^
    ...<11 lines>...
        serialize_as_any=serialize_as_any,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.PydanticSerializationError: Error calling function `_serialize`: PydanticOmit: PydanticOmit()

Python, Pydantic & OS Version

pydantic version: 2.11.4
        pydantic-core version: 2.33.2
          pydantic-core build: profile=release pgo=false
               python version: 3.13.2 (main, Feb  4 2025, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
                     platform: Linux-6.10.4-200.fc40.x86_64-x86_64-with-glibc2.39
             related packages: typing_extensions-4.12.2 pydantic-settings-2.8.1 fastapi-0.115.9
                       commit: unknown

The two patches

Our patch+63 −1
pydantic-core/python/pydantic_core/core_schema.pyindex 85f6a8c0..e26d25d6 100644@@ -352,7 +352,9 @@ def plain_serializer_function_ser_schema(   class SerializerFunctionWrapHandler(Protocol):  # pragma: no cover-    def __call__(self, input_value: Any, index_key: int | str | None = None, /) -> Any: ...+    def __call__(+        self, input_value: Any, index_key: int | str | SerializationInfo[Any] | None = None, /+    ) -> Any: ...   # (input_value: Any, serializer: SerializerFunctionWrapHandler, /) -> Anypydantic-core/src/serializers/type_serializers/function.rsindex 705c1e12..451ef275 100644@@ -473,6 +473,11 @@ impl SerializationCallable {          let state = &mut self.extra_owned.to_state(py); +        // A common pattern is `handler(value, info)`, passing the `SerializationInfo` through;+        // it isn't a valid index key, so treat it as if no index key was provided+        // rather than filtering on it (which would omit the value when `include` is set).+        let index_key = index_key.filter(|key| !key.is_instance_of::<SerializationInfo>());+         if let Some(index_key) = index_key {             let filter = if let Ok(index) = index_key.extract::<usize>() {                 self.filter.index_filter(index, state, None)?pydantic-core/tests/serializers/test_functions.pyindex b6bd900e..43118edb 100644@@ -507,6 +507,27 @@ def test_custom_mapping():     assert s.to_json({'a': 1, 'b': 2}, exclude={'b'}) == b'"a=1"'  +@pytest.mark.parametrize('mode', ['python', 'json'])+def test_wrap_handler_called_with_info(mode):+    # passing `info` through to the handler is a common pattern, it must not be treated as an index key+    s = SchemaSerializer(+        core_schema.typed_dict_schema(+            {+                'a': core_schema.typed_dict_field(core_schema.int_schema()),+                'b': core_schema.typed_dict_field(core_schema.int_schema()),+            },+            serialization=core_schema.wrap_serializer_function_ser_schema(+                lambda v, handler, info: handler(v, info), info_arg=True+            ),+        )+    )+    assert s.to_python({'a': 1, 'b': 2}, mode=mode) == {'a': 1, 'b': 2}+    assert s.to_python({'a': 1, 'b': 2}, mode=mode, include={'a'}) == {'a': 1}+    assert s.to_python({'a': 1, 'b': 2}, mode=mode, exclude={'a'}) == {'b': 2}+    assert s.to_json({'a': 1, 'b': 2}, include={'a'}) == b'{"a":1}'+    assert s.to_json({'a': 1, 'b': 2}, exclude={'a'}) == b'{"b":2}'++ def test_function_wrap_model():     calls = 0 tests/test_serialize.pyindex b55e49d7..7851c7fb 100644@@ -513,6 +513,40 @@ def test_model_serializer_wrap_info():     assert m.model_dump_json(exclude={'a'}) == '{"b":"boom","info":"mode=json exclude={\'a\'}"}'  +def test_model_serializer_wrap_handler_called_with_info() -> None:+    """https://github.com/pydantic/pydantic/issues/11859"""++    class Model(BaseModel):+        bar: int+        baz: int++        @model_serializer(mode='wrap')+        def _serialize(self, handler, info: SerializationInfo):+            return handler(self, info)++    m = Model(bar=123, baz=456)+    assert m.model_dump() == {'bar': 123, 'baz': 456}+    assert m.model_dump(include={'bar'}) == {'bar': 123}+    assert m.model_dump(exclude={'bar'}) == {'baz': 456}+    assert m.model_dump(mode='json', include={'bar'}) == {'bar': 123}+    assert m.model_dump_json(include={'bar'}) == '{"bar":123}'+    assert m.model_dump_json(exclude={'bar'}) == '{"baz":456}'+++def test_field_wrap_serializer_handler_called_with_info() -> None:+    class Model(BaseModel):+        sub: dict[str, int]++        @field_serializer('sub', mode='wrap')+        def _serialize_sub(self, value, handler, info: SerializationInfo):+            return handler(value, info)++    m = Model(sub={'a': 1, 'b': 2})+    assert m.model_dump(include={'sub': {'a'}}) == {'sub': {'a': 1}}+    assert m.model_dump_json(include={'sub': {'a'}}) == '{"sub":{"a":1}}'+    assert m.model_dump(exclude={'sub': {'a'}}) == {'sub': {'b': 2}}++ def test_model_serializer_plain_json_return_type():     class MyModel(BaseModel):         a: int 
Merged fix · PR #13506+47 −33
pydantic-core/src/serializers/type_serializers/function.rsindex 705c1e129..5944310cc 100644@@ -2,20 +2,20 @@ use std::borrow::Cow; use std::sync::Arc;  use pyo3::PyTraverseError;-use pyo3::exceptions::{PyAttributeError, PyRecursionError, PyRuntimeError};+use pyo3::exceptions::{PyAttributeError, PyRecursionError, PyRuntimeError, PyTypeError}; use pyo3::gc::PyVisit; use pyo3::intern; use pyo3::prelude::*; use pyo3::types::PyDict; -use pyo3::types::PyString;+use pyo3::types::{PyInt, PyString};  use crate::definitions::DefinitionsBuilder; use crate::py_gc::PyGcTraverse; use crate::serializers::SerializationState; use crate::serializers::extra::IncludeExclude; use crate::tools::SchemaDict;-use crate::tools::{function_name, py_err, py_error_type};+use crate::tools::{function_name, py_err, py_error_type, safe_repr}; use crate::{PydanticOmit, PydanticSerializationUnexpectedValue};  use super::format::WhenUsed;@@ -476,8 +476,13 @@ impl SerializationCallable {         if let Some(index_key) = index_key {             let filter = if let Ok(index) = index_key.extract::<usize>() {                 self.filter.index_filter(index, state, None)?-            } else {+            } else if index_key.is_instance_of::<PyString>() || index_key.is_instance_of::<PyInt>() {                 self.filter.key_filter(index_key, state)?+            } else {+                return Err(PyTypeError::new_err(format!(+                    "'index_key' is expected to be an integer or a string, got '{}'",+                    safe_repr(index_key),+                )));             };             if let Some(next_include_exclude) = filter {                 let state = &mut state.scoped_include_exclude(next_include_exclude);pydantic/functional_serializers.pyindex 662a8c3a4..aad3a8b8d 100644@@ -97,50 +97,41 @@ class WrapSerializer:     from datetime import datetime, timezone     from typing import Annotated, Any -    from pydantic import BaseModel, WrapSerializer--    class EventDatetime(BaseModel):-        start: datetime-        end: datetime+    from pydantic import (+        BaseModel,+        SerializationInfo,+        SerializerFunctionWrapHandler,+        WrapSerializer,+    ) -    def convert_to_utc(value: Any, handler, info) -> dict[str, datetime]:+    def convert_to_utc(+        value: Any, handler: SerializerFunctionWrapHandler, info: SerializationInfo+    ):         # Note that `handler` can actually help serialize the `value` for         # further custom serialization in case it's a subclass.-        partial_result = handler(value, info)+        serialized = handler(value)         if info.mode == 'json':-            return {-                k: datetime.fromisoformat(v).astimezone(timezone.utc)-                for k, v in partial_result.items()-            }-        return {k: v.astimezone(timezone.utc) for k, v in partial_result.items()}+            return datetime.fromisoformat(serialized).astimezone(timezone.utc)+        else:+            return serialized.astimezone(timezone.utc) -    UTCEventDatetime = Annotated[EventDatetime, WrapSerializer(convert_to_utc)]+    UTCDatetime = Annotated[datetime, WrapSerializer(convert_to_utc)]      class EventModel(BaseModel):-        event_datetime: UTCEventDatetime+        event_datetime: UTCDatetime -    dt = EventDatetime(-        start='2024-01-01T07:00:00-08:00', end='2024-01-03T20:00:00+06:00'-    )-    event = EventModel(event_datetime=dt)+    event = EventModel(event_datetime='2024-01-01T07:00:00-08:00')     print(event.model_dump())     '''     {-        'event_datetime': {-            'start': datetime.datetime(-                2024, 1, 1, 15, 0, tzinfo=datetime.timezone.utc-            ),-            'end': datetime.datetime(-                2024, 1, 3, 14, 0, tzinfo=datetime.timezone.utc-            ),-        }+        'event_datetime': datetime.datetime(+            2024, 1, 1, 15, 0, tzinfo=datetime.timezone.utc+        )     }     '''      print(event.model_dump_json())-    '''-    {"event_datetime":{"start":"2024-01-01T15:00:00Z","end":"2024-01-03T14:00:00Z"}}-    '''+    #> {"event_datetime":"2024-01-01T15:00:00Z"}     ```      Attributes:tests/test_serialize.pyindex b55e49d72..57832ca0a 100644@@ -1419,3 +1419,21 @@ def test_serialization_info_properties() -> None:         'round_trip',     ):         assert getattr(m.model_dump(**{property: True})['f'], property) is True, f'{property!r} check failed'+++def test_model_serializer_index_key_type_error() -> None:+    """https://github.com/pydantic/pydantic/issues/11862"""++    class Model(BaseModel):+        f: int++        @model_serializer(mode='wrap')+        def _serialize(self, handler: SerializerFunctionWrapHandler):+            return handler(self, b'not_a_valid_type_for_index_key')++    m = Model(f=1)+    with pytest.raises(+        PydanticSerializationError,+        match="Error calling function `_serialize`: TypeError: 'index_key' is expected to be an integer or a string, got 'b'not_a_valid_type_for_index_key''",+    ):+        m.model_dump(include={'f'})