Backtestspydanticrun-20261027-165153#7713

Issue #7713 Merged fix PR #13922 Run 27 Oct 2026 · run-20261027-165153 pydantic/pydantic

Error during Serialization (model_dump)

Worse

The merged fix was judged better than our patch.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches fix the root cause the same way: when inference (or the polymorphism trampoline) hits a `__pydantic_serializer__` that is a `MockValSer`, call its `rebuild()` and use the resulting `SchemaSerializer`. Both pass all six independent tests, including the issue's exact example and the function-scope variant. The difference is in how the rebuild resolves names. Patch A threads a `parent_namespace_depth` parameter through `MockValSer.rebuild()` and the `attempt_rebuild` handlers and passes 0 from Rust, so the rebuild uses the class's stored parent namespace rather than walking five Python frames up from a call stack that originates in Rust and has nothing to do with where the class was defined. Patch B calls `rebuild()` with the default depth of 5, which from the Rust call site lands on an arbitrary frame (the Rust-to-Python hop adds the `MockValSer.rebuild` frame, so the usual count is off). A's `test_serialization_inference_unbuildable_model` demonstrates the concrete hazard: a name in the serializing caller's locals must not silently satisfy the forward ref. B's own tests (`test_serialize_instance_of_incomplete_model_by_inference`, `test_polymorphic_serialization_of_incomplete_subclass`) define the forward-referenced class after the model inside a function and expect the Rust-triggered rebuild to find it; that only works if depth 5 happens to hit the test frame, and A's test comment explicitly notes classes must be module-level for this path, so B's tests are at real risk of not passing as written. B does have one nicer edge: when the mock has no rebuild hook it touches `to_python` so the mock raises its own descriptive error instead of the raw cast `TypeError`, whereas A falls through to the `TypeError` in that case. B's trampoline change (only swallowing `AttributeError`) is clean, but A achieves the same via the `Option` return and a shared `py_get_attrs` helper. A's xfail `model_construct` test is unrelated churn but harmless. On correctness at the frame-namespace edge and on tests that are more likely to hold, A is the better 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

10m 4s

Wall-clock time in the container

Full test suite

No new failures

1,292 passed · 127 skipped · 1,419 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

177vs 202 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hello,

During migration of our code from V1 to V2 we encountered the following error .

File "/scratch/envs/pydantic-v2/lib/python3.11/site-packages/pydantic/main.py", line 308, in model_dump
    return self.__pydantic_serializer__.to_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'MockValSer' object cannot be converted to 'SchemaSerializer'

After some debugging i found two scenarios where the model_dump works

  1. when exclude_unset=True is set
  2. or when one of the fields in the base classes is modified from default=[] to default=None
step_settings: Union[ StepConstant, StepEstimation] = Field(default=[])

if the default of the above field is modified to None , then the code works.

