Backtestspydanticrun-20260923-170312#13460
Unexpected serialization schema with ValidateAs
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches diagnose the root cause identically: the serialization schema was built from `from_type` (list[int]) while the validated output is of `source` type (int), so both rebuild the ser schema from the output type. The decisive differences are in tests, craft, and scope. Patch B's test wraps model_dump/model_dump_json in `warnings.simplefilter('error')`, so it genuinely fails before the fix (the PydanticSerializationUnexpectedValue warning is raised as an error) and passes after — a real regression test. Patch A's tests only assert value equality (`model_dump_json() == '{"x":2}'`, `dump_python(...) == Arbitrary(1)`); but the issue shows the value was already correct ({'x': 8}) and only a warning was emitted, so Patch A's assertions pass both before and after the change and do not actually verify the bug is fixed — tautological per the 'weak tests count for little' rule. On craft, Patch B passes `serialization=` directly to `no_info_after_validator_function` (idiomatic), whereas Patch A mutates `schema['serialization'] = ...` after construction, which is hackier. Patch B also uses `handler.generate_schema(source)` to extract a clean schema for the output type, which is safer than Patch A's `handler(source)` that re-enters the current handler chain and could pick up other annotations. Scope: Patch B is smaller and focused; Patch A adds a test rename (test_validate_from -> test_validate_as) and extra churn. Patch A's one genuine advantage is broader handling of arbitrary (non-schema-generable) output types via an any_schema fallback, which Patch B leaves at pre-existing behavior — but that case is outside the reported issue and Patch B introduces no regression there. Given correctness parity on the actual reported issue plus Patch B's stronger regression test, cleaner idiomatic implementation, and tighter scope, B is the better fix.
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
9m 52s17 turns
Wall-clock time in the container
Full test suite
No new failures
6,454 passed · 368 skipped · 6,822 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
34vs 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
pydantic/functional_validators.pyindex 6a38f619..8084cfec 100644@@ -894,8 +894,22 @@ class ValidateAs: self.instantiation_hook = instantiation_hook def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+ # The output value is of type `source`, not `from_type`. Build the serialization schema from+ # the output type so serialization doesn't emit a `PydanticSerializationUnexpectedValue` warning.+ # Some valid output types can't have a core schema generated (e.g. arbitrary classes); in that+ # case we fall back to no serialization override.+ from pydantic import PydanticSchemaGenerationError+ schema = handler(self.from_type)+ try:+ serialization = core_schema.wrap_serializer_function_ser_schema(+ function=lambda v, h: h(v),+ schema=handler.generate_schema(source),+ )+ except PydanticSchemaGenerationError:+ serialization = None return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=serialization, )tests/test_types.pyindex e70a0102..750108ef 100644@@ -6096,6 +6096,26 @@ def test_validate_from() -> None: assert ta.validate_python({'a': 1}) == Arbitrary(1) +def test_validate_from_serialization() -> None:+ 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)]++ d = Foo(x=[1, 3, 5, 8, 6, 7])+ assert d.x == 8++ with warnings.catch_warnings():+ warnings.simplefilter('error')++ assert d.model_dump() == {'x': 8}+ assert d.model_dump_json() == '{"x":8}'++ @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.
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
Clean — only the two intended files changed, no stray files. The work is complete.
Summary
What was wrong: ValidateAs.__get_pydantic_core_schema__ built its core schema from the source type (from_type, e.g. list[int]) and wrapped it in no_info_after_validator_function with no serialization override. Serialization therefore defaulted to the source-type schema, so dumping the actual output value (the annotated output type, e.g. int) emitted PydanticSerializationUnexpectedValue(Expected list[int] ...) — exactly the report's symptom.
What I changed (pydantic/functional_validators.py, ValidateAs.__get_pydantic_core_schema__): the serialization schema is now derived from the output type (source) via core_schema.wrap_serializer_function_ser_schema(function=lambda v, h: h(v), schema=handler.generate_schema(source)), guarded by try/except PydanticSchemaGenerationError with a serialization=None fallback. This mirrors the existing PlainValidator idiom in the same file, so output types whose schema can't be generated (e.g. arbitrary classes) keep the prior behavior. Validation still runs against from_type.
Test evidence:
- Added
test_validate_from_serializationintests/test_types.py(next totest_validate_from), assertingd.x == 8, and under warnings-as-errors thatmodel_dump() == {'x': 8}andmodel_dump_json() == '{"x":8}'. - Confirmed it fails before the fix (
UserWarning: Pydantic serializer warnings: PydanticSerializationUnexpectedValue(Expected list[int]...)) and passes after. - Gate:
test_validate_from+test_validate_from_serialization→ 2 passed. AdjacentPlainValidator/serialization tests intest_validators.py→ 5 passed, 1 xfailed. - Diff scope verified: only the two intended files, no stray/scratch files.
Requirements list:
- Fix the unexpected serialization schema so
ValidateAswith differing source/output types dumps without warning — implemented, with test evidence (dump returns{'x': 8}/8, no warning).
The report offered no alternatives or additional named scenarios. Changes are left uncommitted in the working tree.