The Fix
pip install pydantic==1.10.18
Based on closed pydantic/pydantic issue #10153 · PR/commit linked
@@ -4,13 +4,14 @@
import builtins
import operator
+import sys
import typing
import warnings
# from __future__ import annotations
import os
from pydantic import BaseModel, PrivateAttr
from typing_extensions import Annotated
class ParentModel(BaseModel):
name: str
age: int
class ChildModel(ParentModel):
_cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)]
child_instance = ChildModel(name="John Doe", age=30)
count = child_instance._cpu_count
print(count)
# > 16
# -----------------
from __future__ import annotations
import os
from pydantic import BaseModel, PrivateAttr
from typing_extensions import Annotated
class ParentModel(BaseModel):
name: str
age: int
class ChildModel(ParentModel):
_cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)]
child_instance = ChildModel(name="John Doe", age=30)
count = child_instance._cpu_count
print(count)
# >Traceback (most recent call last):
# > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 807, in # >__getattr__
# > return self.__pydantic_private__[item] # type: ignore
# > ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
# >KeyError: '_cpu_count'
# >
# >The above exception was the direct cause of the following exception:
# >
# >Traceback (most recent call last):
# > File "/home/xx/xx/testing/pydanticbugg.py", line 19, in <module>
# > count = child_instance._cpu_count
# > ^^^^^^^^^^^^^^^^^^^^^^^^^
# > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 809, in # >__getattr__
# > raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') from exc
# > AttributeError: 'ChildModel' object has no attribute '_cpu_count'
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==1.10.18\nWhen NOT to use: This fix should not be used if the `Annotated` pattern is not supported by the type checker.\n\n
Why This Fix Works in Production
- Trigger: AttributeError when using __future__ import with PrivateAttr in Pydantic
- Mechanism: The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations
- Why the fix works: Adds support for stringified annotations when using `PrivateAttr` with `Annotated`, resolving the AttributeError issue. (first fixed release: 1.10.18).
Why This Breaks in Prod
- Shows up under Python 3.12 in real deployments (not just unit tests).
- The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations
- Surfaces as: AttributeError when using __future__ import with PrivateAttr in Pydantic
Proof / Evidence
- GitHub issue: #10153
- Fix PR: https://github.com/pydantic/pydantic/pull/10157
- First fixed release: 1.10.18
- 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.44
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I would suggest avoiding the Annotated pattern for private attrs, as type checkers won't recognize them: But as it was supported in https://github.com/pydantic/pydantic/pull/8004, we'll have…”
“Should be fixed by https://github.com/pydantic/pydantic/pull/10157”
Failure Signature (Search String)
- AttributeError when using __future__ import with PrivateAttr in Pydantic
Error Message
Stack trace
Error Message
-------------
AttributeError when using __future__ import with PrivateAttr in Pydantic
Minimal Reproduction
# from __future__ import annotations
import os
from pydantic import BaseModel, PrivateAttr
from typing_extensions import Annotated
class ParentModel(BaseModel):
name: str
age: int
class ChildModel(ParentModel):
_cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)]
child_instance = ChildModel(name="John Doe", age=30)
count = child_instance._cpu_count
print(count)
# > 16
# -----------------
from __future__ import annotations
import os
from pydantic import BaseModel, PrivateAttr
from typing_extensions import Annotated
class ParentModel(BaseModel):
name: str
age: int
class ChildModel(ParentModel):
_cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)]
child_instance = ChildModel(name="John Doe", age=30)
count = child_instance._cpu_count
print(count)
# >Traceback (most recent call last):
# > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 807, in # >__getattr__
# > return self.__pydantic_private__[item] # type: ignore
# > ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
# >KeyError: '_cpu_count'
# >
# >The above exception was the direct cause of the following exception:
# >
# >Traceback (most recent call last):
# > File "/home/xx/xx/testing/pydanticbugg.py", line 19, in <module>
# > count = child_instance._cpu_count
# > ^^^^^^^^^^^^^^^^^^^^^^^^^
# > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 809, in # >__getattr__
# > raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') from exc
# > AttributeError: 'ChildModel' object has no attribute '_cpu_count'
Environment
- Python: 3.12
- Pydantic: 2
What Broke
Users encounter an `AttributeError` when accessing private attributes in models with future annotations.
Why It Broke
The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.18
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/10157
First fixed release: 1.10.18
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be used if the `Annotated` pattern is not supported by the type checker.
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 |
|---|---|
| 1.10.18 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.