The same works without modifications in V1. I have attached the sample code(apologies for the longer file but i wasn't able to reliably shorten it) that generates the error.

NOTE: I have also tried with model_rebuild instead of update_forward_refs and model_dump instead of dict. The code below should work in both V1 and V2.

It would be helpful for debugging if there is a more detailed stack trace for the serialization error. It's not directly clear where the issue lies.

Thanks in advance!

Example Code

# serialization_err.py

from pydantic import BaseModel, Field, PositiveFloat
from typing import ForwardRef, Literal, Union


class Base(BaseModel):
    type: str
    name: str


class BaseSettings(BaseModel):
    geo: str


class CoupledBodySettings(BaseSettings):
    coupled_body: list[BaseSettings] = Field(default=[])


class StepConstant(BaseModel):
    type: Literal["constant"]
    init_step_size: float


class StepEstimation(BaseModel):
    type: Literal["estimation"]
    init_step_size: int


class Body(Base):
    type: Literal["body"]
    settings: BaseSettings
    step_settings: Union[StepConstant, StepEstimation] = Field(default=[])


class CoupledBody(Base):
    type: Literal["coupled_body"]
    parameter_type: Literal["shape"] = Field(default="shape")
    settings: CoupledBodySettings
    step_settings: Union[StepConstant, StepEstimation] = Field(default=[])


class Glue(BaseModel):
    radius: PositiveFloat


class SubBase(BaseModel):
    type: str
    name: str
    glue: Glue


class SubCoupledBody(CoupledBody, SubBase):
    type: Literal["coupled_body"]


class SubBody(Body, SubBase):
    type: Literal["body"]


class NestedBodySettings(BaseSettings):
    nested: list[
        Union[
            SubBody,
            SubCoupledBody,
            ForwardRef("SubNestedBody"),
            ForwardRef("SubNestedSeqBody"),
        ]
    ]


class NestedBody(Base):
    type: Literal["nested"]
    parameter_type: Literal["shape"] = Field(
        default="shape",
    )
    settings: NestedBodySettings
    step_settings: Union[StepConstant, StepEstimation] = Field(default=[])


class NestedSeqBodySettings(NestedBodySettings):
    iterations_per_parameterization: list[int] = Field(default=[1, 1])
    nested_step_settings: Union[StepConstant, StepEstimation] = Field(default=[])


class NestedSeqBody(Base):
    type: Literal["nested_sequential_body"]
    settings: NestedSeqBodySettings
    step_settings: Union[StepConstant, StepEstimation] = Field(
        default=[],
    )


class SubNestedBody(NestedBody, SubBase):
    type: Literal["nested"]


class SubNestedSeqBody(NestedSeqBody, SubBase):
    type: Literal["nested_sequential_body"]


NestedBodySettings.update_forward_refs()
NestedSeqBodySettings.update_forward_refs()


class Base(BaseModel):
    name: str
    param: list[NestedBody | NestedSeqBody]


data = {
    "name": "test",
    "param": [
        {
            "type": "nested",
            "name": "parent_rb",
            "settings": {
                "geo": "plate",
                "nested": [
                    {
                        "type": "body",
                        "name": "child1_rb",
                        "glue": {"radius": 10.0},
                        "settings": {
                            "geo": "bla",
                        },
                    }
                ],
            },
        }
    ],
}

model = Base(**data)
print(model.dict(exclude_unset=False))

Python, Pydantic & OS Version

pydantic version: 2.4.2
pydantic-core version: 2.10.1
pydantic-core build: profile=release pgo=false
install path: /scratch/envs/pydantic-v2/lib/python3.11/site-packages/pydantic
python version: 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:34:09) [GCC 12.3.0]
platform: Linux-3.10.0-1160.45.1.el7.x86_64-x86_64-with-glibc2.17
related packages: typing_extensions-4.8.0

Update 10/03/23: Modified the script to be able to run it as a single file

The two patches

