Backtestspydanticrun-20261009-110034#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 core bug the same way. They generate a schema for the source type and attach it as a wrap serializer on the after-validator, so the validated value is serialized as the target type (`int`) rather than `from_type` (`list[int]`). Both fall back gracefully when the source type can't produce a schema (A uses `any_schema` inside a wrap serializer; B uses an identity plain serializer). Correctness on the reported scenario is therefore similar. B goes further in a way that matters for the same root cause. A wrap serializer's inner schema isn't picked up by the JSON Schema generator, so with A the serialization-mode JSON schema would still describe `list[int]`. That is the same mismatch the issue reports, just in the schema. B adds a `pydantic_js_functions` metadata hook so the serialization-mode JSON schema reflects the source type, and its tests assert this. Tests: A adds one test for the issue case, checking only `model_dump_json` and not that the warning is gone. Its arbitrary-type test only checks round-trip equality. A also renames an existing test, which is unrelated churn. B's tests turn warnings into errors to confirm the reported warning no longer fires. They also cover: - validation vs serialization JSON schema - a source type with its own serializer (`date` → isoformat) - an inner `PlainSerializer` - arbitrary types, both allowed and disallowed - include/exclude propagation to nested models These would fail before the fix and are meaningfully stronger. Craft is comparable, though A leaves a TODO and makes an unrelated comment edit. Overall B is more complete and much better tested.
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 24s
Wall-clock time in the container
Full test suite
No new failures
1,315 passed · 10 skipped · 1,325 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
138vs 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+138 −0
pydantic/functional_validators.pyindex 6a38f619..28e461e0 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:+ from pydantic._internal._generate_schema import GENERATE_SCHEMA_ERRORS+ schema = handler(self.from_type)++ # The validated value is the one returned by `instantiation_hook`, so it should be serialized+ # according to the source type (and not `from_type`). Note that for some valid uses of `ValidateAs`+ # (e.g. with arbitrary types), it is not possible to generate a core schema for the source type,+ # in which case the value is serialized without any type expectation:+ try:+ source_schema = handler(source)+ except GENERATE_SCHEMA_ERRORS:+ serialization = core_schema.plain_serializer_function_ser_schema(lambda v: v)+ metadata = None+ else:+ serialization = core_schema.wrap_serializer_function_ser_schema(+ function=lambda v, h: h(v), schema=source_schema+ )+ # The wrap serializer's `schema` isn't used by the JSON Schema generator, so we explicitly+ # use the source schema when generating the JSON Schema in serialization mode:+ metadata = {+ 'pydantic_js_functions': [+ lambda s, h: h(source_schema) if h.mode == 'serialization' else h(s),+ ]+ }+ return core_schema.no_info_after_validator_function( self.instantiation_hook, schema=schema,+ serialization=serialization,+ metadata=metadata, )tests/test_types.pyindex e70a0102..d47b4896 100644@@ -6096,6 +6096,118 @@ 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 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_source_type_serializer() -> None:+ ta = TypeAdapter(Annotated[date, ValidateAs(int, date.fromordinal)])++ value = ta.validate_python(738000)+ assert value == date.fromordinal(738000)++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) == value+ assert ta.dump_python(value, mode='json') == value.isoformat()+ assert ta.dump_json(value) == f'"{value.isoformat()}"'.encode()++ assert ta.json_schema(mode='validation') == {'type': 'integer'}+ assert ta.json_schema(mode='serialization') == {'format': 'date', 'type': 'string'}+++def test_validate_as_serialization_inner_serializer() -> None:+ ta = TypeAdapter(Annotated[int, PlainSerializer(lambda v: f'#{v}'), ValidateAs(str, int)])++ value = ta.validate_python('12')+ assert value == 12++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) == '#12'+ assert ta.dump_json(value) == b'"#12"'+++def test_validate_as_serialization_arbitrary_type() -> None:+ class Arbitrary:+ def __init__(self, a: int) -> None:+ self.a = a++ class Model(BaseModel):+ model_config = ConfigDict(arbitrary_types_allowed=True)++ x: Annotated[Arbitrary, ValidateAs(int, Arbitrary)]++ m = Model(x=1)+ assert m.x.a == 1++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert m.model_dump() == {'x': m.x}+++def test_validate_as_serialization_arbitrary_type_not_allowed() -> None:+ class Arbitrary:+ def __init__(self, a: int) -> None:+ self.a = a++ ta = TypeAdapter(Annotated[Arbitrary, ValidateAs(int, Arbitrary)])++ value = ta.validate_python(1)+ assert value.a == 1++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert ta.dump_python(value) is value+++def test_validate_as_serialization_include_exclude() -> None:+ class Inner(BaseModel):+ a: int+ b: int++ class Model(BaseModel):+ x: Annotated[Inner, ValidateAs(int, lambda v: Inner(a=v, b=v))]++ m = Model(x=1)++ with warnings.catch_warnings():+ warnings.simplefilter('error')+ assert m.model_dump() == {'x': {'a': 1, 'b': 1}}+ assert m.model_dump(exclude={'x': {'b'}}) == {'x': {'a': 1}}+ assert m.model_dump(include={'x': {'a'}}) == {'x': {'a': 1}}+ assert m.model_dump_json(exclude={'x': {'b'}}) == '{"x":{"a":1}}'++ assert Model.model_json_schema(mode='serialization')['properties']['x'] == Inner.model_json_schema(+ mode='serialization'+ )+ assert Model.model_json_schema(mode='validation')['properties']['x'] == {'title': 'X', 'type': 'integer'}++ @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.