How to Resolve Authentication Error 401 When Using GPT-4-o with the Azure OpenAI Python SDK

AMROUN Lysa 241 Reputation points
2024-05-25T21:02:03.94+00:00

Hello ,

I would like to use gpt4-o with the python sdk for text generation (conversation), I have deployed the gpt4o model in the US2 region in azure openai and I use the endpoint, key and deployment in my code like this:


openai.api_type = "azure"
openai.api_version = "2024-02-01"
openai.api_base = "my_api_base"
openai.api_key = "my_api_key"

response = openai.chat.completions.create(
            model=os.getenv("MyGpt4oDeployment"),
            messages=myconversation
        )

but I get this error that I don't understand:

openai.AuthenticationError: Error code: 401 - {'statusCode': 401, 'message': 'Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired.'}

knowing that the same code works with a gpt4 deployment. How can I solve this problem, please?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,482 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 29,906 Reputation points
    2024-05-27T03:01:33.2166667+00:00

    AMROUN Lysa Greetings!

    This error indicates that your authentication credentials are invalid. This could happen for several reasons, such as your API key is missing or invalid, or the endpoint is incorrect.

    Please make sure that you have a valid API key and endpoint for GPT-4o.

    Also, make sure that you are using latest and updated python versions, Openai and api_version.

    See the similar issue which has been addressed here and let me know if that helps.

    0 comments No comments