The Fix
Fixes the serialization of union types involving HttpUrl to ensure correct behavior when non-url types are passed.
Based on closed pydantic/pydantic issue #11211 · PR/commit linked
@@ -10,7 +10,13 @@
from typing import TYPE_CHECKING, Any, ClassVar
-from pydantic_core import MultiHostHost, PydanticCustomError, SchemaSerializer, core_schema
+from pydantic_core import (
+ MultiHostHost,
from pydantic import HttpUrl, BaseModel
class A(BaseModel):
a: str
class B(BaseModel):
a: HttpUrl | A
b = B(a=A(a="a"))
b.model_dump_json()
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Apply the official fix\nFixes the serialization of union types involving HttpUrl to ensure correct behavior when non-url types are passed.\nWhen NOT to use: This fix should not be applied if the behavior of union serialization is intentionally desired.\n\nOption C — Workaround\nis switch `a: HttpUrl | A` to `a: A | HttpUrl`\nWhen NOT to use: This fix should not be applied if the behavior of union serialization is intentionally desired.\n\n
Why This Fix Works in Production
- Trigger: Incorrect JSON output occurs when dumping models with HttpUrl types.
- Mechanism: The serialization of union types involving HttpUrl fails when non-url types are passed
Why This Breaks in Prod
- The serialization of union types involving HttpUrl fails when non-url types are passed
- Production symptom (often without a traceback): Incorrect JSON output occurs when dumping models with HttpUrl types.
Proof / Evidence
- GitHub issue: #11211
- Fix PR: https://github.com/pydantic/pydantic/pull/11227
- Reproduced locally: No (not executed)
- Last verified: 2026-02-11
- Confidence: 0.70
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.74
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Additional info: 1. My temporary workaround is switch a: HttpUrl | A to a: A | HttpUrl 2. Same behavior on my mac.”
“Thanks for the report. This is definitely a bug. I'll work on a fix for this in the coming days.”
Failure Signature (Search String)
- Incorrect JSON output occurs when dumping models with HttpUrl types.
Copy-friendly signature
Failure Signature
-----------------
Incorrect JSON output occurs when dumping models with HttpUrl types.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Incorrect JSON output occurs when dumping models with HttpUrl types.
Minimal Reproduction
from pydantic import HttpUrl, BaseModel
class A(BaseModel):
a: str
class B(BaseModel):
a: HttpUrl | A
b = B(a=A(a="a"))
b.model_dump_json()
Environment
- Pydantic: 2
What Broke
Incorrect JSON output occurs when dumping models with HttpUrl types.
Why It Broke
The serialization of union types involving HttpUrl fails when non-url types are passed
Fix Options (Details)
Option A — Apply the official fix
Fixes the serialization of union types involving HttpUrl to ensure correct behavior when non-url types are passed.
Option C — Workaround Temporary workaround
is switch `a: HttpUrl | A` to `a: A | HttpUrl`
Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.
Fix reference: https://github.com/pydantic/pydantic/pull/11227
Last verified: 2026-02-11. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the behavior of union serialization is intentionally desired.
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.
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.