The Fix
pip install pydantic==2.7.1
Based on closed pydantic/pydantic issue #9122 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -721,7 +721,7 @@ def __copy__(self: Model) -> Model:
_object_setattr(m, '__pydantic_fields_set__', copy(self.__pydantic_fields_set__))
- if self.__pydantic_private__ is None:
+ if not hasattr(self, '__pydantic_private__') or self.__pydantic_private__ is None:
_object_setattr(m, '__pydantic_private__', None)
pydantic-core version: 2.16.3
pydantic-core build: profile=release pgo=true
install path: /home/dave/axial/src/services/axial-api.git/venv/lib/python3.11/site-packages/pydantic
python version: 3.11.8 (main, Feb 25 2024, 16:41:26) [GCC 9.4.0]
platform: Linux-5.15.0-1056-aws-x86_64-with-glibc2.31
related packages: fastapi-0.110.0 typing_extensions-4.10.0 mypy-1.9.0
commit: unknown
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.7.1\nWhen NOT to use: Do not use this fix if the model does not define model_post_init.\n\n
Why This Fix Works in Production
- Trigger: Cannot use model_copy(deep=True) on a model instance which was created using model_construct, when there is a model_post_init in the class hierarchy.
- Mechanism: The model_copy method fails due to an unset __pydantic_private__ attribute when using model_construct
- Why the fix works: Addresses an AttributeError that occurs when using model_copy on a model constructed with model_construct and having a model_post_init method. (first fixed release: 2.7.1).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Shows up under Python 3.11 in real deployments (not just unit tests).
- The model_copy method fails due to an unset __pydantic_private__ attribute when using model_construct
- Surfaces as: File "/home/dave/test_deepcopy.py", line 21, in <module>\n main()\n File "/home/dave/test_deepcopy.py", line 17, in main\n m.model_copy(deep=True)\n File…
Proof / Evidence
- GitHub issue: #9122
- Fix PR: https://github.com/pydantic/pydantic/pull/9168
- First fixed release: 2.7.1
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.49
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Looks like a bug! Thanks for reporting this!”
“It occurs to me that maybe this simple patch would be sufficient to fix this”
“@babygrimes, Please open a PR, I'd be happy to review!”
Failure Signature (Search String)
- Cannot use model_copy(deep=True) on a model instance which was created using model_construct, when there is a model_post_init in the class hierarchy.
Error Message
Stack trace
Error Message
-------------
File "/home/dave/test_deepcopy.py", line 21, in <module>\n main()\n File "/home/dave/test_deepcopy.py", line 17, in main\n m.model_copy(deep=True)\n File "/home/dave/axial/src/services/axial-api.git/venv/lib/python3.11/site-packages/pydantic/main.py", line 266, in model_copy\n copied = self.__deepcopy__() if deep else self.__copy__()\n ^^^^^^^^^^^^^^^^^^^\n File "/home/dave/axial/src/services/axial-api.git/venv/lib/python3.11/site-packages/pydantic/main.py", line 723, in __deepcopy__\n if self.__pydantic_private__ is None:\n ^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/home/dave/axial/src/services/axial-api.git/venv/lib/python3.11/site-packages/pydantic/main.py", line 764, in __getattr__\n return super().__getattribute__(item) # Raises AttributeError if appropriate\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Minimal Reproduction
pydantic-core version: 2.16.3
pydantic-core build: profile=release pgo=true
install path: /home/dave/axial/src/services/axial-api.git/venv/lib/python3.11/site-packages/pydantic
python version: 3.11.8 (main, Feb 25 2024, 16:41:26) [GCC 9.4.0]
platform: Linux-5.15.0-1056-aws-x86_64-with-glibc2.31
related packages: fastapi-0.110.0 typing_extensions-4.10.0 mypy-1.9.0
commit: unknown
Environment
- Python: 3.11
- Pydantic: 2
What Broke
Users experience AttributeError when attempting to copy models constructed with model_construct.
Why It Broke
The model_copy method fails due to an unset __pydantic_private__ attribute when using model_construct
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.7.1
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option D — Guard side-effects with OnceOnly Guardrail for side-effects
Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.
- Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
- Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
- Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
Show example snippet (optional)
from onceonly import OnceOnly
import os
once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True)
# Stable idempotency key per real side-effect.
# Use a request id / job id / webhook delivery id / Stripe event id, etc.
event_id = "evt_..." # replace
key = f"stripe:webhook:{event_id}"
res = once.check_lock(key=key, ttl=3600)
if res.duplicate:
return {"status": "already_processed"}
# Safe to execute the side-effect exactly once.
handle_event(event_id)
Fix reference: https://github.com/pydantic/pydantic/pull/9168
First fixed release: 2.7.1
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if the model does not define model_post_init.
- Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.
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.7.1 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.