Valid JSON Isn't Enough: Validate Model Output Before Code Acts on It
A support tool that let a language model's JSON output trigger refunds worked for weeks until a message produced a wrong order ID and an over-limit refund, even though the JSON was valid. The core lesson is that parsing successfully does not make a model's response safe to act on. Schema constraints only enforce shape, and Claude's structured outputs documentation notes exceptions such as refusal or max_tokens stops, plus numeric limits and enum casing that the API does not enforce. The author proposes three checks: confirming the response finished normally, verifying the expected structure and an allowed action, and validating it against trusted business data such as the order from the database. Following OWASP guidance on improper output handling, the example code requires the order ID to match the loaded record, uses Decimal for money, and returns short reason codes for rejections.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in