The Fix
pip install pydantic==2.11.5
Based on closed pydantic/pydantic issue #11805 · 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%.
@@ -4,6 +4,7 @@
import ast
import inspect
+import sys
import textwrap
from typing import Any
class Model(BaseModel, use_attribute_docstrings=True):
x: list[str] | None
'''this is x'''
print('Model schema')
print(Model.model_json_schema())
# wrong docstring
import pydantic._internal._fields
from pydantic._internal._fields import _update_fields_from_docstrings
from functools import partial
pydantic._internal._fields._update_fields_from_docstrings = partial(
_update_fields_from_docstrings,
use_inspect=True
)
# inject
del Model
class Model(BaseModel, use_attribute_docstrings=True):
x: list[str]|None
'''this is x'''
print('Model schema after injecting `_update_fields_from_docstrings`')
print(Model.model_json_schema())
# now fixed
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.11.5\nWhen NOT to use: Do not use if it changes public behavior or if the failure cannot be reproduced.\n\n
Why This Fix Works in Production
- Trigger: when `use_attribute_docstrings=True`, `DocstringVisitor` fail to grab field docs in unknown reason when using
- Mechanism: The issue arises from the failure to retrieve field docstrings due to improper source extraction logic
- Why the fix works: Ensures that `inspect.getsourcelines()` is used for docstring extraction on Python 3.13 and greater, fixing issues with field docstrings not being retrieved correctly. (first fixed release: 2.11.5).
- 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.13 in real deployments (not just unit tests).
- The issue arises from the failure to retrieve field docstrings due to improper source extraction logic
- Production symptom (often without a traceback): when `use_attribute_docstrings=True`, `DocstringVisitor` fail to grab field docs in unknown reason when using
Proof / Evidence
- GitHub issue: #11805
- Fix PR: https://github.com/pydantic/pydantic/pull/11829
- First fixed release: 2.11.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.59
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“The default use_attribute_docstrings behavior is to extract the docstrings by walking up the frames from the call stack”
“Here is a MRE: proj structure: in __init__.py: in test.py:”
“I wouldn't expect things to work correctly if you are manually modifying such dunder attributes”
“> The default use_attribute_docstrings behavior is to extract the docstrings by walking up the frames from the call stack”
Failure Signature (Search String)
- when `use_attribute_docstrings=True`, `DocstringVisitor` fail to grab field docs in unknown reason when using
- print(Model.model_json_schema())
Copy-friendly signature
Failure Signature
-----------------
when `use_attribute_docstrings=True`, `DocstringVisitor` fail to grab field docs in unknown reason when using
print(Model.model_json_schema())
Error Message
Signature-only (no traceback captured)
Error Message
-------------
when `use_attribute_docstrings=True`, `DocstringVisitor` fail to grab field docs in unknown reason when using
print(Model.model_json_schema())
Minimal Reproduction
class Model(BaseModel, use_attribute_docstrings=True):
x: list[str] | None
'''this is x'''
print('Model schema')
print(Model.model_json_schema())
# wrong docstring
import pydantic._internal._fields
from pydantic._internal._fields import _update_fields_from_docstrings
from functools import partial
pydantic._internal._fields._update_fields_from_docstrings = partial(
_update_fields_from_docstrings,
use_inspect=True
)
# inject
del Model
class Model(BaseModel, use_attribute_docstrings=True):
x: list[str]|None
'''this is x'''
print('Model schema after injecting `_update_fields_from_docstrings`')
print(Model.model_json_schema())
# now fixed
Environment
- Python: 3.13
- Pydantic: 2
What Broke
Field docstrings are not retrieved, leading to incorrect model schema generation.
Why It Broke
The issue arises from the failure to retrieve field docstrings due to improper source extraction logic
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.11.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/11829
First fixed release: 2.11.5
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use if it changes public behavior or if the failure cannot be reproduced.
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.11.5 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.