Azure OpenAI API return KeyError: 'content'

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?

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.

You should try to catch the key exception next time.

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

Might give you some insight.

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.

1 Like

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.

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

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

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

1 Like