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
- Inspect the response status and output items using the provider's documented response shape.
- Route refusal content to an appropriate user-facing or review path. Keep schema validation for successful structured output.
- 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
- Why did an AI API response stop halfway through its answer?
- How do I tell when streamed AI tool arguments are complete and ready to parse?