Azure OpenAI API return KeyError: 'content'

content filter is a finish_reason. You can see that your detection can short-circuit on a change of the finish reason from “stop” to “length” (when you specify small max_tokens), and that should also allow it to catch others that are not “stop”.