Backtestspydanticrun-20261022-024030#13460

Issue #13460 Merged fix PR #13487 Run 22 Oct 2026 · run-20261022-024030 pydantic/pydantic

Unexpected serialization schema with ValidateAs

Better

Our patch was judged better than the merged fix.

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

Both patches share the same core fix: build the serializer from `handler(source)` (falling back to `any_schema()` on GENERATE_SCHEMA_ERRORS) and attach it via a wrap serializer, so the validated `int` is no longer serialized as `list[int]`. Independent tests confirm both resolve the reported warning. Patch B goes further on correctness in two ways that matter for the same field: (1) it adds a `pydantic_js_annotation_functions` hook so `model_json_schema(mode='serialization')` reports the source type (`integer`) rather than falling through to the inner `list[int]` schema, which is what Patch A would produce since `GenerateJsonSchema.ser_schema` ignores a function-wrap ser schema without `return_schema`; (2) it handles the `is-instance` schema produced under `arbitrary_types_allowed`, where the serializer should infer and JSON schema generation would otherwise fail. Patch B's tests are also stronger: they use `warnings.simplefilter('error')` so the regression would fail loudly, and they cover the issue example, a model source type, a custom serializer composed with ValidateAs, arbitrary source types with and without `arbitrary_types_allowed`, and both JSON schema modes. Patch A's tests only check `model_dump_json` output without asserting the warning is gone, and it incidentally renames an existing test. Patch A leaves a TODO comment rather than addressing the JSON schema consequence. B's extra scope is tightly related to the fix and adds little risk.

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

7m 21s

Wall-clock time in the container

Full test suite

No new failures

1,314 passed · 10 skipped · 1,324 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

134vs 56 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

From https://github.com/pydantic/pydantic/discussions/13232:

import pydantic as p
import typing as t

def first_even(nums: list[int]) -> int:
    for i in nums:
        if i%2 == 0:
            return i
    raise ValueError("Even not found.")

class Foo(p.BaseModel):
    x: t.Annotated[int, p.ValidateAs(list[int], first_even)]

d = Foo(x=[1, 3, 5, 8, 6, 7])
print(d)
# x=8
print(d.model_dump())
# PydanticSerializationUnexpectedValue(Expected `list[int]` - serialized value may not be as expected [field_name='x', input_value=8, input_type=int])
# {'x': 8}

Example Code

Python, Pydantic & OS Version

main

The two patches

