Backtestspydanticrun-20261022-024030#11862
Cannot use model_dump(include=...) with @model_serializer (raises PydanticOmit)
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The issue's ask is that `model_dump(include=...)` work when a wrap `@model_serializer` calls `handler(self, info)`, a call shape the base repo's own `WrapSerializer` docstring demonstrates. Patch B addresses this directly: in `SerializationCallable.__call__` it drops an `index_key` that is a `SerializationInfo` instance before applying include/exclude filtering, so the issue's exact example returns `{'bar': 123}`, and exclude/JSON variants also work. The independent tests confirm this (all four pass on B, all fail on base). B also corrects the docstring to `handler(value)` and adds tests at both the pydantic-core level (typed_dict with wrap serializer, include/exclude/json) and the pydantic level (model_serializer and field_serializer), all of which fail on base and pass with the fix.
Patch A takes the position that passing `info` to the handler is user error: it keeps the `PydanticOmit` path replaced by a `TypeError` for non-int/non-str index keys and rewrites the docstring example to stop passing `info`. That is a reasonable hardening and the error message is clearer than a stray `PydanticOmit`, but it does not resolve the reported scenario: the issue's code still raises (now a `TypeError`), which the independent tests show (all four FAIL-EXC on A). Its only test checks the new error text with a bytes key, not the issue's behaviour. A also rewrites a large chunk of the docstring example unrelated to the fix.
On correctness B resolves the issue at the point where the bad key is interpreted and remains backward-compatible with previously documented usage; A changes the symptom without making the documented call work. B's tests are far more relevant. B wins clearly.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
4m 25s
Wall-clock time in the container
Full test suite
No new failures
795 passed · 91 skipped · 886 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
68vs 80 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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: unknownThe two patches
pydantic-core/src/serializers/type_serializers/function.rsindex 705c1e12..aef1a91d 100644@@ -473,6 +473,11 @@ impl SerializationCallable { let state = &mut self.extra_owned.to_state(py); + // A common mistake is to call `handler(value, info)`, passing the `SerializationInfo`+ // as the second argument; it is not a valid index or key, so treat it as if no+ // `index_key` was given rather than applying include/exclude filtering to it.+ 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..3131b68b 100644@@ -480,6 +480,28 @@ def test_deque(): assert s.to_json(deque([1, 2, 3]), exclude={2}) == b'[1,2]' +def test_wrap_handler_called_with_info():+ """Passing the `SerializationInfo` to the handler must not be treated as an `index_key`."""++ def serialize(value, serializer, info: core_schema.SerializationInfo):+ return serializer(value, info)++ 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(serialize, info_arg=True),+ )+ )+ assert s.to_python({'a': 1, 'b': 2}) == {'a': 1, 'b': 2}+ assert s.to_python({'a': 1, 'b': 2}, include={'a'}) == {'a': 1}+ assert s.to_python({'a': 1, 'b': 2}, exclude={'a'}) == {'b': 2}+ assert s.to_python({'a': 1, 'b': 2}, mode='json', include={'a'}) == {'a': 1}+ assert s.to_json({'a': 1, 'b': 2}, include={'a'}) == b'{"a":1}'++ def test_custom_mapping(): def serialize_custom_mapping(value, serializer, _info): items = {}pydantic/functional_serializers.pyindex 662a8c3a..6fe4d68c 100644@@ -106,7 +106,7 @@ class WrapSerializer: def convert_to_utc(value: Any, handler, info) -> dict[str, datetime]: # Note that `handler` can actually help serialize the `value` for # further custom serialization in case it's a subclass.- partial_result = handler(value, info)+ partial_result = handler(value) if info.mode == 'json': return { k: datetime.fromisoformat(v).astimezone(timezone.utc)tests/test_serialize.pyindex b55e49d7..43a6c970 100644@@ -513,6 +513,45 @@ 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:+ """Passing `info` as the second argument to the handler must not be treated as an `index_key`."""++ class MyModel(BaseModel):+ bar: int+ baz: int++ @model_serializer(mode='wrap')+ def _serialize(self, handler, info: SerializationInfo):+ return handler(self, info)++ m = MyModel(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 Inner(BaseModel):+ x: int+ y: int++ class Outer(BaseModel):+ inner: Inner++ @field_serializer('inner', mode='wrap')+ def _ser_inner(self, value, handler, info: FieldSerializationInfo):+ return handler(value, info)++ m = Outer(inner=Inner(x=1, y=2))+ assert m.model_dump() == {'inner': {'x': 1, 'y': 2}}+ assert m.model_dump(include={'inner': {'x'}}) == {'inner': {'x': 1}}+ assert m.model_dump(exclude={'inner': {'x'}}) == {'inner': {'y': 2}}+ assert m.model_dump_json(include={'inner': {'x'}}) == '{"inner":{"x":1}}'++ def test_model_serializer_plain_json_return_type(): class MyModel(BaseModel): a: int
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'})