Backtestspydanticrun-20261027-165153#14073
Defer formatting discarded errors during nested union serialization
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches stop the eager formatting: all four independent repr-count tests go from FAIL-ASSERT on base to PASS on both, and the final warning/error text is unchanged for both. The decisive difference is the observable exception protocol the issue explicitly calls out. Patch A builds the aggregate as a lazy PyErr whose args materialize to the same (message, None, None, None) tuple as the eager path, so a wrap serializer that catches it sees an identical exception; the independent test for this passes on A. Patch B constructs the exception via Bound::new, which bypasses BaseException.__init__ and leaves .args == (), exactly the regression the issue warned about, and the independent test confirms FAIL-ASSERT on B. B tries to paper over this with a custom __reduce__ for pickling, but that does not restore .args for user code, and it adds a new message enum, Python::attach inside to_py_err, and __traverse__ complexity to the exception type. A's approach keeps the child errors only in Rust until normalization, so no GC traversal changes are needed, and A's tests cover the repr count, warnings-disabled path, and the wrap-serializer args/repr invariants directly. A's one risk is the early Ok(None) return in TaggedUnionSerializer when warnings are disabled, which could skip whatever the non-UnexpectedValue else branch does, but that is narrower than B's confirmed args regression. A better satisfies correctness as the issue defines it, has stronger tests, and is the smaller, less invasive change.
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 13s
Wall-clock time in the container
Full test suite
No new failures
678 passed · 95 skipped · 773 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
185vs 151 merged
Changed lines, ours vs merged
The issue
View on GitHubThis issue was posted by Codex Desktop using gpt-6.1-sol on behalf of David.
Serializing a valid dataclass subclass through str | list[str | Content] calls its __repr__ twice, although serialization succeeds without warnings. This becomes expensive when the representation includes large byte payloads.
Reproduced on Pydantic 2.13.5 / pydantic-core 2.46.5 and current Pydantic main 37dc34b4f001191b4f3db707cc52bac95baf17f0 (2.15.0a0+dev / core 2.50.0).
<details>
<summary>Minimal reproduction</summary>
from pydantic import TypeAdapter
from pydantic.dataclasses import dataclass
calls = []
@dataclass
class Content:
data: bytes
@dataclass
class Image(Content):
def __repr__(self):
calls.append(1)
return f'Image(data={self.data!r})'
value = [Image(data=b'abc')]
adapter = TypeAdapter(str | list[str | Content])
print(adapter.dump_json(value, warnings='error'))
print(len(calls))
# b'[{"data":"abc"}]'
# 2
The same case calls repr zero times when the annotation is list[str | Content], without the outer union. dump_python also reproduces the eager calls.
</details>
The nested union fails its strict pass because the value is a subclass. UnionChoices::serialize calls union_serialization_unexpected_value, which immediately joins the child exceptions with ToString::to_string. The outer union then succeeds during its lax pass and discards those formatted errors.
Affected source: union.rs at the tested commit.
pydantic-core #1652 deferred formatting individual unexpected values, but the nested aggregate still formats eagerly. pydantic-core #1649 describes the earlier case.
A local prototype retaining child exceptions until __str__ removes the unnecessary calls without changing strict/lax branch selection. It also removes five repr calls in the downstream Pydantic AI #11138 example. Output, final warning/error text, filtering, and metadata serializer counts match the unmodified upstream build in the executed probes.
The prototype is not ready for a PR: a wrap serializer can catch an intermediate aggregate, so its exception protocol needs care. Constructing that aggregate directly currently changes .args from (message, None, None, None) to (). Retained exceptions also need correct GC traversal. The intended fix is lazy formatting while preserving those observable details.
The two patches
pydantic-core/src/serializers/extra.rsindex 1551057b..9f0466d3 100644@@ -444,8 +444,13 @@ impl CollectWarnings { } } + /// Whether registered warnings are retained, i.e. whether it's worth building them at all+ pub fn is_enabled(&self) -> bool {+ self.mode != WarningsMode::None+ }+ pub fn register_warning(&mut self, warning: PydanticSerializationUnexpectedValue) {- if self.mode != WarningsMode::None {+ if self.is_enabled() { self.warnings.push(warning); } }@@ -500,7 +505,7 @@ impl CollectWarnings { field_type: &str, value: &Bound<'_, PyAny>, ) {- if self.mode != WarningsMode::None {+ if self.is_enabled() { self.register_warning(PydanticSerializationUnexpectedValue::new_from_parts( field_name.map(|name| name.clone().unbind()), Some(field_type.to_string()),pydantic-core/src/serializers/type_serializers/union.rsindex 5d13102a..54292f26 100644@@ -296,6 +296,10 @@ impl TaggedUnionSerializer { if in_top_level_union(state) { // Register a warning for the matched variant only, then // fall through to inference instead of trying all other variants.+ // (Inspecting the error may format a deferred nested union error, so skip it if warnings are disabled.)+ if !state.warnings.is_enabled() {+ return Ok(None);+ } if let Ok(unexpected_value) = err.value(value.py()).cast::<PydanticSerializationUnexpectedValue>() { state.warnings.register_warning(unexpected_value.borrow().clone()); } else {@@ -379,7 +383,7 @@ impl UnionChoices { // in a nested union, we immediately bail out with the collected errors if !in_top_level_union(state) { debug_assert_eq!(errors.len(), self.choices.len());- return Err(union_serialization_unexpected_value(&errors));+ return Err(union_serialization_unexpected_value(errors)); } // otherwise, in a top level union, we retry with lax checking if any choice supports it@@ -409,16 +413,41 @@ fn scoped_check_level<'scope, 'py>( } /// Produce an unexpected value error from errors encountered during union serialization+///+/// The returned error is lazy: the child errors are only formatted into the message when+/// the error is actually inspected (e.g. displayed, registered as a warning or raised into+/// Python). This matters because an enclosing union often discards this error when one of+/// its later attempts succeeds, and formatting can be expensive (e.g. `repr` of large inputs). #[cold]-fn union_serialization_unexpected_value(errors: &[PyErr]) -> PyErr {- // FIXME: used "joined" helper- let message = errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n");- PydanticSerializationUnexpectedValue::new_from_msg(Some(message)).to_py_err()+fn union_serialization_unexpected_value(errors: SmallVec<[PyErr; SMALL_UNION_THRESHOLD]>) -> PyErr {+ PyErr::new::<PydanticSerializationUnexpectedValue, _>(UnionErrorsArgs(errors.into_vec()))+}++/// Deferred constructor arguments for the aggregate error of a failed nested union.+///+/// Converts to the same `(message, field_name, field_type, input_value)` arguments as+/// `PydanticSerializationUnexpectedValue::to_py_err`, so the materialized exception is+/// indistinguishable from an eagerly constructed one.+struct UnionErrorsArgs(Vec<PyErr>);++impl<'py> IntoPyObject<'py> for UnionErrorsArgs {+ type Target = PyTuple;+ type Output = Bound<'py, PyTuple>;+ type Error = PyErr;++ fn into_pyobject(self, py: Python<'py>) -> PyResult<Self::Output> {+ let message = self.0.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n");+ (Some(message), None::<Py<PyAny>>, None::<String>, None::<Py<PyAny>>).into_pyobject(py)+ } } /// Register warnings from errors encountered during union serialization #[cold] fn register_union_serialization_warnings(state: &mut SerializationState<'_>, errors: &[PyErr]) {+ // inspecting the errors may format deferred nested union errors, so skip it if warnings are discarded anyway+ if !state.warnings.is_enabled() {+ return;+ } let py = state.py(); for err in errors { if let Ok(unexpected_value) = err.value(py).cast::<PydanticSerializationUnexpectedValue>() {pydantic-core/tests/serializers/test_union.pyindex 893850f9..6de3be0e 100644@@ -1116,3 +1116,119 @@ def test_discriminated_union_ser_with_typed_dict() -> None: assert v.to_python({'type': 'a', 'a': 1}, warnings='error') == {'type': 'a', 'a': 1} assert v.to_python({'type': 'b', 'b': 'foo'}, warnings='error') == {'type': 'b', 'b': 'foo'}+++@dataclasses.dataclass+class _ReprContent:+ data: bytes+++_repr_calls: list[int] = []+++@dataclasses.dataclass(repr=False)+class _ReprImage(_ReprContent):+ def __repr__(self) -> str:+ _repr_calls.append(1)+ return f'_ReprImage(data={self.data!r})'+++def _repr_content_schema() -> core_schema.DataclassSchema:+ return core_schema.dataclass_schema(+ _ReprContent,+ core_schema.dataclass_args_schema(+ '_ReprContent',+ [core_schema.dataclass_field(name='data', schema=core_schema.bytes_schema())],+ ),+ ['data'],+ )+++@pytest.fixture+def repr_calls() -> list[int]:+ _repr_calls.clear()+ yield _repr_calls+ _repr_calls.clear()+++def test_nested_union_discarded_errors_not_formatted(repr_calls: list[int]) -> None:+ """https://github.com/pydantic/pydantic/issues/14073++ The nested union fails its strict pass for a subclass, but the outer union then succeeds in its lax pass,+ so the nested union's errors are discarded and must never be formatted (formatting calls `repr`).+ """+ s = SchemaSerializer(+ core_schema.union_schema(+ [+ core_schema.str_schema(),+ core_schema.list_schema(core_schema.union_schema([core_schema.str_schema(), _repr_content_schema()])),+ ]+ )+ )+ value = [_ReprImage(data=b'abc')]++ assert s.to_python(value, warnings='error') == [{'data': b'abc'}]+ assert s.to_python(value, mode='json', warnings='error') == [{'data': 'abc'}]+ assert s.to_json(value, warnings='error') == b'[{"data":"abc"}]'+ assert repr_calls == []+++def test_nested_union_errors_not_formatted_when_warnings_disabled(repr_calls: list[int]) -> None:+ s = SchemaSerializer(+ core_schema.union_schema(+ [+ core_schema.int_schema(),+ core_schema.list_schema(core_schema.union_schema([core_schema.int_schema(), core_schema.str_schema()])),+ ]+ )+ )+ value = [_ReprImage(data=b'abc')]++ assert s.to_python(value, warnings=False) == [{'data': b'abc'}]+ assert repr_calls == []++ # with warnings enabled the nested errors are still reported in full+ with pytest.warns(UserWarning) as w:+ s.to_python(value)+ assert repr_calls+ message = str(w[0].message)+ assert 'Expected `int`' in message+ assert 'Expected `str`' in message+ assert "input_value=_ReprImage(data=b'abc')" in message+++def test_nested_union_error_seen_by_wrap_serializer() -> None:+ """A wrap serializer can catch the aggregate error of a nested union; it must look exactly like before."""+ caught: list[PydanticSerializationUnexpectedValue] = []++ def wrap(value: Any, handler: core_schema.SerializerFunctionWrapHandler) -> Any:+ try:+ return handler(value)+ except PydanticSerializationUnexpectedValue as e:+ caught.append(e)+ raise++ s = SchemaSerializer(+ core_schema.union_schema(+ [+ core_schema.str_schema(),+ core_schema.list_schema(+ core_schema.union_schema(+ [core_schema.str_schema(), _repr_content_schema()],+ serialization=core_schema.wrap_serializer_function_ser_schema(wrap),+ )+ ),+ ]+ )+ )++ assert s.to_python([_ReprImage(data=b'abc')], warnings='error') == [{'data': b'abc'}]++ assert len(caught) == 1+ err = caught[0]+ assert type(err) is PydanticSerializationUnexpectedValue+ message = str(err)+ assert 'Expected `str`' in message+ assert '\n' in message+ assert err.args == (message, None, None, None)+ assert repr(err) == f'PydanticSerializationUnexpectedValue({message})'tests/test_dataclasses.pyindex b3fea31d..f43ebe98 100644@@ -3410,3 +3410,24 @@ def test_polymorphic_serialization_with_model_serializer(config: bool, runtime: else: assert serializer.to_python(ClassB(a=123, b='test'), **kwargs) == 'ClassA' assert serializer.to_json(ClassB(a=123, b='test'), **kwargs) == b'"ClassA"'+++def test_nested_union_serialization_does_not_repr_subclass() -> None:+ """https://github.com/pydantic/pydantic/issues/14073"""+ calls = []++ @pydantic.dataclasses.dataclass+ class Content:+ data: bytes++ @pydantic.dataclasses.dataclass+ class Image(Content):+ def __repr__(self) -> str:+ calls.append(1)+ return f'Image(data={self.data!r})'++ value = [Image(data=b'abc')]+ adapter = TypeAdapter(str | list[str | Content])+ assert adapter.dump_json(value, warnings='error') == b'[{"data":"abc"}]'+ assert adapter.dump_python(value, warnings='error') == [{'data': b'abc'}]+ assert calls == []
pydantic-core/src/serializers/errors.rsindex e043f0d20..b27afb1fe 100644@@ -1,9 +1,10 @@ use std::fmt; use std::fmt::Write; -use pyo3::exceptions::PyValueError;+use pyo3::exceptions::{PyBaseException, PyValueError}; use pyo3::prelude::*;-use pyo3::types::PyString;+use pyo3::types::{PyString, PyTuple};+use pyo3::{PyTraverseError, PyVisit, intern}; use crate::tools::truncate_safe_repr; @@ -108,10 +109,18 @@ impl PydanticSerializationError { } } +#[derive(Debug, Clone)]+enum UnexpectedValueMessage {+ Text(String),+ /// Errors encountered during (nested) union serialization, only formatted when the message is+ /// needed, as formatting them can be expensive.+ UnionErrors(Vec<Py<PyBaseException>>),+}+ #[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core", frozen)] #[derive(Debug, Clone)] pub struct PydanticSerializationUnexpectedValue {- message: Option<String>,+ message: Option<UnexpectedValueMessage>, field_name: Option<Py<PyString>>, field_type: Option<String>, input_value: Option<Py<PyAny>>,@@ -120,10 +129,26 @@ pub struct PydanticSerializationUnexpectedValue { impl PydanticSerializationUnexpectedValue { pub fn new_from_msg(message: Option<String>) -> Self { Self {- message,+ message: message.map(UnexpectedValueMessage::Text),+ field_name: None,+ field_type: None,+ input_value: None,+ }+ }++ /// Create an error from the errors encountered during union serialization.+ #[cold]+ pub fn new_from_union_errors(py: Python<'_>, errors: &[PyErr]) -> PyErr {+ let union_errors = errors.iter().map(|err| err.value(py).clone().unbind()).collect();+ let this = Self {+ message: Some(UnexpectedValueMessage::UnionErrors(union_errors)), field_name: None, field_type: None, input_value: None,+ };+ match Bound::new(py, this) {+ Ok(err) => PyErr::from_value(err.into_any()),+ Err(err) => err, } } @@ -147,16 +172,30 @@ impl PydanticSerializationUnexpectedValue { input_value: Option<Py<PyAny>>, ) -> Self { Self {- message,+ message: message.map(UnexpectedValueMessage::Text), field_name, field_type, input_value, } } + /// The message of the error, formatting the union errors if any.+ fn message(&self, py: Python<'_>) -> Option<String> {+ match self.message.as_ref()? {+ UnexpectedValueMessage::Text(message) => Some(message.clone()),+ UnexpectedValueMessage::UnionErrors(errors) => Some(+ errors+ .iter()+ .map(|err| PyErr::from_value(err.bind(py).clone().into_any()).to_string())+ .collect::<Vec<_>>()+ .join("\n"),+ ),+ }+ }+ pub fn to_py_err(&self) -> PyErr { PyErr::new::<Self, _>((- self.message.clone(),+ Python::attach(|py| self.message(py)), self.field_name.clone(), self.field_type.clone(), self.input_value.clone(),@@ -175,7 +214,7 @@ impl PydanticSerializationUnexpectedValue { input_value: Option<Py<PyAny>>, ) -> Self { Self {- message,+ message: message.map(UnexpectedValueMessage::Text), field_name, field_type, input_value,@@ -183,7 +222,7 @@ impl PydanticSerializationUnexpectedValue { } pub(crate) fn __str__(&self, py: Python) -> String {- let mut message = self.message.as_deref().unwrap_or("").to_string();+ let mut message = self.message(py).unwrap_or_default(); if let Some(field_type) = &self.field_type { if !message.is_empty() {@@ -227,4 +266,39 @@ impl PydanticSerializationUnexpectedValue { pub(crate) fn __repr__(&self, py: Python) -> String { format!("PydanticSerializationUnexpectedValue({})", self.__str__(py)) }++ // Instances created from Rust don't go through `BaseException.__init__()` and have empty `args`,+ // so `BaseException.__reduce__()` can't be relied on:+ fn __reduce__<'py>(slf: &Bound<'py, Self>) -> PyResult<Bound<'py, PyTuple>> {+ let py = slf.py();+ let args = slf.getattr(intern!(py, "args"))?.cast_into::<PyTuple>()?;+ // Only use the stored values if `args` is empty, as `args` reflects the arguments+ // passed to (potentially overridden) `__new__()` implementations of subclasses:+ let args = if args.is_empty() {+ let this = slf.get();+ (this.message(py), &this.field_name, &this.field_type, &this.input_value).into_pyobject(py)?+ } else {+ args+ };+ match slf.getattr_opt(intern!(py, "__dict__"))? {+ // e.g. attributes set on subclass instances, or exception notes:+ Some(dict) if dict.is_truthy()? => (slf.get_type(), args, dict).into_pyobject(py),+ _ => (slf.get_type(), args).into_pyobject(py),+ }+ }++ fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {+ if let Some(field_name) = &self.field_name {+ visit.call(field_name)?;+ }+ if let Some(input_value) = &self.input_value {+ visit.call(input_value)?;+ }+ if let Some(UnexpectedValueMessage::UnionErrors(errors)) = &self.message {+ for err in errors {+ visit.call(err)?;+ }+ }+ Ok(())+ } }pydantic-core/src/serializers/type_serializers/union.rsindex 5d13102af..d9cd18e6c 100644@@ -379,7 +379,10 @@ impl UnionChoices { // in a nested union, we immediately bail out with the collected errors if !in_top_level_union(state) { debug_assert_eq!(errors.len(), self.choices.len());- return Err(union_serialization_unexpected_value(&errors));+ return Err(PydanticSerializationUnexpectedValue::new_from_union_errors(+ state.py(),+ &errors,+ )); } // otherwise, in a top level union, we retry with lax checking if any choice supports it@@ -408,14 +411,6 @@ fn scoped_check_level<'scope, 'py>( state.scoped_set(|s| &mut s.check, check_level) } -/// Produce an unexpected value error from errors encountered during union serialization-#[cold]-fn union_serialization_unexpected_value(errors: &[PyErr]) -> PyErr {- // FIXME: used "joined" helper- let message = errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n");- PydanticSerializationUnexpectedValue::new_from_msg(Some(message)).to_py_err()-}- /// Register warnings from errors encountered during union serialization #[cold] fn register_union_serialization_warnings(state: &mut SerializationState<'_>, errors: &[PyErr]) {tests/test_serialize.pyindex f0adc7145..01542fe0a 100644@@ -3,6 +3,7 @@ New tests for v2 of serialization logic. """ import json+import pickle import re import sys from collections.abc import Callable@@ -12,7 +13,12 @@ from re import Pattern from typing import Annotated, Any, ClassVar, Optional import pytest-from pydantic_core import PydanticSerializationError, core_schema, to_jsonable_python+from pydantic_core import (+ PydanticSerializationError,+ PydanticSerializationUnexpectedValue,+ core_schema,+ to_jsonable_python,+) from typing_extensions import TypedDict from pydantic import (@@ -1207,6 +1213,46 @@ def test_subclass_support_unions() -> None: assert s.model_dump() == {'pets': [{'name': 'fluffy'}, {'name': 'patches'}]} +def test_nested_union_errors_formatted_lazily() -> None:+ """https://github.com/pydantic/pydantic/issues/14073"""++ repr_calls = 0++ class Pet(BaseModel):+ name: str++ class Dog(Pet):+ def __repr__(self) -> str:+ nonlocal repr_calls+ repr_calls += 1+ return super().__repr__()++ caught: list[PydanticSerializationUnexpectedValue] = []++ def wrap(v: Any, handler: SerializerFunctionWrapHandler) -> Any:+ try:+ return handler(v)+ except PydanticSerializationUnexpectedValue as e:+ caught.append(e)+ raise++ ta = TypeAdapter(str | list[Annotated[str | Pet, WrapSerializer(wrap)]])++ # The nested union fails during the strict pass of the outer union, and succeeds during the lax one:+ assert ta.dump_python([Dog(name='fluffy')], warnings='error') == [{'name': 'fluffy'}]+ assert ta.dump_json([Dog(name='fluffy')], warnings='error') == b'[{"name":"fluffy"}]'+ assert repr_calls == 0++ err = caught[0]+ assert str(err) == (+ 'PydanticSerializationUnexpectedValue: Expected `str` - serialized value may not be as expected '+ "[input_value=Dog(name='fluffy'), input_type=Dog]\n"+ 'PydanticSerializationUnexpectedValue: Expected `Pet` - serialized value may not be as expected '+ "[input_value=Dog(name='fluffy'), input_type=Dog]"+ )+ assert str(pickle.loads(pickle.dumps(err))) == str(err)++ def test_subclass_support_unions_with_forward_ref() -> None: class Bar(BaseModel): bar_id: int