AuthenticationError: Error code: 401

Hello,

I tried to run some inference in a Jupyter Notebok but I cannot manage to get a valid connection.

The documentation and looking here in the forum or online didn’t help me at all.

I added a credit card and have a balance of 10 USD.

The key is copy and pasted correctly.

In the Notebook I tried to set the key with these 3 versions:

openai.api_key = 'sk-proj-XXX'
os.environ['OPENAI_API_KEY'] = 'sk-proj-XXX'
%env OPENAI_API_KEY='sk-proj-XX'

When I try to run this code:

import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

gpt_model = "gpt-3.5-turbo-0125"

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model=gpt_model,
)

or this

client = OpenAI(
  organization='org-XXX', # it has the actual org ID
  project='proj_XXX', # it has the actual project ID
)

gpt_model = "gpt-3.5-turbo-0125"

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model=gpt_model,
)

I get the same error message:

AuthenticationError: Error code: 401 - {'error': {'message': "Incorrect API key provided: 'sk-proj**********************************************Ldy'. You can find your API key at https://platform.openai.com/account/api-keys.", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

As I am fairly new to this. What am I doing wrong?

Hmmm…
Try it:

from openai import OpenAI

client = OpenAI(
      api_key="API_KEY_OPENAI"
)

maybe you specified the wrong key? or the key was created incorrectly. When creating a key, try giving it all access rights.

I tried it and I get the same result.

The key has all permissions:

Have you tried generating a new key and seeing if you get the same result?

That’s not going to work. Sending the string and not your actual key.

The OpenAI python client will use an environment variable API_KEY_OPENAI automatically - when you set it in the OS or the environment.

It was just two days ago I produced an AI response telling someone how to set an environment variable: https://chatgpt.com/share/b2c99472-e3ee-4067-ab2c-f39a7e29c0ed

There is no need to put anything as parameters in the client, unless you want a particular timeout or are billing to a different organization you are a member of.

1 Like

What?! I simply specified API_KEY_OPENAI as a stub that needs to be replaced. What’s not clear here?

It would be clear if it didn’t look like an environment variable.

client = OpenAI(api_key="sk-proj-use your actual API key string here")

However, hard-coded keys are a path to leaked keys.

I got it to work today. I created yet another key and it worked. Honestly, I have no idea what went wrong.

Thanks for the support!

I tried both variations anyhow yesterday, neither would work, but it did today. While not good practice, I don’t mind hard-coding the key here. Once I am done with the task, I will delete the key and this project isn’t going to be committed anywhere either.

Thanks for the reminder!