Jump to solution
Verify

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

Jump to Verify Open PR/Commit
@@ -1899,7 +1899,7 @@ def get_title_from_name(self, name: str) -> str: The title. """ - return name.title().replace('_', ' ') + return name.title().replace('_', ' ').strip()
repro.py
from pydantic import BaseModel, Field class MyModel(BaseModel): private_field: int = Field(alias='_private_field', serialization_alias='_private_field') MyModel.model_json_schema()
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.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).
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.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

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

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 πŸ‘β€
@sydney-runkle Β· 2024-09-13 Β· source

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

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

When NOT to use: Do not apply this fix if the leading space is intentionally required for specific formatting.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not apply this fix if the leading space is intentionally required for specific formatting.

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