Backtestspydanticrun-20261002-175417#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: they build a core schema for `source` (the post-hook type) and serialize with it instead of the `from_type` schema, falling back to untyped serialization when `source` has no schema. Both resolve the reported warning. Correctness: Patch A is more complete. It also fixes the serialization-mode JSON schema by adding a `pydantic_js_annotation_functions` entry that uses `source_schema` in serialization mode and the validation schema otherwise. Its test checks this: `x` is `integer` in serialization mode and `array` in validation mode. Patch B attaches a wrap serializer with no `return_schema`. Its serialization JSON schema will most likely still describe `list[int]`, so the mismatch survives there. Patch A also reuses the source's own `serialization` key when one exists, so custom serializers on the source type (e.g. `PlainSerializer`) are honoured directly; it tests this case. Patch B catches the broader `GENERATE_SCHEMA_ERRORS` while A catches only `PydanticSchemaGenerationError`. That is a small edge in B's favour, but A's catch covers the arbitrary-type case that matters, and A tests it. Tests: Patch A's tests are much stronger. They run dumps with warnings escalated to errors, so they fail without the fix. They cover `model_dump`, `model_dump_json`, both JSON schema modes, a custom serializer on the source, a model source with include/exclude, and the arbitrary-type fallback. Patch B has two thin tests. The known-serialization test only checks the JSON output, which was already correct before the fix (only a warning was emitted), so it relies on the global warning filter config to be meaningful. B also renames an existing test, which is unrelated churn. Craft: both are reasonable. A's TODO and type-ignore are acknowledged. B's comment about pydantic-core limitations is informative. Overall A is the more correct and better-tested 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
14m 15s
Wall-clock time in the container
Full test suite
No new failures
12,303 passed · 501 skipped · 12,804 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
119vs 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+118 −1
pydantic/functional_validators.pyindex 6a38f619..f102594d 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@@ -894,8 +894,39 @@ 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)++ # The validated value is an instance of `source`, not of `from_type`, so serialization+ # should be performed according to `source` (the schema of `from_type` would otherwise+ # be used, leading to serialization warnings or unexpected outputs).+ # Generating a core schema for `source` may not be possible (e.g. for arbitrary types),+ # in which case we fallback to type inference during serialization.+ try:+ source_schema = handler(source)+ except PydanticSchemaGenerationError:+ return core_schema.no_info_after_validator_function(+ self.instantiation_hook,+ schema=schema,+ serialization=core_schema.simple_ser_schema('any'),+ )++ # 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.+ # Note that no `return_schema` is set on the wrap serializer, as the value returned by `h(v)`+ # is already serialized. The serialization JSON Schema is generated from `source_schema` instead:+ serialization = source_schema.get(+ 'serialization',+ core_schema.wrap_serializer_function_ser_schema(function=lambda v, h: h(v), schema=source_schema),+ )++ def json_schema_func(cs: core_schema.CoreSchema, handler: GetJsonSchemaHandler) -> dict[str, Any]:+ return handler(source_schema if handler.mode == 'serialization' else cs)+ return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=serialization, # pyright: ignore[reportArgumentType]+ metadata={'pydantic_js_annotation_functions': [json_schema_func]}, )tests/test_types.pyindex e70a0102..ed6a0f6b 100644@@ -6096,6 +6096,92 @@ 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:+ 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, 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}'++ assert Model.model_json_schema(mode='validation')['properties']['x'] == {+ 'items': {'type': 'integer'},+ 'title': 'X',+ 'type': 'array',+ }+ assert Model.model_json_schema(mode='serialization')['properties']['x'] == {'title': 'X', 'type': 'integer'}+++def test_validate_as_serialization_source_custom_serializer() -> None:+ ta = TypeAdapter(+ Annotated[+ Annotated[int, PlainSerializer(lambda v: f'value={v}')],+ ValidateAs(str, lambda v: int(v)),+ ]+ )++ assert ta.validate_python('3') == 3+ assert ta.dump_python(3) == 'value=3'+++def test_validate_as_serialization_model_source_type() -> None:+ class Inner(BaseModel):+ a: int+ b: int = 0++ class Model(BaseModel):+ inner: Annotated[Inner, ValidateAs(tuple[int, int], lambda t: Inner(a=t[0], b=t[1]))]++ m = Model(inner=(1, 2))+ assert m.inner == Inner(a=1, b=2)++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert m.model_dump() == {'inner': {'a': 1, 'b': 2}}+ assert m.model_dump(exclude={'inner': {'b'}}) == {'inner': {'a': 1}}+ assert m.model_dump_json() == '{"inner":{"a":1,"b":2}}'++ assert Model.model_json_schema(mode='serialization')['properties']['inner'] == {+ 'properties': {+ 'a': {'title': 'A', 'type': 'integer'},+ 'b': {'default': 0, 'title': 'B', 'type': 'integer'},+ },+ 'required': ['a'],+ 'title': 'Inner',+ '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})++ # No core schema can be generated for `Arbitrary`, so serialization falls back to type inference,+ # instead of attempting to serialize as `ArbitraryModel` (and emitting a warning):+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) is value++ @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.