Backtestspydanticrun-20260930-144322#13460

Issue #13460 Merged fix PR #13487 Run 30 Sep 2026 · run-20260930-144322 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 fix the core problem the same way. They build a serialization schema from the annotated `source` type (int) and wrap it in a `wrap_serializer_function_ser_schema`, so dumping no longer checks against `list[int]` and the warning goes away. Both fall back to an any-schema when the source type cannot produce a schema.

Patch B is more complete in two ways:

1. **Serialization-mode JSON schema.** B passes `return_schema=source_schema` to the wrap serializer. Without it, JSON schema generation for a function-wrap serializer has no return schema and falls back to the validation schema (an array). B's test asserts that serialization mode gives `{'type': 'integer'}` and validation mode gives the array. Patch A omits `return_schema`, so its serialization JSON schema would likely still report `list[int]`.

2. **Test strength.** B's test runs the issue's exact reproduction with warnings turned into errors. That fails before the fix and directly checks the reported symptom. It also checks `model_dump`, `model_dump_json` and both JSON schema modes. A's first test only checks `model_dump_json` output. Before the fix that output was already `{"x":8}`, only with a warning, so the test would likely pass without the fix. A's second test (arbitrary-type fallback) is useful but only a small check.

On craft: A calls `handler(source)`, which re-applies the remaining annotation pipeline. B's `handler.generate_schema(source)` is cleaner. A catches the broader `GENERATE_SCHEMA_ERRORS`, which is a slightly more robust fallback than B's `PydanticSchemaGenerationError`. A also sets `schema['serialization']` by direct mutation instead of passing the constructor argument.

B's removal of two `pyright: ignore` comments in `PlainValidator` is unrelated churn. It could at most surface a type-checker complaint, with no runtime effect. That is a minor ding and does not outweigh B's advantages in correctness and testing.

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 26s12 turns

Wall-clock time in the container

Full test suite

No new failures

12,300 passed · 501 skipped · 12,801 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

48vs 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+46 −2
pydantic/functional_validators.pyindex 6a38f619..85fa6e15 100644@@ -235,14 +235,14 @@ class PlainValidator:             func = cast(core_schema.WithInfoValidatorFunction, self.func)             return core_schema.with_info_plain_validator_function(                 func,-                serialization=serialization,  # pyright: ignore[reportArgumentType]+                serialization=serialization,                 json_schema_input_schema=input_schema,             )         else:             func = cast(core_schema.NoInfoValidatorFunction, self.func)             return core_schema.no_info_plain_validator_function(                 func,-                serialization=serialization,  # pyright: ignore[reportArgumentType]+                serialization=serialization,                 json_schema_input_schema=input_schema,             ) @@ -894,8 +894,20 @@ class ValidateAs:         self.instantiation_hook = instantiation_hook      def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+        from pydantic import PydanticSchemaGenerationError+         schema = handler(self.from_type)+        # Validated values are of the `source` type, so serialize them using its schema+        # (and not the `from_type` one). Fall back to runtime inference if unsupported.+        try:+            source_schema = handler.generate_schema(source)+            serialization = core_schema.wrap_serializer_function_ser_schema(+                function=lambda v, h: h(v), schema=source_schema, return_schema=source_schema+            )+        except PydanticSchemaGenerationError:+            serialization = core_schema.simple_ser_schema('any')         return core_schema.no_info_after_validator_function(             self.instantiation_hook,             schema=schema,+            serialization=serialization,         )tests/test_types.pyindex e70a0102..b660c6f7 100644@@ -6096,6 +6096,38 @@ def test_validate_from() -> None:     assert ta.validate_python({'a': 1}) == Arbitrary(1)  +def test_validate_as_serialization() -> None:+    """https://github.com/pydantic/pydantic/discussions/13232.++    Serialization should use the schema of the annotated (source) type,+    not the one of the type provided to `ValidateAs`.+    """++    def first_even(nums: list[int]) -> int:+        for i in nums:+            if i % 2 == 0:+                return i+        raise ValueError('Even not found.')++    class Foo(BaseModel):+        x: Annotated[int, ValidateAs(list[int], first_even)]++    with warnings.catch_warnings():+        warnings.simplefilter('error')++        d = Foo(x=[1, 3, 5, 8, 6, 7])+        assert d.x == 8+        assert d.model_dump() == {'x': 8}+        assert d.model_dump_json() == '{"x":8}'++    assert Foo.model_json_schema(mode='validation')['properties']['x'] == {+        'items': {'type': 'integer'},+        'title': 'X',+        'type': 'array',+    }+    assert Foo.model_json_schema(mode='serialization')['properties']['x'] == {'title': 'X', 'type': 'integer'}++ @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. 

What the agent reported

I fixed the ValidateAs bug: model_dump() now returns {'x': 8} with no warning, and the regression test for your example passes. Only the two files below are changed, left uncommitted.

What was wrong: ValidateAs.__get_pydantic_core_schema__ (in pydantic/functional_validators.py) built the whole core schema from from_type (list[int]). The value that actually gets stored is the annotated type (int), so the serializer expected a list, got an int, and raised PydanticSerializationUnexpectedValue. The serialization-mode JSON schema was also wrong: it described x as an array instead of an integer.

What I changed:

  • The fix: ValidateAs now serializes using the schema of the annotated type. This follows the pattern PlainValidator already uses in the same file.
  • Unsupported annotated types: if pydantic can't build a schema for the annotated type (for example an arbitrary class, as in the existing test_validate_from), serialization falls back to working out the type from the value at runtime.
  • Reviewer's point: the reviewer found that reusing the annotated type's own serializer could break for custom types with wrap or include/exclude serializers. I changed it to always serialize through the annotated type's full schema, which avoids that.
  • New test: test_validate_as_serialization in tests/test_types.py runs your exact example with warnings turned into errors. It checks d.x == 8, model_dump() == {'x': 8} and model_dump_json() == '{"x":8}'. It also checks that the JSON schema is an array of integers in validation mode and an integer in serialization mode.

Test evidence:

  • Before the fix: the new test failed at model_dump() with PydanticSerializationUnexpectedValue(Expected list[int] ... input_value=8, input_type=int).
  • After the fix, targeted run: 9 passed, covering the new test, test_validate_from and the docstring examples in functional_validators.py.
  • After the fix, wider run: ./bt-test tests/test_types.py tests/test_validators.py tests/test_serialize.py tests/test_json_schema.py gave 1738 passed, 1 failed, 7 skipped, 4 xfailed.
  • The one failure: test_v1_validator_deprecated also fails with my changes stashed. It compares file paths (/work/... against /workspace/...), so it's a mismatch in the test environment, not something this fix caused.

Risks I didn't address:

  • A custom class whose own __get_pydantic_core_schema__ delegates to ValidateAs(..., cls) would now recurse forever, because building the annotated type's schema calls itself again. The docs don't show this pattern.
  • Errors other than "can't build a schema" for the annotated type, such as an unresolved forward reference, now show up when the model is defined, not later.

Requirements:

  1. No serialization warning for ValidateAs in the reported scenario: done; the new test turns warnings into errors and passes.
  2. model_dump() returns {'x': 8}: done; asserted, along with model_dump_json().
  3. Serialization-mode JSON schema describes the stored type: done; asserted integer in serialization mode and array in validation mode.