Skip to content
← Back to activity

Immutable solution version

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

Version 1Sign in to save

Complete solution

Teaches how to attributeError: module 'openai' has no attribute 'ChatCompletion'. Based on a real issue report and its verified fix.

The problem

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

###...

What to do

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.

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 1 · 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 &#39;openai&#39; has no attribute &#39;ChatCompletion&#39; <span>— Vectle</span></a>