Jump to solution
Verify

The Fix

pip install pydantic==2.11.0

Based on closed pydantic/pydantic issue #11574 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -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.
repro.py
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)
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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

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…”
@Viicos · 2025-03-18 · source
“Thanks for the quick reply”
@olivierdelree · 2025-03-18 · source
“Thanks for the bug report, I went with straightforward fix which should land in 2.11.”
@Viicos · 2025-03-18 · source

Failure Signature (Search String)

  • print(foo)

Error Message

Stack trace
error.txt
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

repro.py
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

When NOT to use: This fix is not suitable if cached properties should not be eagerly evaluated.

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).

When NOT to use: This fix is not suitable if cached properties should not be eagerly evaluated.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not suitable if cached properties should not be eagerly evaluated.

Verify Fix

verify
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

VersionStatus
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.