The Fix
pip install pydantic==2.7.2
Based on closed pydantic/pydantic issue #9048 · 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%.
@@ -33,12 +33,14 @@
from ..fields import Field as PydanticModelField
from ..fields import FieldInfo, ModelPrivateAttr
+ from ..fields import PrivateAttr as PydanticModelPrivateAttr
from ..main import BaseModel
else:
from pydantic import BaseModel
class MyModel(BaseModel):
_private_attr: str
thing = MyModel()
print(thing.model_dump())
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.2\nWhen NOT to use: This fix should not be applied if the model relies on private attributes being exposed in the constructor.\n\n
Why This Fix Works in Production
- Trigger: KeyError: '_private_attr'
- Mechanism: Private model attributes were incorrectly included in the __init__ function signature, causing type checker errors
- Why the fix works: Addresses the issue of private model attributes being incorrectly handled by type checkers by adding `PrivateAttr` to the list of field specifiers in Pydantic. (first fixed release: 2.7.2).
- 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.9 in real deployments (not just unit tests).
- Private model attributes were incorrectly included in the __init__ function signature, causing type checker errors
- Surfaces as: KeyError: '_private_attr'
Proof / Evidence
- GitHub issue: #9048
- Fix PR: https://github.com/pydantic/pydantic/pull/9293
- First fixed release: 2.7.2
- 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.77
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: pydantic
- Fixed: 2.7.2
- Mode: fixed_only
- Outcome: ok
Logs
{}
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“@TeeSeal I ran into the same issue. Since https://github.com/pydantic/pydantic/pull/9293 was merged, I was now able to solve the pyright issue by marking the attribute as…”
“Closing as per https://github.com/pydantic/pydantic/issues/9048#issuecomment-2294362155.”
“@mvernacc > I ran into the same issue”
“@quentin-ag static type checkers _do not_ understand annotated metadata. Read more here.”
Failure Signature (Search String)
- KeyError: '_private_attr'
Error Message
Stack trace
Error Message
-------------
KeyError: '_private_attr'
Minimal Reproduction
from pydantic import BaseModel
class MyModel(BaseModel):
_private_attr: str
thing = MyModel()
print(thing.model_dump())
Environment
- Python: 3.9
- Pydantic: 2
What Broke
Type checkers report errors when instantiating models with private attributes, leading to confusion for developers.
Why It Broke
Private model attributes were incorrectly included in the __init__ function signature, causing type checker errors
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.7.2
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/pydantic/pydantic/pull/9293
First fixed release: 2.7.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the model relies on private attributes being exposed in the constructor.
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.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.