Jump to solution
Verify

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

Jump to Verify Open PR/Commit
@@ -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,
repro.py
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()
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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

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.”
@atodniAr · 2025-01-03 · source
“Thanks for the report. This is definitely a bug. I'll work on a fix for this in the coming days.”
@sydney-runkle · 2025-01-07 · source

Failure Signature (Search String)

  • Incorrect JSON output occurs when dumping models with HttpUrl types.
Copy-friendly signature
signature.txt
Failure Signature ----------------- Incorrect JSON output occurs when dumping models with HttpUrl types.

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- Incorrect JSON output occurs when dumping models with HttpUrl types.

Minimal Reproduction

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

When NOT to use: This fix should not be applied if the behavior of union serialization is intentionally desired.

Option C — Workaround Temporary workaround

is switch `a: HttpUrl | A` to `a: A | HttpUrl`

When NOT to use: This fix should not be applied if the behavior of union serialization is intentionally desired.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if the behavior of union serialization is intentionally desired.

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.

Related Issues

No related fixes found.

Sources

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