The Fix
pip install pydantic==2.11.0
Based on closed pydantic/pydantic issue #11574 · PR/commit linked
@@ -1195,6 +1195,13 @@ def __repr__(self) -> str:
def __repr_args__(self) -> _repr.ReprArgs:
+ # Eagerly create the repr of computed fields, as this may trigger access of cached properties and as such
+ # modify the instance's `__dict__`. If we don't do it now, it could happen when iterating over the `__dict__`
+ # below if the instance happens to be referenced in a field, and would modify the `__dict__` size *during* iteration.
from __future__ import annotations
from functools import cached_property
from pydantic import BaseModel, computed_field
class Model(BaseModel):
parent: Model | None = None
children: list[Model] = []
@computed_field
@cached_property
def expensive(self) -> bool:
return True
foo = Model()
bar = Model()
foo.children.append(bar)
bar.parent = foo
print(foo)
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install pydantic==2.11.0\nWhen NOT to use: This fix is not suitable if cached properties should not be eagerly evaluated.\n\nOption C — Workaround\nis to set `@computed_field(repr=True)`. Would this work for you, or do you still want the cached property to be included in the repr? (And thus computed).\nWhen NOT to use: This fix is not suitable if cached properties should not be eagerly evaluated.\n\n
Why This Fix Works in Production
- Trigger: print(foo)
- Mechanism: The cached_property causes a RuntimeError due to dictionary size change during iteration
- Why the fix works: Fixes a RuntimeError when computing model string representation involving cached properties and self-referenced models. (first fixed release: 2.11.0).
Why This Breaks in Prod
- Shows up under Python 3.10 in real deployments (not just unit tests).
- The cached_property causes a RuntimeError due to dictionary size change during iteration
- Surfaces as: Traceback (most recent call last):
Proof / Evidence
- GitHub issue: #11574
- Fix PR: https://github.com/pydantic/pydantic/pull/11579
- First fixed release: 2.11.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.41
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“An alternative workaround is to set @computed_field(repr=True). Would this work for you, or do you still want the cached property to be included in the…”
“Thanks for the quick reply”
“Thanks for the bug report, I went with straightforward fix which should land in 2.11.”
Failure Signature (Search String)
- print(foo)
Error Message
Stack trace
Error Message
-------------
Traceback (most recent call last):
File "/home/username/script.py", line 24, in <module>
print(foo)
File "/home/username/miniforge3/envs/pydantic_issue/lib/python3.10/site-packages/pydantic/main.py", line 1127, in __str__
return self.__repr_str__(' ')
File "/home/username/miniforge3/envs/pydantic_issue/lib/python3.10/site-packages/pydantic/_internal/_repr.py", line 61, in __repr_str__
return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
File "/home/username/miniforge3/envs/pydantic_issue/lib/python3.10/site-packages/pydantic/_internal/_repr.py", line 61, in <genexpr>
return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
File "/home/username/miniforge3/envs/pydantic_issue/lib/python3.10/site-packages/pydantic/main.py", line 1099, in __repr_args__
for k, v in self.__dict__.items():
RuntimeError: dictionary changed size during iteration
Minimal Reproduction
from __future__ import annotations
from functools import cached_property
from pydantic import BaseModel, computed_field
class Model(BaseModel):
parent: Model | None = None
children: list[Model] = []
@computed_field
@cached_property
def expensive(self) -> bool:
return True
foo = Model()
bar = Model()
foo.children.append(bar)
bar.parent = foo
print(foo)
Environment
- Python: 3.10
- Pydantic: 2
What Broke
Attempting to display a model with cached properties results in a RuntimeError.
Why It Broke
The cached_property causes a RuntimeError due to dictionary size change during iteration
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.11.0
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option C — Workaround Temporary workaround
is to set `@computed_field(repr=True)`. Would this work for you, or do you still want the cached property to be included in the repr? (And thus computed).
Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.
Fix reference: https://github.com/pydantic/pydantic/pull/11579
First fixed release: 2.11.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not suitable if cached properties should not be eagerly evaluated.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
- Upgrade behind a canary and run integration tests against the canary before 100% rollout.
Version Compatibility Table
| Version | Status |
|---|---|
| 2.11.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.