The Fix
pip install pydantic==2.9.2
Based on closed pydantic/pydantic issue #10397 Β· 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%.
@@ -1899,7 +1899,7 @@ def get_title_from_name(self, name: str) -> str:
The title.
"""
- return name.title().replace('_', ' ')
+ return name.title().replace('_', ' ').strip()
from pydantic import BaseModel, Field
class MyModel(BaseModel):
private_field: int = Field(alias='_private_field', serialization_alias='_private_field')
MyModel.model_json_schema()
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.9.2\nWhen NOT to use: Do not apply this fix if the leading space is intentionally required for specific formatting.\n\n
Why This Fix Works in Production
- Trigger: model_json_schema() generates an unexpected leading space in title when using a serialization_alias with a `_` prefix
- Mechanism: Leading whitespace is introduced when generating titles from serialization aliases with a '_' prefix
- Why the fix works: Strips leading whitespace from JSON Schema title generation when using serialization aliases. (first fixed release: 2.9.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.11 in real deployments (not just unit tests).
- Leading whitespace is introduced when generating titles from serialization aliases with a '_' prefix
- Production symptom (often without a traceback): model_json_schema() generates an unexpected leading space in title when using a serialization_alias with a `_` prefix
Proof / Evidence
- GitHub issue: #10397
- Fix PR: https://github.com/pydantic/pydantic/pull/10404
- First fixed release: 2.9.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.63
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.9.2
- Mode: fixed_only
- Outcome: ok
Logs
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
βHi @eyurtsev, Thanks for bringing this to our attention! I've opened a PR with a fix πβ
Failure Signature (Search String)
- model_json_schema() generates an unexpected leading space in title when using a serialization_alias with a `_` prefix
- When generating model_schema() with a serialization alias the automatic title is getting an unexpected leading space. I would expect either the `_` to be present or else to be
Copy-friendly signature
Failure Signature
-----------------
model_json_schema() generates an unexpected leading space in title when using a serialization_alias with a `_` prefix
When generating model_schema() with a serialization alias the automatic title is getting an unexpected leading space. I would expect either the `_` to be present or else to be stripped rather than replaced with a space.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
model_json_schema() generates an unexpected leading space in title when using a serialization_alias with a `_` prefix
When generating model_schema() with a serialization alias the automatic title is getting an unexpected leading space. I would expect either the `_` to be present or else to be stripped rather than replaced with a space.
Minimal Reproduction
from pydantic import BaseModel, Field
class MyModel(BaseModel):
private_field: int = Field(alias='_private_field', serialization_alias='_private_field')
MyModel.model_json_schema()
Environment
- Python: 3.11
- Pydantic: 2
What Broke
Generated JSON Schema titles contain unexpected leading spaces, affecting API responses.
Why It Broke
Leading whitespace is introduced when generating titles from serialization aliases with a '_' prefix
Fix Options (Details)
Option A β Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.9.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/10404
First fixed release: 2.9.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not apply this fix if the leading space is intentionally required for specific formatting.
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.9.2 | Fixed |
Related Issues
No related fixes found.
Sources
We donβt republish the full GitHub discussion text. Use the links above for context.