azure-sdk-for-java icon indicating copy to clipboard operation
azure-sdk-for-java copied to clipboard

[BUG] Azure SDK for Open AI doesn't respect Open AI API Specification when for content_filter and content_length_exceeded

Open leandrodvd opened this issue 2 years ago • 1 comments

Describe the bug On Open AI API documentation for ChatCompletion response, it is stated that the Choice object has a field finish_reason. And it states finish reason can be content_filter and length. But in the azure sdk for open ai client, when there is a content lengh exceeded or a content filter scenario, instead of returning a response according to the open ai api documentation, it throws an HttpResponseException and i need to parse/explore the exception content to understand that the there is a content_filter or length exceeded issue.

I believe the SDK should follow the OpenAI API specification and return a valid 200 response with the finish_reason filled instead of throwing an HttpResponseException when the finish reason is content_filter or length.

Exception or Stack Trace

com.azure.core.exception.HttpResponseException: Status code 400, "{
  "error": {
    "message": "This model's maximum context length is 8192 tokens. However, your messages resulted in 9878 tokens (6742 in the messages, 3136 in the functions). Please reduce the length of the messages or functions.",
    "type": "invalid_request_error",
    "param": "messages",
    "code": "context_length_exceeded"
  }
}
"
	at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:389) ~[azure-core-1.48.0.jar!/:1.48.0]
	at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:133) ~[azure-core-1.48.0.jar!/:1.48.0]
	at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:210) ~[azure-core-1.48.0.jar!/:1.48.0]
	at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:86) ~[azure-core-1.48.0.jar!/:1.48.0]
	at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:125) ~[azure-core-1.48.0.jar!/:1.48.0]
	at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:97) ~[azure-core-1.48.0.jar!/:1.48.0]
	at jdk.proxy3.$Proxy199.getChatCompletionsSync(Unknown Source) ~[?:?]
	at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:1444) ~[azure-ai-openai-1.0.0-beta.8.jar!/:1.0.0-beta.8]
	at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:318) ~[azure-ai-openai-1.0.0-beta.8.jar!/:1.0.0-beta.8]
	at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:685) ~[azure-ai-openai-1.0.0-beta.8.jar!/:1.0.0-beta.8]

To Reproduce Run a chatCompletion with a token count above model limit or with a message that gets filtered by content filter (like sexual or hate content)

Code Snippet

OpenAIClient openAiClient;
openAiChatCompletionsMapper.chatCompletionRequestToChatCompletionsOptions(chatCompletionRequest);

Expected behavior A valid response (HTTP 200) should be returned with the content of the response having a Choice object with finish_reason = content_filter or length (instead of an Exception thrown)

leandrodvd avatar Jun 18 '24 15:06 leandrodvd

Hi, @leandrodvd

This is an expected 400 error. The OpenAI services returns content_length_exceeded as 400 error. You can verify it by using curl below

 curl --location --request POST 'https://api.openai.com/v1/chat/completions' --header 'Content-Type: application/json' --header 'Authorization: Bearer sk-your-own-key' -d '{"messages":[{"role":"user","content":"This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit.This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit, This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit. This is a repeated pattern to exceed the token limit "}],"model": "gpt-3.5-turbo-0613"}'

The reponse will be error:

{
  "error": {
    "message": "This model's maximum context length is 4097 tokens. However, your messages resulted in 5198 tokens. Please reduce the length of the messages.",
    "type": "invalid_request_error",
    "param": "messages",
    "code": "context_length_exceeded"
  }
}

mssfang avatar Sep 04 '24 01:09 mssfang