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.
@@ -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.
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)
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.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).
- 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
- GitHub issue: #10221
- Fix PR: https://github.com/pydantic/pydantic/pull/8645
- First fixed release: 2.6.0
- 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.57
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Hi @EmperorNiu, Thanks for your inquiry here”
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
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 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
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
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)
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)
Fix reference: https://github.com/pydantic/pydantic/pull/8645
First fixed release: 2.6.0
Last verified: 2026-02-09. Validate in your environment.
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
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.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.