Search⌘ K
AI Features

Validating Structured Output Against the Contract

Understand how to parse and validate LLM-generated responses against strict API contracts to prevent malformed outputs from reaching clients. Learn to distinguish parsing errors from schema validation failures and handle each with appropriate, predictable error responses to maintain backend reliability.

With a stable API contract defined, clients now have a defined response shape. LLMs can produce variable, unstructured output and do not inherently conform to application schemas. Next, we will align model output with the API contract by parsing and validating structured model output against the response schema.

A Support Ticket Assistant response can look like valid JSON and still break our endpoint when one field is the wrong shape. One representative example is a payload where draft_reply and rationale are present, but open_questions is a string, or the model appends trailing prose after the closing }. The failure is delayed. json.loads returns a dict, our handler returns it, and a client crashes later when it tries to iterate open_questions as a ...