Our patch+133 −1
pydantic/functional_validators.pyindex 6a38f619..471c50ca 100644@@ -13,7 +13,7 @@ from pydantic_core import PydanticUndefined, core_schema from typing_extensions import Self  from ._internal import _decorators, _generics-from .annotated_handlers import GetCoreSchemaHandler+from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler from .errors import PydanticUserError from .version import version_short from .warnings import ArbitraryTypeWarning, PydanticDeprecatedSince212@@ -330,6 +330,7 @@ class WrapValidator:   if TYPE_CHECKING:+    from .json_schema import JsonSchemaValue      class _OnlyValueValidatorClsMethod(Protocol):         def __call__(self, cls: Any, value: Any, /) -> Any: ...@@ -894,8 +895,32 @@ class ValidateAs:         self.instantiation_hook = instantiation_hook      def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+        from pydantic._internal._generate_schema import GENERATE_SCHEMA_ERRORS+         schema = handler(self.from_type)++        # The validated value is an instance of the annotated (source) type, not of `from_type`,+        # so serialization must follow the source type's schema rather than the `from_type` one:+        try:+            serialization_schema = handler(source)+        except GENERATE_SCHEMA_ERRORS:+            # The source type isn't natively supported (e.g. an arbitrary class),+            # so fall back to inferring the serialization at runtime:+            serialization_schema = core_schema.any_schema()+        else:+            if serialization_schema['type'] == 'is-instance':+                # Arbitrary types (with `arbitrary_types_allowed`) are also serialized by inference,+                # and an `is-instance` schema can't be used to generate a JSON Schema:+                serialization_schema = core_schema.any_schema()++        def js_function(cs: core_schema.CoreSchema, js_handler: GetJsonSchemaHandler) -> JsonSchemaValue:+            return js_handler(serialization_schema) if js_handler.mode == 'serialization' else js_handler(cs)+         return core_schema.no_info_after_validator_function(             self.instantiation_hook,             schema=schema,+            metadata={'pydantic_js_annotation_functions': [js_function]},+            serialization=core_schema.wrap_serializer_function_ser_schema(+                function=lambda v, h: h(v), schema=serialization_schema+            ),         )tests/test_types.pyindex e70a0102..d963f28b 100644@@ -6096,6 +6096,113 @@ def test_validate_from() -> None:     assert ta.validate_python({'a': 1}) == Arbitrary(1)  +def test_validate_as_serialization_uses_source_type() -> None:+    """https://github.com/pydantic/pydantic/issues/13460."""++    def first_even(nums: list[int]) -> int:+        for i in nums:+            if i % 2 == 0:+                return i+        raise ValueError('Even not found.')++    class Model(BaseModel):+        x: Annotated[int, ValidateAs(list[int], first_even)]++    m = Model(x=[1, 3, 5, 8, 6, 7])+    assert m.x == 8++    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert m.model_dump() == {'x': 8}+        assert m.model_dump_json() == '{"x":8}'++    assert Model.model_json_schema(mode='validation')['properties']['x'] == {+        'items': {'type': 'integer'},+        'title': 'X',+        'type': 'array',+    }+    assert Model.model_json_schema(mode='serialization')['properties']['x'] == {'title': 'X', 'type': 'integer'}+++def test_validate_as_serialization_source_type_custom_serializer() -> None:+    class Model(BaseModel):+        x: Annotated[+            int,+            PlainSerializer(lambda v: f'int:{v}'),+            ValidateAs(str, lambda v: len(v)),+        ]++    m = Model(x='abc')+    assert m.x == 3++    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert m.model_dump() == {'x': 'int:3'}+        assert m.model_dump_json() == '{"x":"int:3"}'+++def test_validate_as_serialization_model_source_type() -> None:+    class Inner(BaseModel):+        a: int++    class Model(BaseModel):+        x: Annotated[Inner, ValidateAs(int, lambda v: Inner(a=v))]++    m = Model(x=1)+    assert m.x == Inner(a=1)++    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert m.model_dump() == {'x': {'a': 1}}+        assert m.model_dump_json() == '{"x":{"a":1}}'++    assert Model.model_json_schema(mode='validation')['properties']['x'] == {'title': 'X', 'type': 'integer'}+    assert Model.model_json_schema(mode='serialization')['properties']['x'] == {+        'properties': {'a': {'title': 'A', 'type': 'integer'}},+        'required': ['a'],+        'title': 'Inner',+        'type': 'object',+    }+++def test_validate_as_serialization_arbitrary_source_type() -> None:+    class Arbitrary:+        def __init__(self, a: int) -> None:+            self.a = a++    class ArbitraryModel(BaseModel):+        a: int++    ta = TypeAdapter(Annotated[Arbitrary, ValidateAs(ArbitraryModel, instantiation_hook=lambda v: Arbitrary(a=v.a))])+    value = ta.validate_python({'a': 1})++    with warnings.catch_warnings():+        warnings.simplefilter('error')+        # The source type isn't supported natively, so serialization is inferred (and doesn't+        # attempt to serialize the value as `ArbitraryModel`):+        assert ta.dump_python(value) is value+++def test_validate_as_serialization_arbitrary_source_type_allowed() -> None:+    class Arbitrary:+        def __init__(self, a: int) -> None:+            self.a = a++    class Model(BaseModel):+        model_config = ConfigDict(arbitrary_types_allowed=True)++        x: Annotated[Arbitrary, ValidateAs(int, instantiation_hook=lambda v: Arbitrary(a=v))]++    m = Model(x=1)++    with warnings.catch_warnings():+        warnings.simplefilter('error')+        assert m.model_dump()['x'] is m.x++    assert Model.model_json_schema(mode='validation')['properties']['x'] == {'title': 'X', 'type': 'integer'}+    assert Model.model_json_schema(mode='serialization')['properties']['x'] == {'title': 'X'}++ @pytest.mark.skipif(sys.version_info < (3, 12), reason="`Annotated` doesn't allow instances in <3.12") def test_skip_validation_arbitrary_type_object() -> None:     """https://github.com/pydantic/pydantic/issues/11997. 
Merged fix · PR #13487+52 −4
pydantic/functional_validators.pyindex 6a38f6195..336eff24d 100644@@ -794,6 +794,7 @@ else:          @classmethod         def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+            # Lazy import to avoid cycle:             from pydantic._internal._generate_schema import GENERATE_SCHEMA_ERRORS              # use the generic _origin_ as the second argument to isinstance when appropriate@@ -894,8 +895,24 @@ class ValidateAs:         self.instantiation_hook = instantiation_hook      def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:-        schema = handler(self.from_type)-        return core_schema.no_info_after_validator_function(+        # Lazy import to avoid cycle:+        from pydantic._internal._generate_schema import GENERATE_SCHEMA_ERRORS++        from_type_schema = handler(self.from_type)+        schema = core_schema.no_info_after_validator_function(             self.instantiation_hook,-            schema=schema,+            schema=from_type_schema,+        )+        try:+            ser_schema = handler(source)+        except GENERATE_SCHEMA_ERRORS:+            ser_schema = core_schema.any_schema()++        # TODO: pydantic-core currently doesn't accept it, but we should be able to provide an+        # arbitrary core schema for serialization (currently `simple_ser_schema()` can be used,+        # but only allow specifying a specific `'type`). Working around this with a wrap serializer+        # is already done in other parts of the code base, so grep for similar cases when fixing:+        schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(+            function=lambda v, h: h(v), schema=ser_schema         )+        return schematests/test_types.pyindex e70a0102e..df4d5f060 100644@@ -6080,7 +6080,7 @@ def test_skip_validation_json_schema():     }  -def test_validate_from() -> None:+def test_validate_as() -> None:     class Arbitrary:         def __init__(self, a: int) -> None:             self.a = a@@ -6096,6 +6096,37 @@ def test_validate_from() -> None:     assert ta.validate_python({'a': 1}) == Arbitrary(1)  +def test_validate_as_known_serialization() -> None:+    """https://github.com/pydantic/pydantic/issues/13460"""++    def first_even(nums: list[int]) -> int:+        for i in nums:+            if i % 2 == 0:+                return i+        raise ValueError('Even not found.')++    class Model(BaseModel):+        x: Annotated[int, ValidateAs(list[int], first_even)]++    m = Model(x=[1, 2])++    assert m.model_dump_json() == '{"x":2}'+++def test_validate_as_unknown_serialization() -> None:++    class Arbitrary:+        def __init__(self, a: int) -> None:+            self.a = a++        def __eq__(self, other) -> bool:+            return self.a == other.a++    ta = TypeAdapter(Annotated[Arbitrary, ValidateAs(int, instantiation_hook=Arbitrary)])++    assert ta.dump_python(Arbitrary(1)) == Arbitrary(1)++ @pytest.mark.skipif(sys.version_info < (3, 12), reason="`Annotated` doesn't allow instances in <3.12") def test_skip_validation_arbitrary_type_object() -> None:     """https://github.com/pydantic/pydantic/issues/11997.