OpenAI GPT-4API ERROR "AttributeError: content"

Consider your extraction of only one key from the response object:

return completion.choices[0].message.content

Where would a “content” error come from? It would come from the key being missing within messages. You can’t extract “content” from the “completion” object if there is no content key returned.

What would cause there to be no content? This response might explain:


{
    "error": {
        "message": "The response was filtered due to the prompt triggering Azure Content management policy. 
                   Please modify your prompt and retry. To learn more about our content filtering policies
                   please read our documentation: https://go.microsoft.com/fwlink/?linkid=21298766",
        "type": null,
        "param": "prompt",
        "code": "content_filter",
        "status": 400,
        "innererror": {
            "code": "ResponsibleAIPolicyViolation",
            "content_filter_result": {
                "hate": {
                    "filtered": true,
                    "severity": "high"
                },
                "self-harm": {
                    "filtered": true,
                    "severity": "high"
                },
                "sexual": {
                    "filtered": false,
                    "severity": "safe"
                },
                "violence": {
                    "filtered":true,
                    "severity": "medium"
                }
            }
        }
    }
}

You need to capture the full response with json.loads(str(completion)), and then look for the data and handle different responses.

2 Likes