DEV Community

Charlie Zhu
Charlie Zhu

Posted on

The Shape Guard Workshop: When to Repair Model JSON

A support triage field is supposed to be an integer from 1 through 5. One model returns the JSON string "3"; another returns "high". They are both strings, but accepting them would be two different product decisions. One can be defined as a representation change. The other requires inventing a mapping between words and severity.

Ask the schema owner to approve a coercion table before changing the parser. For this example, choose to accept integers 1–5 and, as a documented compatibility exception, strings consisting of one ASCII digit 1–5 after trimming surrounding whitespace. Reject booleans, missing values, decimal strings and labels. A language that treats booleans as a subtype of integers needs an explicit check.

Decoded JSON value Result under this contract Reason
3 Accept 3 Integer in range
" 3 " Coerce to 3 Exact approved representation
"03" or "3.0" Review Outside the compatibility exception
true, "high" or null Review No approved conversion

For each field, decide whether changes to representation are even permissible. Converting an account identifier that happens to contain digits can destroy meaningful leading zeros. A rule for severity should therefore live with severity, not in a generic “fix all model JSON” utility.

When a representation can be repaired deterministically, do that once, then validate the complete object again. A fresh model answer is a new extraction attempt, not evidence that the original value was correct. Record field, reason, attempt count and schema version; avoid retaining raw sensitive content merely to debug a conversion. If reviewers disagree whether “high” means four or five, stop at review and settle the contract. Cleanup code cannot supply that domain decision.

This article was created with AI assistance.

Top comments (0)