Our patch+168 −9
pydantic-core/src/serializers/infer.rsindex a99b22ba..45589c96 100644@@ -660,9 +660,26 @@ pub(crate) fn infer_json_key_known<'a, 'py>(     } } +/// Fetch the `__pydantic_serializer__` of `value`.+///+/// For models and dataclasses which were not fully defined at creation time, this attribute is a+/// `MockValSer` placeholder rather than a `SchemaSerializer`; in that case its `rebuild()` method+/// is called, which either returns the rebuilt `SchemaSerializer` or raises an error explaining+/// why the class is not fully defined. pub(crate) fn get_pydantic_serializer<'py>(value: &Bound<'py, PyAny>) -> PyResult<Bound<'py, SchemaSerializer>> {     let py = value.py();     let py_serializer = value.getattr(intern!(py, "__pydantic_serializer__"))?;+    if py_serializer.is_exact_instance_of::<SchemaSerializer>() {+        return py_serializer.cast_into_exact().map_err(Into::into);+    }+    if let Some(rebuild) = py_serializer.getattr_opt(intern!(py, "rebuild"))? {+        let rebuilt = rebuild.call0()?;+        if !rebuilt.is_none() {+            return rebuilt.cast_into_exact().map_err(Into::into);+        }+        // no rebuild was possible; accessing an attribute makes the placeholder raise its own error+        py_serializer.getattr(intern!(py, "to_python"))?;+    }     py_serializer.cast_into_exact().map_err(Into::into) } @@ -674,9 +691,8 @@ fn serialize_pydantic_serializable<'py, S: DoSerialize>(     state: &mut SerializationState<'py>,     do_serialize: S, ) -> Result<S::Ok, S::Error> {-    let py = value.py();-    let py_serializer = value.getattr(intern!(py, "__pydantic_serializer__"))?;-    call_pydantic_serializer(py_serializer.cast().map_err(Into::into)?, value, state, do_serialize)+    let serializer = get_pydantic_serializer(value)?;+    call_pydantic_serializer(&serializer, value, state, do_serialize) }  pub(crate) fn call_pydantic_serializer<'py, S: DoSerialize>(pydantic-core/src/serializers/polymorphism_trampoline.rsindex badab29d..1f563350 100644@@ -1,6 +1,6 @@ use std::{borrow::Cow, sync::Arc}; -use pyo3::{prelude::*, types::PyType};+use pyo3::{exceptions::PyAttributeError, prelude::*, types::PyType};  use crate::serializers::{     CombinedSerializer, SerializationState,@@ -49,13 +49,15 @@ impl PolymorphismTrampoline {         if state.check != SerCheck::Strict // strict disables polymorphism             && runtime_polymorphic.unwrap_or(self.enabled_from_config)             && self.is_subclass(value)?-            // stdlib dataclasses do not have a `__pydantic_serializer__`-            && let Ok(serializer) = get_pydantic_serializer(value)         {-            call_pydantic_serializer(&serializer, value, state, do_serialize)-        } else {-            do_serialize.serialize_no_infer(&self.serializer, value, state)+            match get_pydantic_serializer(value) {+                Ok(serializer) => return call_pydantic_serializer(&serializer, value, state, do_serialize),+                // stdlib dataclasses do not have a `__pydantic_serializer__`+                Err(err) if err.is_instance_of::<PyAttributeError>(value.py()) => {}+                Err(err) => return Err(err.into()),+            }         }+        do_serialize.serialize_no_infer(&self.serializer, value, state)     } } tests/test_forward_ref.pyindex 59ebb6a9..e415e81d 100644@@ -7,6 +7,7 @@ from typing import Annotated, Any, Generic, Optional, TypeVar, Union  import pytest from annotated_types import Gt+from pydantic_core import PydanticSerializationError from typing_extensions import get_args, get_origin  # noqa: UP035 from typing_inspection import typing_objects @@ -1731,3 +1732,143 @@ def test_string_annotation_union_type() -> None:      assert typing_objects.is_union(get_origin(annotation))     assert get_args(annotation)[0] is Main+++def test_serialize_instance_of_incomplete_model_by_inference() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    Instances of a model whose `__pydantic_serializer__` is still a `MockValSer`+    (the model was not fully defined at creation and was never rebuilt itself)+    must be serializable when reached through type inference.+    """++    class Foo(BaseModel):+        bar: 'Bar'++    class Bar(BaseModel):+        x: int++    class Outer(BaseModel):+        foo: Foo+        anything: Any = None++    foo = Outer(foo={'bar': {'x': 1}}).foo+    assert not Foo.__pydantic_complete__++    ta = TypeAdapter(Any)+    assert ta.dump_python(foo) == {'bar': {'x': 1}}+    assert ta.dump_json(foo) == b'{"bar":{"x":1}}'+    # serializing triggered a rebuild of the model:+    assert Foo.__pydantic_complete__+++def test_serialize_incomplete_model_union_fallback() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    A field holding an invalid default makes the union serializer fall back to inference+    for the enclosing (incomplete) model instance.+    """++    class StepA(BaseModel):+        type: typing.Literal['a']++    class StepB(BaseModel):+        type: typing.Literal['b']++    class Child(BaseModel):+        settings: 'Settings'+        step: StepA | StepB = Field(default=[])++    class Settings(BaseModel):+        geo: str++    class Root(BaseModel):+        param: list[Child | StepA]++    root = Root(param=[{'settings': {'geo': 'plate'}}])+    assert not Child.__pydantic_complete__++    with pytest.warns(UserWarning, match='Pydantic serializer warnings'):+        assert root.model_dump() == {'param': [{'settings': {'geo': 'plate'}, 'step': []}]}+    with pytest.warns(UserWarning, match='Pydantic serializer warnings'):+        assert root.model_dump_json() == '{"param":[{"settings":{"geo":"plate"},"step":[]}]}'+++def test_serialize_instance_of_incomplete_model_rebuild_fails() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    If the model can't be rebuilt, a clear `PydanticUserError` is raised instead of a `TypeError`.+    """++    class Foo(BaseModel):+        bar: 'UndefinedType'  # noqa: F821++    foo = Foo.model_construct(bar=1)+    ta = TypeAdapter(Any)++    with pytest.raises(PydanticUserError, match='`Foo` is not fully defined'):+        ta.dump_python(foo)+    with pytest.raises(PydanticSerializationError, match='`Foo` is not fully defined'):+        ta.dump_json(foo)+++def test_polymorphic_serialization_of_incomplete_subclass() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    Polymorphic serialization of a subclass instance whose serializer is still a `MockValSer`+    uses the (rebuilt) subclass serializer.+    """++    class Parent(BaseModel):+        a: int++    class Sub(Parent):+        b: 'B'++    class B(BaseModel):+        x: int++    class Holder(BaseModel):+        p: Parent+        s: Sub++    holder = Holder(p={'a': 1}, s={'a': 2, 'b': {'x': 3}})+    assert not Sub.__pydantic_complete__++    assert holder.model_dump(polymorphic_serialization=True) == {+        'p': {'a': 1},+        's': {'a': 2, 'b': {'x': 3}},+    }+    sub = holder.s+    assert TypeAdapter(Parent).dump_python(sub, polymorphic_serialization=True) == {'a': 2, 'b': {'x': 3}}+++def test_serialize_base_model_instance_by_inference() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    A `MockValSer` which cannot rebuild at all raises its own descriptive error.+    """++    instance = BaseModel.__new__(BaseModel)++    with pytest.raises(PydanticUserError, match='BaseModel cannot be instantiated directly'):+        TypeAdapter(Any).dump_python(instance)+++def test_polymorphic_serialization_of_incomplete_subclass_rebuild_fails() -> None:+    """https://github.com/pydantic/pydantic/issues/7713++    If the subclass can't be rebuilt, polymorphic serialization raises instead of silently+    dropping the subclass fields.+    """++    class Parent(BaseModel):+        a: int++    class Sub(Parent):+        b: 'UndefinedType'  # noqa: F821++    sub = Sub.model_construct(a=1, b=2)++    with pytest.raises(PydanticUserError, match='`Sub` is not fully defined'):+        TypeAdapter(Parent).dump_python(sub, polymorphic_serialization=True) 
Merged fix · PR #13922+159 −43
pydantic-core/src/lookup_key.rsindex 7af597934..56ac74dd8 100644@@ -3,7 +3,7 @@ use std::convert::Infallible; use std::fmt;  use pyo3::IntoPyObjectExt;-use pyo3::exceptions::{PyAttributeError, PyTypeError, PyValueError};+use pyo3::exceptions::{PyTypeError, PyValueError}; use pyo3::prelude::*; use pyo3::pybacked::PyBackedStr; use pyo3::types::{PyDict, PyList, PyMapping, PyString};@@ -14,7 +14,7 @@ use smallvec::SmallVec; use crate::build_tools::py_schema_err; use crate::errors::{ErrorType, LocItem, Location, ValError, ValLineError, ValResult, py_err_string}; use crate::input::StringMapping;-use crate::tools::{mapping_get, py_err};+use crate::tools::{mapping_get, py_err, py_get_attrs};  /// The possible choices for an alias value in Python #[derive(FromPyObject)]@@ -454,20 +454,3 @@ impl LookupType {         (self as u8 & other as u8) != 0     } }--// TODO replace with `PyAnyMethods::getattr_opt` once https://github.com/PyO3/pyo3/pull/5985 is merged:-fn py_get_attrs<'py, N>(obj: &Bound<'py, PyAny>, attr_name: N) -> PyResult<Option<Bound<'py, PyAny>>>-where-    N: IntoPyObject<'py, Target = PyString>,-{-    match obj.getattr(attr_name) {-        Ok(attr) => Ok(Some(attr)),-        Err(err) => {-            if err.get_type(obj.py()).is_subclass_of::<PyAttributeError>()? {-                Ok(None)-            } else {-                Err(err)-            }-        }-    }-}pydantic-core/src/serializers/infer.rsindex a99b22baa..ef8ec3b2e 100644@@ -27,7 +27,7 @@ use crate::serializers::shared::serialize_to_python; use crate::serializers::type_serializers; use crate::serializers::type_serializers::any::AnySerializer; use crate::serializers::type_serializers::format::serialize_via_str;-use crate::tools::{py_err, safe_repr};+use crate::tools::{py_err, py_get_attrs, safe_repr};  use super::SchemaSerializer; use super::config::InfNanMode;@@ -660,10 +660,33 @@ pub(crate) fn infer_json_key_known<'a, 'py>(     } } -pub(crate) fn get_pydantic_serializer<'py>(value: &Bound<'py, PyAny>) -> PyResult<Bound<'py, SchemaSerializer>> {+/// Get the `SchemaSerializer` stored as `__pydantic_serializer__` on `value`'s type, or `None`+/// if there's no such attribute (e.g. for stdlib dataclasses).+///+/// If the type isn't fully built yet, pydantic stores a placeholder (`MockValSer`) that rebuilds the+/// type lazily when one of its attributes is accessed from Python. Casting it from Rust bypasses that+/// mechanism, so we explicitly call its `rebuild()` method, which rebuilds the whole type and either+/// returns the built serializer or raises a meaningful error.+pub(crate) fn get_pydantic_serializer<'py>(+    value: &Bound<'py, PyAny>,+) -> PyResult<Option<Bound<'py, SchemaSerializer>>> {     let py = value.py();-    let py_serializer = value.getattr(intern!(py, "__pydantic_serializer__"))?;-    py_serializer.cast_into_exact().map_err(Into::into)+    let Some(py_serializer) = py_get_attrs(value, intern!(py, "__pydantic_serializer__"))? else {+        return Ok(None);+    };+    if let Ok(serializer) = py_serializer.cast_exact::<SchemaSerializer>() {+        return Ok(Some(serializer.clone()));+    }+    if py_serializer.hasattr(intern!(py, "rebuild"))? {+        // The call stack is unrelated to where the type was defined, so don't use a parent frame namespace:+        let kwargs = PyDict::new(py);+        kwargs.set_item(intern!(py, "parent_namespace_depth"), 0)?;+        let rebuilt = py_serializer.call_method(intern!(py, "rebuild"), (), Some(&kwargs))?;+        if !rebuilt.is_none() {+            return Ok(Some(rebuilt.cast_into_exact()?));+        }+    }+    Ok(Some(py_serializer.cast_into_exact()?)) }  /// Serialize `value` as if it had a `__pydantic_serializer__` attribute@@ -674,9 +697,11 @@ fn serialize_pydantic_serializable<'py, S: DoSerialize>(     state: &mut SerializationState<'py>,     do_serialize: S, ) -> Result<S::Ok, S::Error> {-    let py = value.py();-    let py_serializer = value.getattr(intern!(py, "__pydantic_serializer__"))?;-    call_pydantic_serializer(py_serializer.cast().map_err(Into::into)?, value, state, do_serialize)+    match get_pydantic_serializer(value)? {+        Some(serializer) => call_pydantic_serializer(&serializer, value, state, do_serialize),+        // `ObType::PydanticSerializable` was inferred from the attribute being present+        None => Err(unknown_type_error(value).into()),+    } }  pub(crate) fn call_pydantic_serializer<'py, S: DoSerialize>(pydantic-core/src/serializers/polymorphism_trampoline.rsindex badab29da..579b63019 100644@@ -50,7 +50,7 @@ impl PolymorphismTrampoline {             && runtime_polymorphic.unwrap_or(self.enabled_from_config)             && self.is_subclass(value)?             // stdlib dataclasses do not have a `__pydantic_serializer__`-            && let Ok(serializer) = get_pydantic_serializer(value)+            && let Some(serializer) = get_pydantic_serializer(value)?         {             call_pydantic_serializer(&serializer, value, state, do_serialize)         } else {pydantic-core/src/tools.rsindex 6fc6df6d2..7a3533eb9 100644@@ -3,7 +3,7 @@ use core::fmt; use hashbrown::HashTable; use pyo3::PyTraverseError; use pyo3::PyVisit;-use pyo3::exceptions::PyKeyError;+use pyo3::exceptions::{PyAttributeError, PyKeyError}; use pyo3::intern; use pyo3::prelude::*; use pyo3::types::{PyDict, PyMapping, PyString};@@ -202,6 +202,23 @@ pub fn mapping_get<'py>(         .map(|value| if value.is(undefined) { None } else { Some(value) }) } +// TODO replace with `PyAnyMethods::getattr_opt` once https://github.com/PyO3/pyo3/pull/5985 is merged:+pub(crate) fn py_get_attrs<'py, N>(obj: &Bound<'py, PyAny>, attr_name: N) -> PyResult<Option<Bound<'py, PyAny>>>+where+    N: IntoPyObject<'py, Target = PyString>,+{+    match obj.getattr(attr_name) {+        Ok(attr) => Ok(Some(attr)),+        Err(err) => {+            if err.get_type(obj.py()).is_subclass_of::<PyAttributeError>()? {+                Ok(None)+            } else {+                Err(err)+            }+        }+    }+}+ /// A hash table which uses (hashable) Python objects as keys #[derive(Debug)] pub struct PyHashTable<T>(HashTable<PyHashTableEntry<T>>);pydantic/_internal/_mock_val_ser.pyindex fea9713c9..b0344ac06 100644@@ -1,7 +1,7 @@ from __future__ import annotations  from collections.abc import Callable, Iterator, Mapping-from typing import TYPE_CHECKING, Any, Generic, Literal, TypeVar+from typing import TYPE_CHECKING, Any, Generic, Literal, Protocol, TypeVar  from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator @@ -16,6 +16,11 @@ if TYPE_CHECKING:  ValSer = TypeVar('ValSer', bound=SchemaValidator | PluggableSchemaValidator | SchemaSerializer) T = TypeVar('T')+T_co = TypeVar('T_co', covariant=True)+++class AttemptRebuild(Protocol[T_co]):+    def __call__(self, *, parent_namespace_depth: int = ...) -> T_co | None: ...   class MockCoreSchema(Mapping[str, Any]):@@ -30,7 +35,7 @@ class MockCoreSchema(Mapping[str, Any]):         error_message: str,         *,         code: PydanticErrorCodes,-        attempt_rebuild: Callable[[], CoreSchema | None] | None = None,+        attempt_rebuild: AttemptRebuild[CoreSchema] | None = None,     ) -> None:         self._error_message = error_message         self._code: PydanticErrorCodes = code@@ -81,7 +86,7 @@ class MockValSer(Generic[ValSer]):         *,         code: PydanticErrorCodes,         val_or_ser: Literal['validator', 'serializer'],-        attempt_rebuild: Callable[[], ValSer | None] | None = None,+        attempt_rebuild: AttemptRebuild[ValSer] | None = None,     ) -> None:         self._error_message = error_message         self._val_or_ser = SchemaValidator if val_or_ser == 'validator' else SchemaSerializer@@ -99,9 +104,9 @@ class MockValSer(Generic[ValSer]):         getattr(self._val_or_ser, item)         raise PydanticUserError(self._error_message, code=self._code) -    def rebuild(self) -> ValSer | None:+    def rebuild(self, *, parent_namespace_depth: int = 5) -> ValSer | None:         if self._attempt_rebuild:-            val_ser = self._attempt_rebuild()+            val_ser = self._attempt_rebuild(parent_namespace_depth=parent_namespace_depth)             if val_ser is not None:                 return val_ser             else:@@ -121,9 +126,9 @@ def set_type_adapter_mocks(adapter: TypeAdapter) -> None:         f' then call `.rebuild()` on the instance.'     ) -    def attempt_rebuild_fn(attr_fn: Callable[[TypeAdapter], T]) -> Callable[[], T | None]:-        def handler() -> T | None:-            if adapter.rebuild(raise_errors=False, _parent_namespace_depth=5) is not False:+    def attempt_rebuild_fn(attr_fn: Callable[[TypeAdapter], T]) -> AttemptRebuild[T]:+        def handler(*, parent_namespace_depth: int = 5) -> T | None:+            if adapter.rebuild(raise_errors=False, _parent_namespace_depth=parent_namespace_depth) is not False:                 return attr_fn(adapter)             return None @@ -162,9 +167,9 @@ def set_model_mocks(cls: type[BaseModel], undefined_name: str | None = None) ->         f' then call `{cls.__name__}.model_rebuild()`.'     ) -    def attempt_rebuild_fn(attr_fn: Callable[[type[BaseModel]], T]) -> Callable[[], T | None]:-        def handler() -> T | None:-            if cls.model_rebuild(raise_errors=False, _parent_namespace_depth=5) is not False:+    def attempt_rebuild_fn(attr_fn: Callable[[type[BaseModel]], T]) -> AttemptRebuild[T]:+        def handler(*, parent_namespace_depth: int = 5) -> T | None:+            if cls.model_rebuild(raise_errors=False, _parent_namespace_depth=parent_namespace_depth) is not False:                 return attr_fn(cls)             return None @@ -205,9 +210,9 @@ def set_dataclass_mocks(cls: type[PydanticDataclass], undefined_name: str | None         f' then call `pydantic.dataclasses.rebuild_dataclass({cls.__name__})`.'     ) -    def attempt_rebuild_fn(attr_fn: Callable[[type[PydanticDataclass]], T]) -> Callable[[], T | None]:-        def handler() -> T | None:-            if rebuild_dataclass(cls, raise_errors=False, _parent_namespace_depth=5) is not False:+    def attempt_rebuild_fn(attr_fn: Callable[[type[PydanticDataclass]], T]) -> AttemptRebuild[T]:+        def handler(*, parent_namespace_depth: int = 5) -> T | None:+            if rebuild_dataclass(cls, raise_errors=False, _parent_namespace_depth=parent_namespace_depth) is not False:                 return attr_fn(cls)             return None tests/test_construction.pyindex 1f42457e3..15350a693 100644@@ -1,5 +1,5 @@ import pickle-from typing import Any+from typing import Annotated, Any  import pytest from pydantic_core import PydanticUndefined, ValidationError@@ -669,3 +669,26 @@ def test_model_construct_with_alias_choices_and_path() -> None:     assert MyModel.model_construct(a='a_value').a == 'a_value'     assert MyModel.model_construct(aaa='a_value').a == 'a_value'     assert MyModel.model_construct(AAA={'aaa': 'a_value'}).a == 'a_value'+++@pytest.mark.xfail(+    reason='`model_construct()` does not rebuild incomplete models, and relies on `FieldInfo` instances '+    'that may be incomplete (here, the `Field()` metadata is part of the unevaluated annotation).',+)+def test_model_construct_incomplete_model() -> None:+    # When making this test work, look at `test_string_annotation_union_type()` which assumes `model_construct()`+    # doesn't rebuild the model.+    class Model(BaseModel):+        a: 'Annotated[Later | None, Field(alias="A")]'+        b: 'Annotated[Later | None, Field(default=None)]'++    class Later(BaseModel):+        pass++    assert Model.__pydantic_complete__ is False++    later = Later()+    m = Model.model_construct(A=later)+    assert m.a is later+    assert m.b is None+    assert m.model_fields_set == {'a'}tests/test_forward_ref.pyindex 59ebb6a94..f0e69b73e 100644@@ -1731,3 +1731,44 @@ def test_string_annotation_union_type() -> None:      assert typing_objects.is_union(get_origin(annotation))     assert get_args(annotation)[0] is Main+++def test_serialization_inference_rebuilds_incomplete_model(create_module) -> None:+    """https://github.com/pydantic/pydantic/issues/7713"""++    # Necessary to have classes defined at module level for forward ref+    # to resolve when rebuilding model from pydantic-core:+    @create_module+    def module():+        from typing import Optional++        from pydantic import BaseModel++        class B(BaseModel):+            a: Optional['A'] = None++        class A(BaseModel):+            b: B++    a = module.A.model_validate({'b': {}})+    assert module.B.__pydantic_complete__ is False++    assert TypeAdapter(Any).dump_python(a.b) == {'a': None}+    assert module.B.__pydantic_complete__ is True+    assert a.model_dump(serialize_as_any=True) == {'b': {'a': None}}+++def test_serialization_inference_unbuildable_model() -> None:+    """When rebuilt from pydantic-core, the caller's frame namespace should not be used to resolve annotations."""++    class Model(BaseModel):+        x: 'Undefined'  # noqa: F821++    def serialize(m: Model) -> Any:+        Undefined = int  # noqa: F841+        return TypeAdapter(Any).dump_python(m)++    m = Model.model_construct(x=1)+    with pytest.raises(PydanticUserError, match='`Model` is not fully defined; you should define `Undefined`'):+        serialize(m)+    assert Model.__pydantic_complete__ is Falsetests/test_serialize_as_any.pyindex 2bf6091c6..4bde976ec 100644@@ -209,3 +209,25 @@ def test_serialize_as_any_annotation_with_incorrect_list_el_type() -> None:     # notably, the warning is not raised when using the SerializeAsAny annotation     ta = TypeAdapter(SerializeAsAny[list[int]])     assert ta.dump_python(['a', 'b', 'c']) == ['a', 'b', 'c']+++@pytest.mark.parametrize('kwargs', [{'serialize_as_any': True}, {'polymorphic_serialization': True}])+def test_serialize_as_any_deferred_subclass(kwargs) -> None:+    """https://github.com/pydantic/pydantic/issues/13647"""++    class Base(BaseModel):+        x: int = 1++    class Sub(Base):+        model_config = ConfigDict(defer_build=True)+        y: int = 2++    class Model(BaseModel):+        b: Base++    # `model_construct()` currently doesn't trigger a rebuild of `Sub`:+    m = Model(b=Sub.model_construct(x=1, y=2))+    assert Sub.__pydantic_complete__ is False++    assert m.model_dump(**kwargs) == {'b': {'x': 1, 'y': 2}}+    assert Sub.__pydantic_complete__ is True