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
- Fix langchain.llms OpenAI completion doesn't work due to v1 client update (#13099) This commit fixes the issue that langchain.llms OpenAI completion stopped working since the V1 openai client update... — committed to langchain-ai/langchain by dosuken123 8 months ago
- Fix langchain.llms OpenAI completion doesn't work due to v1 client update (#13099) This commit fixes the issue that langchain.llms OpenAI completion stopped working since the V1 openai client update... — committed to xieqihui/langchain by dosuken123 8 months ago
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 noChatCompletion
attribute. The suggested solution in that issue was to modify the code inazure_openai.py
to directly useopenai.ChatCompletion
instead ofopenai.Completion
.Here’s the suggested code modification:
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
openai
has noChatCompletion
attributeThis 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!!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 theopenai
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
Saved my day, rolling back to 0.28.1
Opened a fix https://github.com/langchain-ai/langchain/pull/12973
@dosuken123 Will langchain be compatible with the latest api soon?
Closing as https://github.com/langchain-ai/langchain/pull/13099 was released in 0.0.334
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