The Fix
pip install pydantic==2.10.2
Based on closed pydantic/pydantic issue #9491 · 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%.
@@ -10,7 +10,7 @@
from typing import TYPE_CHECKING, Any, ClassVar
-from pydantic_core import MultiHostHost, PydanticCustomError, core_schema
+from pydantic_core import MultiHostHost, PydanticCustomError, SchemaSerializer, core_schema
from pydantic_core import MultiHostUrl as _CoreMultiHostUrl
from typing import Any
from pydantic import BaseModel, SecretStr
# Successful case
class SuccessfulCase(BaseModel):
some_value: SecretStr
s = SuccessfulCase(some_value="1")
print(f"Successful `model_dump_json` case: {s.model_dump_json()}")
# Failing case
class FailingCase(BaseModel):
some_value: Any
f = FailingCase(some_value=SecretStr("1"))
f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
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.10.2\nWhen NOT to use: Do not use this fix if your models do not contain SecretStr fields.\n\n
Why This Fix Works in Production
- Trigger: f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
- Mechanism: Serialization fails for models with dynamic SecretStr fields due to missing serializer
- Why the fix works: Supports serialization as any for `Secret` types and `Url` types, addressing serialization issues with `SecretStr` in Pydantic models. (first fixed release: 2.10.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.10 in real deployments (not just unit tests).
- Serialization fails for models with dynamic SecretStr fields due to missing serializer
- Production symptom (often without a traceback): f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
Proof / Evidence
- GitHub issue: #9491
- Fix PR: https://github.com/pydantic/pydantic/pull/10947
- First fixed release: 2.10.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.58
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.10.2
- Mode: fixed_only
- Outcome: ok
Logs
Successful `model_dump_json` case: {"some_value":"**********"}
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Agreed with that opinion I think pretending they are or will be separate libraries would be a mistake. That said I do think we can…”
“@adriangb, I think that might be the right approach, and perhaps we should do that for other Pydantic types as well.”
“@davidhewitt, I recall us discussing the fact that having __pydantic_serializer__ checks in pydantic-core wasn't the best practice in terms of extremely tight coupling between the…”
“cc @bcdurak, I've opened a PR that could potentially fix this :)”
Failure Signature (Search String)
- f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
- Can't we add a serializer to it?
Copy-friendly signature
Failure Signature
-----------------
f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
Can't we add a serializer to it?
Error Message
Signature-only (no traceback captured)
Error Message
-------------
f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
Can't we add a serializer to it?
Minimal Reproduction
from typing import Any
from pydantic import BaseModel, SecretStr
# Successful case
class SuccessfulCase(BaseModel):
some_value: SecretStr
s = SuccessfulCase(some_value="1")
print(f"Successful `model_dump_json` case: {s.model_dump_json()}")
# Failing case
class FailingCase(BaseModel):
some_value: Any
f = FailingCase(some_value=SecretStr("1"))
f.model_dump_json() # fails with a pydantic_core._pydantic_core.PydanticSerializationError
Environment
- Python: 3.10
- Pydantic: 2
What Broke
API responses fail with internal server errors when serializing models containing SecretStr.
Why It Broke
Serialization fails for models with dynamic SecretStr fields due to missing serializer
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.10.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/10947
First fixed release: 2.10.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if your models do not contain SecretStr fields.
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.10.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.