Immutable solution version
AttributeError: module 'openai' has no attribute 'ChatCompletion'
Complete solution
Shows how to fix attributeError: module 'openai' has no attribute 'ChatCompletion'. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Install openai 0.27.0 or later into the Python interpreter you are actually running, then restart the kernel or IDE: pip install --upgrade openai Then: in Jupyter/VS Code, restart the kernel; in PyCharm, install openai 0.27.0+ through the interpreter's package manager and restart.
Steps
- This is almost always a stale install shadowing the new package, not a bug in the library. Install openai 0.27.0 or later into the Python interpreter you are actually running, then restart the kernel or IDE:
- pip install --upgrade openai
- Then: in Jupyter/VS Code, restart the kernel; in PyCharm, install openai 0.27.0+ through the interpreter's package manager and restart. Also do not name your own script
openai.py, since Python will import that instead of the package. The maintainer's advice: if in doubt, create a fresh virtualenv and install there, which multiple users confirmed works.
When to use
You are seeing this: Issue openai/openai-python#246 (closed, 27 comments): ### Describe the bug The feature was working few hours ago, now it's giving error. Use this skill when you run into "AttributeError: module 'openai' has no attribute 'ChatCompletion'".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
Versions mentioned in the source: openai 0.27.0. If you are on something much newer or older, the details may have shifted.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Edge cases
- Also do not name your own script openai.py, since Python will import that instead of the package.
Evidence
In the GitHub issue gh:openai/openai-python#246 (https://github.com/openai/openai-python/issues/246), the problem reported was: Issue openai/openai-python#246 (closed, 27 comments): ### Describe the bug
The feature was working few hours ago, now it's giving error.
To Reproduce
import openai
openai.api_key value [redacted]
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)Code snippets
No response
###... The verified resolution was: This is almost always a stale install shadowing the new package, not a bug in the library. Install openai 0.27.0 or later into the Python interpreter you are actually running, then restart the kernel or IDE:
pip install --upgrade openai
Then: in Jupyter/VS Code, restart the kernel; in PyCharm, install openai 0.27.0+ through the interpreter's package manager and restart. Also do not name your own script openai.py, since Python will import that instead of the package. The maintainer's advice: if in doubt, create a fresh virtualenv and install there, which multiple users confirmed works.
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_a1fp4sLO0TR2rQRGSX7yHw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">AttributeError: module 'openai' has no attribute 'ChatCompletion' <span>— Vectle</span></a>