Backtestspydanticrun-20261013-104308#13460
Unexpected serialization schema with ValidateAs
Better
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the same way. Each keeps the `from_type` schema for validation and attaches a wrap serializer, `lambda v, h: h(v)`, built on the core schema of the `source` type. With that change the issue's example (`int` validated via `list[int]`) serializes without the spurious warning.
Patch A handles more edges.
- It suppresses `ArbitraryTypeWarning` while probing the source schema, which avoids a likely new warning for arbitrary source types.
- It reuses an existing `serialization` key from the source schema when one is present.
- It falls back to the original behaviour for `is-instance` or ungeneratable source schemas.
- It fixes the JSON schema in serialization mode. A wrap serializer without a `return_schema` would otherwise make the serialization-mode JSON schema reflect `from_type` (`list[int]`) rather than `int`. Patch A adds a `pydantic_js_annotation_functions` hook to generate it from the source schema.
Patch B leaves that JSON schema discrepancy unaddressed. For arbitrary source types it falls back to `any_schema` serialization, which is reasonable. It catches the broader `GENERATE_SCHEMA_ERRORS`, but it does not guard against the arbitrary-type warning.
On tests, Patch A is much stronger. It checks that the warning is gone (`simplefilter('error')`), JSON output, validation- and serialization-mode JSON schemas, `date` serialization in JSON mode, arbitrary types with and without `arbitrary_types_allowed`, and model and list-of-model sources with `include`/`exclude` pass-through. Patch B's tests are minimal: one JSON dump check that would mostly pass even before the fix, since only a warning is emitted, and one arbitrary-type dump. Patch B also renames an existing test and adds an unrelated comment elsewhere, which is minor churn.
Patch A is more complex, but the extra handling is justified and well commented.
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
11m 18s
Wall-clock time in the container
Full test suite
No new failures
1,313 passed · 10 skipped · 1,323 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
126vs 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+125 −1
pydantic/functional_validators.pyindex 6a38f619..0635379c 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,43 @@ class ValidateAs: self.instantiation_hook = instantiation_hook def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+ # Validation is performed using `from_type`, but the validated (and instantiated) value is of the+ # `source` type, so serialization should follow the `source` type. If no meaningful core schema can+ # be generated for the `source` type (e.g. an arbitrary type), we don't customize serialization.+ from pydantic import PydanticSchemaGenerationError++ try:+ with warnings.catch_warnings():+ warnings.simplefilter('ignore', ArbitraryTypeWarning)+ source_schema = handler(source)+ except PydanticSchemaGenerationError:+ source_schema = None+ schema = handler(self.from_type)+ if source_schema is None or source_schema['type'] == 'is-instance':+ return core_schema.no_info_after_validator_function(self.instantiation_hook, schema=schema)++ if 'serialization' in source_schema:+ # TODO if `source_schema['serialization']` is one of `'include-exclude-dict/sequence',+ # schema validation will fail. That's why we use 'type ignore' comments below.+ return core_schema.no_info_after_validator_function(+ self.instantiation_hook,+ schema=schema,+ serialization=source_schema['serialization'], # pyright: ignore[reportArgumentType]+ )++ def get_json_schema(cs: core_schema.CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue:+ if handler.mode == 'serialization':+ # A wrap serializer without a return schema isn't supported by the JSON Schema generation,+ # so the JSON Schema is generated from the `source` schema instead:+ return handler(cs['serialization']['schema']) # pyright: ignore+ return handler(cs)+ return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ # The value is serialized using the `source` schema (a wrap serializer is used so that+ # serialization options, such as `include` and `exclude`, are passed through):+ serialization=core_schema.wrap_serializer_function_ser_schema(lambda v, h: h(v), schema=source_schema),+ metadata={'pydantic_js_annotation_functions': [get_json_schema]}, )tests/test_types.pyindex e70a0102..4c96b171 100644@@ -6096,6 +6096,94 @@ 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 Foo(BaseModel):+ x: Annotated[int, ValidateAs(list[int], first_even)]++ foo = Foo(x=[1, 3, 5, 8, 6, 7])+ assert foo.x == 8++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert foo.model_dump() == {'x': 8}+ assert foo.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'}+++def test_validate_as_serialization_uses_source_type_serializer() -> None:+ ta = TypeAdapter(Annotated[date, ValidateAs(int, lambda v: date(2000, 1, v))])++ d = ta.validate_python(15)+ assert d == date(2000, 1, 15)++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(d) == date(2000, 1, 15)+ assert ta.dump_python(d, mode='json') == '2000-01-15'+ assert ta.dump_json(d) == b'"2000-01-15"'+++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, lambda v: Arbitrary(a=v.a))])++ # No core schema can be generated for `Arbitrary`, so serialization falls back to the `from_type` schema:+ assert ta.dump_python(ta.validate_python({'a': 1})) == {'a': 1}++ # Same with `arbitrary_types_allowed`:+ ta = TypeAdapter(+ Annotated[Arbitrary, ValidateAs(ArbitraryModel, lambda v: Arbitrary(a=v.a))],+ config=ConfigDict(arbitrary_types_allowed=True),+ )+ assert ta.dump_python(ta.validate_python({'a': 1})) == {'a': 1}+ assert ta.dump_json(ta.validate_python({'a': 1})) == b'{"a":1}'+ assert ta.json_schema(mode='serialization')['properties'] == {'a': {'title': 'A', 'type': 'integer'}}+++def test_validate_as_serialization_model_source_type() -> None:+ class Inner(BaseModel):+ a: int+ b: int = 0++ class Model(BaseModel):+ x: Annotated[Inner, ValidateAs(int, lambda v: Inner(a=v, b=v))]+ y: Annotated[list[Inner], ValidateAs(int, lambda v: [Inner(a=v), Inner(a=v + 1)])]++ m = Model(x=3, y=1)++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert m.model_dump() == {'x': {'a': 3, 'b': 3}, 'y': [{'a': 1, 'b': 0}, {'a': 2, 'b': 0}]}+ assert m.model_dump(exclude={'x': {'b'}, 'y': {0}}) == {'x': {'a': 3}, 'y': [{'a': 2, 'b': 0}]}+ assert m.model_dump(include={'x': {'a'}}) == {'x': {'a': 3}}+ assert m.model_dump_json(exclude={'x': {'b'}, 'y': {0: {'b'}}}) == '{"x":{"a":3},"y":[{"a":1},{"a":2,"b":0}]}'++ assert Model.model_json_schema(mode='serialization')['properties'] == {+ 'x': {'$ref': '#/$defs/Inner', 'title': 'X'},+ 'y': {'items': {'$ref': '#/$defs/Inner'}, 'title': 'Y', 'type': 'array'},+ }++ @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.