Backtestspydanticrun-20260929-204949#13453
RootModel.model_dump() typing signature rejects supported serialization arguments
The harness couldn't finish this ticket.
api-error at solve. No judgment was produced for this ticket. It counts as an error in the run total.
Solve
—
Wall-clock time in the container
Full test suite
Not run
The harness did not reach the test stage
Contamination audit
Unknown
See the audit notes for this run
Patch size
0vs 0 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
RootModel inherits the runtime implementation of BaseModel.model_dump(), but defines a separate declaration under TYPE_CHECKING to preserve its more precise return type.
That declaration has diverged from the base method:
contextis restricted todict[str, Any] | Noneinstead ofAny | None.fallbackis missing.polymorphic_serializationis missing.
As a result, Mypy, Pyright, and Pyrefly reject valid calls that succeed at runtime.
All three calls in the example below succeed at runtime.
Mypy currently reports errors equivalent to:
Argument "context" to "model_dump" of "RootModel" has incompatible type "str"
Unexpected keyword argument "fallback" for "model_dump" of "RootModel"
Unexpected keyword argument "polymorphic_serialization" for "model_dump" of "RootModel"
Pyright and Pyrefly report the same signature incompatibilities.
Expected behavior
The type-checking-only RootModel.model_dump() declaration should accept the same parameters, annotations, defaults, and ordering as BaseModel.model_dump(), while preserving the more precise RootModel return annotation.
Cause
The declaration appears to have drifted as the base method evolved:
- Serialization context originally updated both signatures.
BaseModel.contextwas later widened toAny.fallbackwas subsequently added toBaseModel.model_dump().polymorphic_serializationwas later added toBaseModel.model_dump().
Those later changes were not propagated to the manually maintained RootModel declaration.
Proposed fix
Update only the static RootModel.model_dump() signature and add focused regression cases to tests/typechecking/root_model.py.
No runtime serialization behavior needs to change.
I have prepared and locally validated a focused two-file fix. The following checks pass:
- Mypy type-checking suite
- Pyright type-checking suite
- Pyrefly type-checking suite
- Focused RootModel serialization tests
- Ruff lint and formatting checks
git diff --check
I would be happy to submit the PR after the issue is reviewed and assigned.
Example Code
from typing import Any
from pydantic import BaseModel, RootModel, SerializationInfo, model_serializer
class ContextRoot(RootModel[str]):
@model_serializer
def serialize_root(self, info: SerializationInfo) -> str:
return f'{info.context}:{self.root}'
class Unknown:
pass
class Base(BaseModel):
x: int
class Sub(Base):
y: int
ContextRoot(root='value').model_dump(context='prefix')
RootModel[Any](root=Unknown()).model_dump(
mode='json',
fallback=lambda _: 'fallback',
)
RootModel[list[Base]](root=[Sub(x=1, y=2)]).model_dump(
polymorphic_serialization=True,
)
Python, Pydantic & OS Version
pydantic version: 2.14.0a1
pydantic-core version: 2.47.0
pydantic-core build: profile=debug pgo=false
python version: 3.13.5 | packaged by Anaconda, Inc. | (main, Jun 12 2025, 11:23:37) [Clang 14.0.6 ]
platform: macOS-15.7.3-arm64-arm-64bit-Mach-O
related packages: mypy-1.18.2 typing_extensions-4.15.0 pydantic-settings-2.7.1 pyright-1.1.407
commit: b281d5aedThe two patches
No patch was produced.No patch was produced.What the agent reported
Failed to authenticate: OAuth session expired and could not be refreshed