langchain: AttributeError: module 'openai' has no attribute 'Completion'

System Info

Downloading langchain-0.0.331-py3-none-any.whl (2.0 MB) Downloading openai-1.1.1-py3-none-any.whl (217 kB)

Who can help?

No response

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Run the following code in colab:

!pip install langchain
!pip install openai

from langchain.llms import OpenAI

OpenAI().predict("hoge")

You’ll get:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-5-0eec0f4f0523>](https://localhost:8080/#) in <cell line: 4>()
      2 from langchain.llms import OpenAI
      3 
----> 4 OpenAI().predict("hoge")

3 frames
[/usr/local/lib/python3.10/dist-packages/langchain/llms/openai.py](https://localhost:8080/#) in validate_environment(cls, values)
    264             import openai
    265 
--> 266             values["client"] = openai.Completion
    267         except ImportError:
    268             raise ImportError(

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

Expected behavior

This was working until yesterday. It’s likely due to the openai dependency update.

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 21
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Confirmed openai==v0.28.1 is https://github.com/openai/openai-python/releases/tag/v0.28.1 working correctly.

Likely this issue started since V1.0.0 release https://github.com/openai/openai-python/releases/tag/v1.0.0 which happened a few hours ago

For those who are wondering how to rollback to v0.28.1 Open your terminal and type pip install --upgrade openai=0.28.1 This will rollback your current version to 0.28.1 Restarting your kernel should fix the issue

🤖

Based on the error message you’re encountering, it seems like the ‘openai’ module has been updated and the ‘Completion’ attribute has been removed or renamed. This is likely the reason why your code was working until yesterday but is now throwing an AttributeError.

A similar issue was encountered in the past as documented in the LangChain repository issue titled openai has no ChatCompletion attribute. The suggested solution in that issue was to modify the code in azure_openai.py to directly use openai.ChatCompletion instead of openai.Completion.

Here’s the suggested code modification:

        openai.api_key = openai_api_key
        if openai_organization:
            openai.organization = openai_organization
        if openai_api_base:
            openai.api_base = openai_api_base

        values["client"] = openai.ChatCompletion

        if values["n"] < 1:
            raise ValueError("n must be at least 1.")
        if values["n"] > 1 and values["streaming"]:
            raise ValueError("n must be 1 when streaming.")
        return values

Please try this modification and see if it resolves your issue. If the problem persists, it would be helpful to know the exact version of the ‘openai’ module you’re using. You can find this by running pip show openai in your Python environment. This will help in diagnosing if the issue is due to a specific version of the ‘openai’ module.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosuken123 - your suggestion of switching to using openai==v0.28.1 with langchain worked like a charm and saved me many more hours of debugging. Thank you!!

Still facing this issue. When will this be fixed?

The fix seems to be merged in #12969 so we are just waiting for the new version to be pushed. Using the previous openai versions should work until then

In v0.28, the Completion class was exposed at the openai package namespace https://github.com/openai/openai-python/blob/v0.28.1/openai/__init__.py#L22, however, v1.0.x doesn’t do that anymore https://github.com/openai/openai-python/blob/v1.0.0/src/openai/__init__.py.

We need to find an openai.Completion equivalent class in the new structure.

I guess langchain needs to update source code to match the changes in openai API this works fine on my instance

response = openai.completions.create(
  model="text-davinci-002",
  prompt="Once upon a time,",
  max_tokens=50
)

Confirmed openai==v0.28.1 is https://github.com/openai/openai-python/releases/tag/v0.28.1 working correctly.

Likely this issue started since V1.0.0 release https://github.com/openai/openai-python/releases/tag/v1.0.0 which happened a few hours ago

Saved my day, rolling back to 0.28.1

@dosuken123 Will langchain be compatible with the latest api soon?

Other two things to make sure you have correct is the imports for LLMChain and PromptTemplate.

This seem to have changed over the past few weeks?

from langchain.chains import LLMChain from langchain.prompts import PromptTemplate