The Fix
pip install pydantic==2.12.5
Based on closed pydantic/pydantic issue #12505 · PR/commit linked
Production note: This tends to surface only under concurrency. Reproduce with load tests and watch for lock contention/cancellation paths.
@@ -11,7 +11,7 @@
from typing_extensions import TypeGuard, dataclass_transform
-from ._internal import _config, _decorators, _namespace_utils, _typing_extra
+from ._internal import _config, _decorators, _mock_val_ser, _namespace_utils, _typing_extra
from ._internal import _dataclasses as _pydantic_dataclasses
from __future__ import annotations
import threading
from pandera import DataFrameModel
from pandera.typing import DataFrame
from pydantic.dataclasses import dataclass
@dataclass
class Base:
"""
Dropping this inheritance will result in the following error:
Thread 1: 'MyObject' object has no attribute '__pydantic_validator__'
occuring during object instantiation instead of
Thread 1: 'MyObject' object has no attribute 'my_attr_1'
during attribute access.
"""
pass
@dataclass
class MyObject(Base):
my_attr_1: MyNestedObject | None
my_attr_2: DataFrame[DataFrameModel] | None
@dataclass
class MyNestedObject:
pass
def create_obj(thread_id: int, results: list, errors: list) -> None:
try:
obj = MyObject(my_attr_1=None, my_attr_2=None)
_ = obj.my_attr_1
except Exception as e:
errors.append((thread_id, e))
else:
results.append(thread_id)
def main():
results = []
errors = []
threads = []
for i in range(10):
t = threading.Thread(target=create_obj, args=(i, results, errors))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(f"Results: {len(results)} successful, {len(errors)} errors")
print()
if errors:
print("Errors encountered:")
for thread_id, error in errors:
print(f" Thread {thread_id}: {error}")
print()
if __name__ == "__main__":
exit(main())
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.12.5\nWhen NOT to use: This fix should not be used if the application does not involve concurrent requests.\n\n
Why This Fix Works in Production
- Trigger: Dataclass instances are missing attributes at runtime
- Mechanism: Race condition during concurrent requests leads to incomplete initialization of dataclass instances
- Why the fix works: Prevents the deletion of the mock validator/serializer in `rebuild_dataclass()`, addressing a race condition that caused missing attributes in dataclass instances. (first fixed release: 2.12.5).
- If left unfixed, failures can be intermittent under concurrency (hard to reproduce; shows up as sporadic 5xx/timeouts).
Why This Breaks in Prod
- Race condition during concurrent requests leads to incomplete initialization of dataclass instances
- Production symptom (often without a traceback): Dataclass instances are missing attributes at runtime
Proof / Evidence
- GitHub issue: #12505
- Fix PR: https://github.com/pydantic/pydantic/pull/12513
- First fixed release: 2.12.5
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.42
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“### Initial Checks - [x] I confirm that I'm using Pydantic V2 ### Description Hi everyone, I've encountered a strange bug that was quite hard to track down. The symptom was that accessing a certain attribute of an object (an instance of pyd”
Failure Signature (Search String)
- Dataclass instances are missing attributes at runtime
- I still don't completely understand the details. It seems to me that it's a race condition that leads to incomplete initialization, but I haven't been able to make more headway
Copy-friendly signature
Failure Signature
-----------------
Dataclass instances are missing attributes at runtime
I still don't completely understand the details. It seems to me that it's a race condition that leads to incomplete initialization, but I haven't been able to make more headway into the problem.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Dataclass instances are missing attributes at runtime
I still don't completely understand the details. It seems to me that it's a race condition that leads to incomplete initialization, but I haven't been able to make more headway into the problem.
Minimal Reproduction
from __future__ import annotations
import threading
from pandera import DataFrameModel
from pandera.typing import DataFrame
from pydantic.dataclasses import dataclass
@dataclass
class Base:
"""
Dropping this inheritance will result in the following error:
Thread 1: 'MyObject' object has no attribute '__pydantic_validator__'
occuring during object instantiation instead of
Thread 1: 'MyObject' object has no attribute 'my_attr_1'
during attribute access.
"""
pass
@dataclass
class MyObject(Base):
my_attr_1: MyNestedObject | None
my_attr_2: DataFrame[DataFrameModel] | None
@dataclass
class MyNestedObject:
pass
def create_obj(thread_id: int, results: list, errors: list) -> None:
try:
obj = MyObject(my_attr_1=None, my_attr_2=None)
_ = obj.my_attr_1
except Exception as e:
errors.append((thread_id, e))
else:
results.append(thread_id)
def main():
results = []
errors = []
threads = []
for i in range(10):
t = threading.Thread(target=create_obj, args=(i, results, errors))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(f"Results: {len(results)} successful, {len(errors)} errors")
print()
if errors:
print("Errors encountered:")
for thread_id, error in errors:
print(f" Thread {thread_id}: {error}")
print()
if __name__ == "__main__":
exit(main())
Environment
- Pydantic: 2
What Broke
Accessing attributes of dataclass instances results in exceptions due to missing attributes.
Why It Broke
Race condition during concurrent requests leads to incomplete initialization of dataclass instances
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.12.5
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/12513
First fixed release: 2.12.5
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be used if the application does not involve concurrent requests.
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.
- Add a stress test that runs high-concurrency workloads and fails on thread dumps / blocked locks.
- Enable watchdog dumps in prod (faulthandler, thread dump endpoint) to capture deadlocks quickly.
Version Compatibility Table
| Version | Status |
|---|---|
| 2.12.5 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.