Backtestspydanticrun-20261005-183002#13460
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 issue the same way: they give the ValidateAs after-validator a serialization schema built from the annotated (source) type, wrapped in a pass-through wrap serializer. Neither keeps the from_type serializer.
Correctness: B is more careful. It builds the source schema with `handler.generate_schema(source)`, so annotations listed before ValidateAs are not applied a second time; its test checks that JSON-schema examples are not duplicated. It keeps an existing custom serializer such as a preceding PlainSerializer. It skips the wrapper when the source schema equals the inner schema, and it silences ArbitraryTypeWarning while probing. A calls `handler(source)`, which re-runs the earlier annotations and can apply them twice. A also always installs a wrap serializer, even when it is unnecessary.
Scope: B also applies the same fix to pipeline `transform`, which has the identical bug. That is extra scope, but it is coherent, low-risk and tested.
Tests: A's main test only checks `model_dump_json() == '{"x":2}'`. That likely passes even before the fix, because pydantic-core warns and then falls back to inference, so the test does not really catch the regression. B runs its assertions with warnings turned into errors, so its tests fail before the fix and pass after. B also covers the arbitrary-type fallback, a dataclass target, the PlainSerializer case, non-duplication of annotations, and the pipeline cases.
Overall B is the more robust fix with meaningfully better tests.
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
21m 15s
Wall-clock time in the container
Full test suite
No new failures
12,306 passed · 501 skipped · 12,807 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
125vs 56 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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
mainThe two patches
Our patch+121 −4
pydantic/experimental/pipeline.pyindex 80615684..7547bf51 100644@@ -22,6 +22,7 @@ from pydantic_core import PydanticCustomError from pydantic_core import core_schema as cs from pydantic import Strict+from pydantic.functional_validators import _output_type_serialization __all__ = ['validate_as', 'validate_as_deferred', 'transform'] @@ -422,7 +423,7 @@ def _apply_step(step: _Step, s: cs.CoreSchema | None, handler: GetCoreSchemaHand elif isinstance(step, _ValidateAsDefer): s = _apply_parse(s, step.tp, False, handler, source_type) elif isinstance(step, _Transform):- s = _apply_transform(s, step.func, handler)+ s = _apply_transform(s, step.func, handler, source_type) elif isinstance(step, _Constraint): s = _apply_constraint(s, step.constraint) elif isinstance(step, _PipelineOr):@@ -457,7 +458,7 @@ def _apply_parse( def _apply_transform(- s: cs.CoreSchema | None, func: Callable[[Any], Any], handler: GetCoreSchemaHandler+ s: cs.CoreSchema | None, func: Callable[[Any], Any], handler: GetCoreSchemaHandler, source_type: Any ) -> cs.CoreSchema: if s is None: return cs.no_info_plain_validator_function(func)@@ -476,7 +477,11 @@ def _apply_transform( s['to_upper'] = True return s - return cs.no_info_after_validator_function(func, s)+ # The transformed value may not be of the type `s` validates, so don't inherit its serializer.+ # If the transform is the last step, its output is the pipeline's output, i.e. the annotated type:+ return cs.no_info_after_validator_function(+ func, s, serialization=_output_type_serialization(source_type, s, handler)+ ) def _apply_constraint( # noqa: C901pydantic/functional_validators.pyindex 6a38f619..ee62c29e 100644@@ -14,7 +14,7 @@ from typing_extensions import Self from ._internal import _decorators, _generics from .annotated_handlers import GetCoreSchemaHandler-from .errors import PydanticUserError+from .errors import PydanticSchemaGenerationError, PydanticUserError from .version import version_short from .warnings import ArbitraryTypeWarning, PydanticDeprecatedSince212 @@ -898,4 +898,32 @@ class ValidateAs: return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=_output_type_serialization(source, schema, handler), )+++def _output_type_serialization(+ source: Any, inner_schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler+) -> core_schema.SerSchema | None:+ """Build a serialization schema for values of type `source`.++ This is used when the validation schema (`inner_schema`) is built from a type different from the annotated+ type (e.g. with `ValidateAs`), so that serialization is driven by the annotated type instead+ of the input type. If no schema can be generated for `source` (e.g. an unsupported custom type),+ the serializer falls back to inferring the serialization from the value.++ `None` is returned (i.e. the serializer of `inner_schema` is kept) if `inner_schema` already has a custom+ serializer (e.g. from a `PlainSerializer` annotation), or if it is the same as the schema of `source`.+ """+ if 'serialization' in inner_schema:+ return None+ try:+ with warnings.catch_warnings():+ warnings.simplefilter('ignore', ArbitraryTypeWarning)+ # Use `generate_schema()` so that the other annotations are not applied (again):+ source_schema = handler.generate_schema(source)+ except PydanticSchemaGenerationError:+ return core_schema.simple_ser_schema('any')+ if source_schema == inner_schema:+ return None+ return core_schema.wrap_serializer_function_ser_schema(function=lambda v, h: h(v), schema=source_schema)tests/test_pipeline.pyindex 7198d430..4c91d604 100644@@ -3,6 +3,7 @@ from __future__ import annotations import datetime+import warnings from collections.abc import Callable from decimal import Decimal from typing import Annotated, Any@@ -422,6 +423,28 @@ def test_json_schema( assert schema == expected_ser_schema +@pytest.mark.parametrize(+ 'model, input_value, expected',+ [+ (Annotated[int, validate_as(list[int]).transform(lambda v: v[0])], [3, 4], 3),+ (Annotated[int, validate_as(str).transform(len)], 'abc', 3),+ (Annotated[list[str], validate_as(str).transform(str.split)], 'a b', ['a', 'b']),+ (Annotated[int, validate_as(int) | validate_as(str).transform(len)], 'abc', 3),+ ],+ ids=['list-to-int', 'str-to-int', 'str-to-list', 'union'],+)+def test_transform_serialization(model: Any, input_value: Any, expected: Any) -> None:+ """The output of a transform is serialized according to the annotated type, not the transform's input type."""+ ta = TypeAdapter(model)+ value = ta.validate_python(input_value)+ assert value == expected++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) == expected+ assert ta.dump_python(value, mode='json') == expected++ def test_transform_first_step() -> None: """Check that when transform() is used as the first step in a pipeline it run after parsing.""" ta = TypeAdapter[int](Annotated[int, transform(lambda x: x + 1)])tests/test_types.pyindex e70a0102..f0d48731 100644@@ -6096,6 +6096,67 @@ def test_validate_from() -> None: assert ta.validate_python({'a': 1}) == Arbitrary(1) +def test_validate_as_serialization_uses_annotated_type() -> None:+ """https://github.com/pydantic/pydantic/discussions/13232"""++ def first_even(nums: list[int]) -> int:+ return next(i for i in nums if i % 2 == 0)++ 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}'+++def test_validate_as_serialization_custom_annotated_type() -> None:+ @dataclass+ class Point:+ x: int+ y: int++ 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})++ # No schema can be generated for `Arbitrary`, serialization is inferred from the value:+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) is value++ ta_point = TypeAdapter(Annotated[Point, ValidateAs(tuple[int, int], lambda t: Point(*t))])+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta_point.dump_python(ta_point.validate_python([1, 2])) == {'x': 1, 'y': 2}+++def test_validate_as_serialization_other_annotations() -> None:+ class Examples:+ def __get_pydantic_json_schema__(self, cs, handler):+ json_schema = handler(cs)+ json_schema.setdefault('examples', []).append(1)+ return json_schema++ # Annotations before `ValidateAs` are only applied once:+ ta = TypeAdapter(Annotated[int, Examples(), ValidateAs(int, lambda v: v)])+ assert ta.json_schema() == {'examples': [1], 'type': 'integer'}++ # A custom serializer applied before `ValidateAs` is kept:+ ta = TypeAdapter(Annotated[int, PlainSerializer(lambda v: f'v{v}'), ValidateAs(str, int)])+ assert ta.dump_python(ta.validate_python('5')) == 'v5'++ @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.