Backtestspydanticrun-20260930-183947#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 root cause the same way. They generate a schema for the annotated `source` type and attach it as a wrap-serializer on the after-validator, so `model_dump` stops warning. Both fall back to an any-type serializer when the source type cannot produce a schema.
Patch B is more complete in three ways.
1. It fixes the serialization-mode JSON schema. The `pydantic_js_annotation_functions` metadata makes serialization mode report `integer` instead of the `list[int]` validation schema. Its test checks both modes. Patch A leaves serialization-mode JSON schema describing the wrong type, a closely related inconsistency the issue implies.
2. It suppresses the spurious `ArbitraryTypeWarning` that may come from probing the source type. Its tests run with warnings turned into errors, so they would fail before the fix, when the `PydanticSerializationUnexpectedValue` warning was emitted.
3. It catches only `PydanticSchemaGenerationError`. Patch A catches the broader `GENERATE_SCHEMA_ERRORS` tuple, which could quietly swallow errors such as undefined forward annotations.
On tests, Patch A's known-serialization test only checks `model_dump_json` output, which was `{"x":8}` even before the fix; the bug was only a warning. It therefore likely would not fail before the change. Patch A also renames an existing test (minor churn) and adds only a TODO comment for a related concern. Patch B's tests are stronger and cover the fallback path.
Overall, Patch B is the better-tested and more correct 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
8m 44s
Wall-clock time in the container
Full test suite
No new failures
12,301 passed · 501 skipped · 12,802 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
80vs 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+79 −1
pydantic/functional_validators.pyindex 6a38f619..07c3088d 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 @@ -895,7 +895,31 @@ class ValidateAs: def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: schema = handler(self.from_type)++ # The validated value is an instance of the source type (not `from_type`),+ # so serialization should be performed according to the source type:+ try:+ with warnings.catch_warnings():+ warnings.simplefilter('ignore', ArbitraryTypeWarning)+ source_schema = handler(source)+ except PydanticSchemaGenerationError:+ # The source type isn't natively supported by Pydantic (e.g. an arbitrary type),+ # fall back to inferring the serialization from the value at runtime:+ serialization = core_schema.simple_ser_schema('any')+ metadata = None+ else:+ serialization = core_schema.wrap_serializer_function_ser_schema(+ function=lambda v, h: h(v), schema=source_schema+ )+ metadata = {+ 'pydantic_js_annotation_functions': [+ lambda c, h: h(source_schema) if h.mode == 'serialization' else h(c)+ ]+ }+ return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=serialization,+ metadata=metadata, )tests/test_types.pyindex e70a0102..fcd4a357 100644@@ -6096,6 +6096,60 @@ 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/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, 8, 6])+ 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'}},+ 'required': ['x'],+ 'title': 'Model',+ 'type': 'object',+ }+ assert Model.model_json_schema(mode='serialization') == {+ 'properties': {'x': {'title': 'X', 'type': 'integer'}},+ 'required': ['x'],+ 'title': 'Model',+ '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')+ assert ta.dump_python(value) is value++ assert ta.json_schema() == {+ 'properties': {'a': {'title': 'A', 'type': 'integer'}},+ 'required': ['a'],+ 'title': 'ArbitraryModel',+ 'type': 'object',+ }++ @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.