openai-python: openai.error.AuthenticationError:

Describe the bug

Since yesterday 24.05.2023 around your outage I am facing this errors:

..
  File "/opt/homebrew/lib/python3.10/site-packages/openai/api_resources/embedding.py", line 33, in create
    response = super().create(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
    response, _, api_key = requestor.request(
  File "/opt/homebrew/lib/python3.10/site-packages/openai/api_requestor.py", line 230, in request
    resp, got_stream = self._interpret_response(result, stream)
  File "/opt/homebrew/lib/python3.10/site-packages/openai/api_requestor.py", line 624, in _interpret_response
    self._interpret_response_line(
  File "/opt/homebrew/lib/python3.10/site-packages/openai/api_requestor.py", line 687, in _interpret_response_line
    raise self.handle_error_response(
openai.error.AuthenticationError: <empty message>

I tried several approaches, thought LangChain would be the problem, thought a vectorstore is it, but now I think it must be something with the API as I rebuilt with several options and it all leads to the same error.

To Reproduce

openai.api_key = OPENAI_KEY or os.getenv("OPENAI_API_KEY")

# where texts can be a list of strings like 
texts = ["test", "foo"]
for i in texts:
    response = openai.Embedding.create(
        input=i,
        model="text-embedding-ada-002"
    )
    embeddings = response['data'][0]['embedding']

Code snippets

No response

OS

macOS, m1-chip

Python version

Python 3.10.9

Library version

openai==0.27.7

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 14
  • Comments: 42 (5 by maintainers)

Most upvoted comments

This problem has been bothering me for a week.

Finally, I found out that when you pass your openai-api-key to a public space(such as github), your openai-api-key on the OpenAI website(https://platform.openai.com/account/api-keys) will be automatically deleted, and then you will have this error report.

So you must regenerate a key and ensure that it is not transferred to the public space.

can you try the solution in this order?

1- open .env file (in project root directory) & add OPENAI_API_KEY OPENAI_API_KEY=sk-....

2- implement this lines in your python file:

config = dotenv_values('.env')
openai.api_key = config['OPENAI_API_KEY']
os.environ['OPENAI_API_KEY'] = config['OPENAI_API_KEY']

3- call langchain method (implicitly openai method)

I saw that LangChain read the wrong key from os’s environment value

i had this problem. When i checked my API key on platform.openai.com, it had disappeared. I know i havent deleted it, but suddenly it had been lost. So i just created a new API key and continued using without problems. Hope this will also solve you problems guys ))

In our case, we generated another token, and that fixed the issue

@mehmetkoca thank you! Your solution works for me! I still don’t know, why my old code stopped working, but I’m just glad, it works now:)

Actually, that might not help. Could we instead ask for more debugging information? The Authentication Error should have more details. Something like this as a wrapper around your code might do the trick:

try:
  # openai stuff
except openai.error.AuthenticationError as e:
  print("OpenAI unknown authentication error")
  print(e.json_body)
  print(e.headers)

I am getting the same error when trying to use the SQL Database Agent.

This is my code,

from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain import SQLDatabase
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(model_name='gpt-3.5-turbo')

db = SQLDatabase.from_uri("sqlite:///chinook.db")
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True
)

agent_executor.run("Describe the playlisttrack table")

I am running the latest versions of Python, langchain and openai.

LangChain uses the OpenAI Python SDK so that’s the main issue. I had as described problems with several libraries.