AI Help

How do I handle an AI refusal when my app expects a structured response?

Handle a refusal as its own result state before parsing the expected success schema. Also check for incomplete responses.

Steps to follow

  1. Inspect the response status and output items using the provider's documented response shape.
  2. Route refusal content to an appropriate user-facing or review path. Keep schema validation for successful structured output.
  3. Test refusal, valid output, malformed output and incomplete responses separately so none is silently treated as success.

Example in practice

Illustrative example. A document classifier returns a refusal item, so the application records review_required instead of inserting an empty category.

Related AI help

Official guidance

OpenAI documentation