Jump to solution
Verify

The Fix

pip install pydantic==2.6.0

Based on closed pydantic/pydantic issue #10221 · PR/commit linked

Production note: This usually shows up under retries/timeouts. Treat it as a side-effect risk until you can verify behavior with a canary + real traffic.

Jump to Verify Open PR/Commit
@@ -1319,8 +1319,12 @@ print(error_locations) ## Required fields -To declare a field as required, you may declare it using just an annotation, or you may use `Ellipsis`/`...` -as the value: +To declare a field as required, you may declare it using an annotation, or an annotation in combination with a `Field` specification.
repro.py
from pydantic import BaseModel import json class ExampleModel(BaseModel): key1: str key2: int example = ExampleModel(key1="value1", key2=123) pydantic_json = example.model_dump_json() standard_json = json.dumps(example.model_dump()) print("Pydantic JSON:", pydantic_json) print("Standard JSON:", standard_json)
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.6.0\nWhen NOT to use: This fix is not suitable for scenarios where exact JSON formatting is critical without adjustments.\n\n

Why This Fix Works in Production

  • Trigger: In some cases, the JSON outputs are not identical, particularly regarding spaces after colons. This discrepancy can cause failures in MD5 hash generation and…
  • Mechanism: Addresses inconsistencies in JSON output between model_dump_json and json.dumps in Pydantic, ensuring better compatibility for exact string matches.
  • Why the fix works: Addresses inconsistencies in JSON output between model_dump_json and json.dumps in Pydantic, ensuring better compatibility for exact string matches. (first fixed release: 2.6.0).
Production impact:
  • If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).

Why This Breaks in Prod

  • Shows up under Python 3.12 in real deployments (not just unit tests).
  • Production symptom (often without a traceback): In some cases, the JSON outputs are not identical, particularly regarding spaces after colons. This discrepancy can cause failures in MD5 hash generation and other validation processes that require exact JSON string matches.

Proof / Evidence

Discussion

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

“Hi @EmperorNiu, Thanks for your inquiry here”
@sydney-runkle · 2024-08-23 · source

Failure Signature (Search String)

  • In some cases, the JSON outputs are not identical, particularly regarding spaces after colons. This discrepancy can cause failures in MD5 hash generation and other validation
  • Thanks for your inquiry here. Looks like this is a duplicate of https://github.com/pydantic/pydantic/issues/6606 which was fixed with
Copy-friendly signature
signature.txt
Failure Signature ----------------- In some cases, the JSON outputs are not identical, particularly regarding spaces after colons. This discrepancy can cause failures in MD5 hash generation and other validation processes that require exact JSON string matches. Thanks for your inquiry here. Looks like this is a duplicate of https://github.com/pydantic/pydantic/issues/6606 which was fixed with https://github.com/pydantic/pydantic/pull/8645.

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- In some cases, the JSON outputs are not identical, particularly regarding spaces after colons. This discrepancy can cause failures in MD5 hash generation and other validation processes that require exact JSON string matches. Thanks for your inquiry here. Looks like this is a duplicate of https://github.com/pydantic/pydantic/issues/6606 which was fixed with https://github.com/pydantic/pydantic/pull/8645.

Minimal Reproduction

repro.py
from pydantic import BaseModel import json class ExampleModel(BaseModel): key1: str key2: int example = ExampleModel(key1="value1", key2=123) pydantic_json = example.model_dump_json() standard_json = json.dumps(example.model_dump()) print("Pydantic JSON:", pydantic_json) print("Standard JSON:", standard_json)

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Inconsistent JSON outputs can lead to validation failures in systems relying on exact string matches.

Fix Options (Details)

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

pip install pydantic==2.6.0

When NOT to use: This fix is not suitable for scenarios where exact JSON formatting is critical without adjustments.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Option D — Guard side-effects with OnceOnly Guardrail for side-effects

Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.

  • Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
  • Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
  • Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
  • This does NOT fix data corruption; it only prevents duplicate side-effects.
Show example snippet (optional)
onceonly.py
from onceonly import OnceOnly import os once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True) # Stable idempotency key per real side-effect. # Use a request id / job id / webhook delivery id / Stripe event id, etc. event_id = "evt_..." # replace key = f"stripe:webhook:{event_id}" res = once.check_lock(key=key, ttl=3600) if res.duplicate: return {"status": "already_processed"} # Safe to execute the side-effect exactly once. handle_event(event_id)

See OnceOnly SDK

When NOT to use: Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

Fix reference: https://github.com/pydantic/pydantic/pull/8645

First fixed release: 2.6.0

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

  • This fix is not suitable for scenarios where exact JSON formatting is critical without adjustments.
  • Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

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

Related Issues

No related fixes found.

Sources

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