New Tool Radar

AI Concepts

How to Handle a Safety Refusal in a Structured-Output Response

Check the response for a refusal field, present it cleanly, and never treat it as parsed data.

Schematic with three steps: Check for the refusal field first; Present the refusal cleanly; Log it and move on.

When using Structured Outputs with user-generated input, models may occasionally refuse to fulfil the request for safety reasons. A refusal does not follow the schema you supplied, so the API response includes a refusal field to indicate that the model refused. Your code must check for that field before it trusts the parsed output, because a refusal handled as data will corrupt whatever consumes it.

What you need

  • A structured-output call that takes user-generated input
  • A UI or code path that can show a refusal
  • A log for refused requests

Step 1: Check for the refusal field first

When the response arrives, look at each output item: if its type is refusal, read the refusal text and stop. Do not try to parse it as the schema.

Step 2: Present the refusal cleanly

Show the refusal in the UI, or branch in code to handle the refused request. Keep the wording the model gave; do not invent a reason for the refusal.

Step 3: Log it and move on

Log the refused request and the refusal text, then return control to the user. Do not retry the same input in a loop hoping for a different answer.

Worked example: synthetic refusal handling

The table below is a synthetic example. It shows the expected result of handling a refusal; no API call was made.

Response item Type Handled as
Item 1 refusal Shown in UI, not parsed
Item 2 message, parsed Used as data
Mixed response Refusal + message Refusal shown, message used
Retry loop Not attempted Pass

Expected result (UNTESTED). the refusal is shown in the UI and never parsed as data, while the valid message item is used normally. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A refusal does not necessarily follow the schema you supplied; check for it before parsing.
  • Your application still needs to handle refusals, incomplete responses and API errors.
  • A refusal is a safety outcome, not a validation failure; do not count it as a schema error.
  • If user-generated input is involved, the prompt should say what to return when the input cannot produce a valid response.

Sources

See all