AttributeError: module 'openai' has no attribute 'error'

17 frames
/usr/local/lib/python3.10/dist-packages/langchain/llms/openai.py in _create_retry_decorator(llm, run_manager)
90
91 errors = [
—> 92 openai.error.Timeout,
93 openai.error.APIError,
94 openai.error.APIConnectionError,

AttributeError: module ‘openai’ has no attribute ‘error’

Error while using:# Initiate a connection to the LLM from Azure OpenAI Service via LangChain.
llm = AzureOpenAI(
** openai_api_key = OPENAI_API_KEY,**
** deployment_name = OPENAI_DEPLOYMENT_NAME,**
** model_name = MODEL_NAME,**
** openai_api_version = "openai_api_version "**
** )**

6 Likes

I am also getting same error when trying to use langchain OpenAI. Can some one please help on this.

3 Likes

Downgrade to 0.28 works. But is anyone aware of the solution on the latest version?

1 Like

No. This is a different case with the OpenAI API & Azure Open AI API

Hi.

Environment/versions
conda environment on local ubuntu machine
openai.version = 1.2.0
langchain.version = 0.0.332

I’m getting the same error .

I tried to downgrade langchain to 0.0.28 but ran in to a number of dependency conflicts so no solution there for me.

BR

Jorgen

Hey @shailesh.sarda ,

I am using openai==1.2.0 and langchain==0.0332

Try using AzureChatOpenAI instead of AzureOpenAI…Something like:

llm = AzureChatOpenAI(
        deployment_name="gpt4",
        model_name="gpt-4-32k",
        openai_api_key=api_key,
        openai_api_base="https://your-host-name/openai",
        openai_api_version=api_version
    )

    embed_model = AzureOpenAIEmbedding(
        model="text-embedding-ada-002",
        deployment_name="ada",
        api_key=api_key,
        azure_endpoint="https://your-host-name/",
        api_version=api_version,
    )

If you are getting some errors like Resource is not found, go to your Azure OpenAI deployment and double check that the URL of your model is the same as the one in logs.

Also for additional logs add: to your code:

import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

hi… What is your filename where you are Initiating a connection to the LLM from Azure OpenAI Service via LangChain ?? if its langchain.ipynb or openai.ipynb, try changing your file name to any other name. Let us know if this helps.

1 Like

this solved my problem:
langchain==0.0.316
openai==0.28.1

I hope that it could help you guys too :smiley:

2 Likes

openai recently changed its API!
Now basically you need to remove the “error” key, i.e.

openai.error.APIError

becomes

openai.APIError

See more here: https://platform.openai.com/docs/guides/error-codes/api-errors

3 Likes

This worked for me with my LangChain error. Thank you.

“”

atseptember

this solved my problem:
langchain==0.0.316
openai==0.28.1

I hope that it could help you guys too :smiley:

“”"

4 Likes

Thank you. This solution also helped me.

Blockquote

maybe

openai migrate

can be solution.

I think just need to revert back to the old version, langchain has not made any changes yet. Can go to langchain on GitHub and see there’s a new issue related to this problem

1 Like

This worked for me Too after running:

!pip install langchain==0.0.316
!pip install openai==0.28.1

1 Like

I am getting the same error and tried downgrading openai and langchain, but I’m still getting the error. I saw in another thread that your filename should not be openai or langchain, and mine is not. Any other suggestions?

I also upgraded to Python 3.11 which seems to have fixed the problem :slight_smile:

Hi

Just upgrading to python 3.11 or is it also necessary to downgrade openai and langchain?

BR

Jorgen

Just update both LangChain and OpenAI

pip install langchain openai --upgrade

4 Likes

My file name was langchain.ipynb, and I changed it to main - just so. Still, the error remains.

It’s working with python 3.8.10, langchain==0.0.339 and openai==1.3.5. Try it.