# Azure OpenAI API return KeyError: 'content'

**URL:** <https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131>\
**Category:** API\
**Created:** [October 4, 2023, 1:14am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131 "2023-10-04T01:14:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiraishi.takahiro](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@shiraishi.takahiro](https://community.openai.com/u/shiraishi.takahiro)\
**Post date:** [October 4, 2023, 1:14am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/1 "2023-10-04T01:14:51Z")

</div>

Hi,  
I executed the code using Azure OpenAI API.

for many time( maybe exceed 100000 times), I called Azure OpenAI API on the code.  
Then I found twice the error (KeyError: ‘content’)

# ====================================== response = openai.ChatCompletion.create( engine=self.deploy\_name, messages=messages, temperature=temperature, max\_tokens=int(max\_tokens\_size), top\_p=top\_p, frequency\_penalty=frequency\_penalty, presence\_penalty=presence\_penalty, ) answer = response[“choices”][0][“message”][“content”]

=============================================

Why did this error happen?

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [October 4, 2023, 1:28am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/2 "2023-10-04T01:28:48Z")

</div>

Hi and welcome to the Developer Forum!

You may wish to contact Azure regarding this as it is using a slightly different back end to the OpenAI version. However, in general you should always treat remote API’s as unreliable and build in error checking, including timeouts and error code checks along with exponential backoff retries and user updates if applicable.

---

<div class="post-metadata">

**Author:** ![codie](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/codie/32/77495_2.png) [@codie](https://community.openai.com/u/codie)\
**Post date:** [October 4, 2023, 2:29am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/3 "2023-10-04T02:29:22Z")

</div>

You should try to catch the key exception next time.

```python
try:
    answer = response["choices"][0]["message"]["content"]
except KeyError:
    print("Error in message chat completions.")
    print(json.dumps(response))

```

Might give you some insight.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [October 4, 2023, 3:00am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/4 "2023-10-04T03:00:11Z")

</div>

You probably got a “content\_filter” return.

Azure’s API also has several other types of output that can be triggered by AI when using extensions.

---

<div class="post-metadata">

**Author:** ![shiraishi.takahiro](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@shiraishi.takahiro](https://community.openai.com/u/shiraishi.takahiro)\
**Post date:** [October 6, 2023, 9:21am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/5 "2023-10-06T09:21:33Z")

</div>

Hi,

I decided to implement exception, and I search for detail of “content\_filter”.

especially, I wanna know return format when “content\_filter” occured, but I try to search it by myself.

Everyone, Thank you for giving me information.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [October 6, 2023, 9:44am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/6 "2023-10-06T09:44:43Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![shiraishi.takahiro](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@shiraishi.takahiro](https://community.openai.com/u/shiraishi.takahiro)\
**Post date:** [October 6, 2023, 11:48am UTC](https://community.openai.com/t/azure-openai-api-return-keyerror-content/412131/7 "2023-10-06T11:48:36Z")

</div>

thank you

but, I found error which your method would not catch.

==============================  
prompt = “You should die！”  
return  
Error: The response was filtered due to the prompt triggering Azure OpenAI’s content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: [Azure OpenAI Service content filtering - Azure OpenAI | Microsoft Learn](https://go.microsoft.com/fwlink/?linkid=2198766)

============================  
this error message should be inner error of the API because when I tried this prompt, I couldn`t get any response from the API.
