Jump to solution
Verify

The Fix

pip install pydantic==2.7.1

Based on closed pydantic/pydantic issue #9231 · 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
@@ -1561,14 +1561,22 @@ def validate_secret_value(value, handler) -> Secret[SecretType]: return cls(validated_inner) + def serialize(value: Secret[SecretType], info: core_schema.SerializationInfo) -> str | Secret[SecretType]: + if info.mode == 'json': + return str(value)
repro.py
from pydantic import BaseModel, Secret class Base(BaseModel): x: Secret[int] | Secret[str] model = Base(x=1) print(model.model_dump()) # No error print(model.model_dump_json()) # pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'pydantic.types.Secret'>
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.7.1\nWhen NOT to use: Do not use this fix if your model does not contain Secret types in unions.\n\n

Why This Fix Works in Production

  • Trigger: Error: `pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class
  • Mechanism: PydanticSerializationError occurs due to improper serialization handling of Secret types in unions
  • Why the fix works: Fixes the serialization schema for Secret types in unions, addressing a PydanticSerializationError when using model_dump_json(). (first fixed release: 2.7.1).
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.12 in real deployments (not just unit tests).
  • PydanticSerializationError occurs due to improper serialization handling of Secret types in unions
  • Surfaces as: Error: `pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class…

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.7.1
  • Mode: fixed_only
  • Outcome: ok
Logs
affected (exit=None)
fixed (exit=0)
{'x': Secret('**********')} {"x":"**********"}

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“@KasperZutterman, Thanks for reporting this! Definitely something that we want to fix. Will release a fix for this early next week with 2.7.1!”
@sydney-runkle · 2024-04-13 · source
“^^ I've opened https://github.com/pydantic/pydantic/pull/9240 to address this issue :).”
@sydney-runkle · 2024-04-14 · source

Failure Signature (Search String)

  • Error: `pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class

Error Message

Stack trace
error.txt
Error Message ------------- Error: `pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'pydantic.types.Secret'>`

Minimal Reproduction

repro.py
from pydantic import BaseModel, Secret class Base(BaseModel): x: Secret[int] | Secret[str] model = Base(x=1) print(model.model_dump()) # No error print(model.model_dump_json()) # pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'pydantic.types.Secret'>

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Models containing Secret types fail to serialize to JSON, causing application errors.

Why It Broke

PydanticSerializationError occurs due to improper serialization handling of Secret types in unions

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.7.1

When NOT to use: Do not use this fix if your model does not contain Secret types in unions.

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/9240

First fixed release: 2.7.1

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 use this fix if your model does not contain Secret types in unions.

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

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.