Jump to solution
Verify

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

Jump to Verify Open PR/Commit
@@ -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:
repro.py
from pydantic import BaseModel class MyModel(BaseModel): _private_attr: str thing = MyModel() print(thing.model_dump())
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.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).
Production impact:
  • 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

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
affected (exit=None)
fixed (exit=0)
{}

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…”
@mvernacc · 2024-08-16 · confirmation · source
“Closing as per https://github.com/pydantic/pydantic/issues/9048#issuecomment-2294362155.”
@Viicos · 2025-01-06 · confirmation · source
“@mvernacc > I ran into the same issue”
@TeeSeal · 2024-08-19 · confirmation · source
“@quentin-ag static type checkers _do not_ understand annotated metadata. Read more here.”
@Viicos · 2025-09-13 · source

Failure Signature (Search String)

  • KeyError: '_private_attr'

Error Message

Stack trace
error.txt
Error Message ------------- KeyError: '_private_attr'

Minimal Reproduction

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

When NOT to use: This fix should not be applied if the model relies on private attributes being exposed in the constructor.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

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

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