5

When attempting to use model.list(), the library encounters an authentication error due to the incorrect configuration of the API key. I've been continuously receiving the error message.

import openai

openai.api_key = "your_api_key"

models = openai.Model.list()

print(models.data[0].id)

chat_completion = openai.Completion.create(
  model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}]
  )

print(chat_completion.choices[0].message.content)`

Error:

Traceback (most recent call last):
  File "C:\Users\USER\gpt1.py", line 6, in <module>
    openai.Model.list()
  File "C:\Python311\Lib\site-packages\openai\api_resources\abstract\listable_api_resource.py", line 52, in list
    requestor, url = cls.__prepare_list_requestor(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\openai\api_resources\abstract\listable_api_resource.py", line 20, in __prepare_list_requestor
    requestor = api_requestor.APIRequestor(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\openai\api_requestor.py", line 138, in __init__
    self.api_key = key or util.default_api_key()
                          ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\openai\util.py", line 186, in default_api_key
    raise openai.error.AuthenticationError(
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

2 Answers 2

1

Try setting the environmental variable, and try setting up authentication the same way they do for the models list documenation using the OS environment variable.

import os
import openai
from dotenv import load_dotenv
load_dotenv()

api_key = os.getenv('OPENAI_KEY')
openai.api_key = api_key

Here are instructionsenter link description here for setting environment variables. This is for a juypter notebook, but it shouldnt be that much different from setting them or your own environment.

Sign up to request clarification or add additional context in comments.

Comments

0

Save the API key as the environment variable OPENAI_API_KEY and use:

from openai import OpenAI
os.environ["OPENAI_API_KEY"] = "sk-proj-xxxxx"
client = OpenAI()

# list of all models
client.models.list()

This works as well:

client = OpenAI(api_key="sk-proj-xxxxx")

In Google Colab you can also format tables nicely

import pandas as pd
from google.colab import data_table

data_table.enable_dataframe_formatter()

models = client.models.list()
models_df = pd.DataFrame([model.model_dump() for model in models.data]).sort_values("created", ascending=False)

# Convert the 'created' column from Unix timestamp to datetime
models_df['created'] = pd.to_datetime(models_df['created'], unit='s')

# Display the DataFrame as a data table
display(models_df)

# Out:
# index id                    created                   object  owned_by
# 1     gpt-audio             2025-08-28 00:00:49   model   system
# 5     gpt-audio-2025-08-28  2025-08-27 00:55:46   model   system
# 4     gpt-5-nano            2025-08-05 20:39:44   model   system
# . . . 

By the way, there's no need to install OpenAI in Google Colab

!pip show openai
# Out:
# Name: openai
# Version: 1.107.0
# Summary: The official Python library for the openai API
# Home-page: https://github.com/openai/openai-python
# Author: 
# Author-email: OpenAI <[email protected]>
# License: Apache-2.0
# Location: /usr/local/lib/python3.12/dist-packages
# Requires: anyio, distro, httpx, jiter, pydantic, sniffio, tqdm, typing-extensions
# Required-by: 

If you try to use this you get a deprecated message:

import openai
openai.Model.list()
# Out:
# APIRemovedInV1: 

# You tried to access openai.Model, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

# You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

# Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

# A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.