Backtestspydanticrun-20261003-150343#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 reported warning the same basic way: they build a schema for the annotated `source` type and attach a wrap serializer to the after-validator, so the validated `int` is no longer serialized against `list[int]`.
Patch A also handles the serialization JSON schema. It passes `return_schema=handler.generate_schema(source)` and tests that `json_schema(mode='serialization')` gives `{'type': 'integer'}`. Patch B's wrap serializer has no `return_schema`, so the serialization-mode JSON schema likely still falls back to the validation schema (`array`). The issue is titled "Unexpected serialization schema", so this is a real gap in B.
Patch A reuses an existing `serialization` on the source schema directly, which is how prior annotations like `PlainSerializer` get respected. Its test checks this, including the JSON schema. Patch B wraps the whole source schema, which also respects them, but it does not test that.
B is better on arbitrary types. If the source schema cannot be generated, B falls back to `any_schema`, which is principled. A falls back to the `from_type` serializer for arbitrary or bare is-instance types, which can still mis-serialize or warn in cases like `ValidateAs(int, Arbitrary)`. A also calls schema generation for `source` twice and carries a TODO about include/exclude serialization types.
On tests, A's are more thorough: they turn warnings into errors, cover both `model_dump` and `dump_json`, use `TypeAdapter`, check the JSON schema in both modes, and include a stacked-serializer case. B's tests are thin, and its unknown-serialization test only checks pass-through. B also includes a minor unrelated test rename and comment churn.
Overall, A resolves more of the issue's surface (runtime serialization plus serialization schema) and tests it better, despite its weaker arbitrary-type fallback.
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
10m 28s
Wall-clock time in the container
Full test suite
No new failures
12,302 passed · 501 skipped · 12,803 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
98vs 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+98 −0
pydantic/functional_validators.pyindex 6a38f619..faf5c67e 100644@@ -894,8 +894,34 @@ class ValidateAs: self.instantiation_hook = instantiation_hook def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:+ # The validated value is of the `source` type (as returned by the instantiation hook), so serialization+ # should be performed according to `source` and not `from_type`. If no meaningful core schema can be+ # generated for `source` (e.g. an arbitrary type, possibly allowed through `arbitrary_types_allowed`),+ # fall back to the serialization of the `from_type` schema.+ from pydantic import PydanticSchemaGenerationError++ try:+ source_schema = handler(source)+ except PydanticSchemaGenerationError:+ serialization = None+ else:+ if source_schema['type'] == 'is-instance' and 'serialization' not in source_schema:+ serialization = None+ else:+ # 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.+ serialization = source_schema.get(+ 'serialization',+ core_schema.wrap_serializer_function_ser_schema(+ function=lambda v, h: h(v),+ schema=source_schema,+ return_schema=handler.generate_schema(source),+ ),+ )+ schema = handler(self.from_type) return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=serialization, # pyright: ignore[reportArgumentType] )tests/test_types.pyindex e70a0102..6c383a49 100644@@ -6096,6 +6096,78 @@ 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}'++ ta = TypeAdapter(Annotated[int, ValidateAs(list[int], first_even)])+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(ta.validate_python([1, 2])) == 2+ assert ta.dump_json(ta.validate_python([1, 2])) == b'2'++ assert ta.json_schema(mode='validation') == {'items': {'type': 'integer'}, 'type': 'array'}+ assert ta.json_schema(mode='serialization') == {'type': 'integer'}+++def test_validate_as_serialization_respects_source_annotations() -> None:+ ta = TypeAdapter(+ Annotated[+ datetime,+ PlainSerializer(lambda v: v.year, return_type=int),+ ValidateAs(int, lambda y: datetime(y, 1, 1)),+ ]+ )++ v = ta.validate_python(2020)+ assert v == datetime(2020, 1, 1)+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(v) == 2020+ assert ta.json_schema(mode='serialization') == {'type': 'integer'}+++def test_validate_as_arbitrary_source_type_serialization() -> 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))])+ v = ta.validate_python({'a': 1})+ assert isinstance(v, Arbitrary)+ assert v.a == 1++ ta_allowed = TypeAdapter(+ Annotated[Arbitrary, ValidateAs(ArbitraryModel, instantiation_hook=lambda v: Arbitrary(a=v.a))],+ config=ConfigDict(arbitrary_types_allowed=True),+ )+ v = ta_allowed.validate_python({'a': 1})+ assert isinstance(v, Arbitrary)+ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta_allowed.dump_python(v) == {'a': 1}+ assert ta_allowed.dump_json(v) == b'{"a":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.
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.