AI and LLM integration · Troubleshooting walkthrough
Python cannot import the OpenAI client class from the openai package
Short answer
ImportError: cannot import name 'OpenAI' from 'openai' means the openai package Python loaded is a 0.x release. The OpenAI client class only exists from version 1.0.0; 0.28.1 has none. Either an old pin installed 0.28, or pip upgraded a different interpreter than the one running your code. Run python -m pip install --upgrade openai with that interpreter (current 3.57).
from openai import OpenAI only works on version 1.0.0 and later of the Python package. OpenAI's v1 migration guide describes that release as a rewrite in which you create a client object; the 0.x line exposed module-level helpers such as openai.ChatCompletion and has no OpenAI class at all. So this ImportError means Python loaded an old copy of the package.
There are two ways to end up with the old copy: a pinned requirements file installed 0.28, or you upgraded the package for one interpreter and ran your code with another. One command tells you which.
The text you see
ImportError: cannot import name 'OpenAI' from 'openai'
Stage 1
Ask the running interpreter which copy it loads
Run this with the same python that raised the error. It prints the interpreter's path, the file the package was imported from, and the version pip sees for that same interpreter.
Interpreter, package location and version in one go
python -c "import sys, openai; print(sys.executable); print(openai.__file__)" && python -m pip show openai | grep -E '^(Version|Location)'| What you see | Meaning |
|---|---|
| Version 0.28.1 or lower | An old pin installed it: next section |
| Version is 1.x or higher, but pip install seemed to do nothing | You upgraded another environment: the section after |
| Version is current and the error says partially initialized module | A local file named openai.py shadows the package |
Stage 2
An old pin from a requirements file
Search the project for the pin. Tutorials from the 0.x era often say openai==0.28, and a lockfile will keep reinstalling it. Raise or remove the pin, then upgrade.
The upgrade fixes the import but not the rest of the file: code written for 0.x calls module-level functions that 1.0 removed. The migration guide includes an openai migrate command that rewrites most calls to the client style.
Find the pin, upgrade, test the import
grep -rnE "openai(==|<|~=)" requirements*.txt pyproject.toml 2>/dev/null
python -m pip install --upgrade openai
python -c "from openai import OpenAI; print('client class found')"Stage 3
pip changed one environment, Python runs another
The pip user guide recommends running it as python -m pip so that it acts on the interpreter you named. A bare pip on the path can belong to a different Python. A Jupyter kernel or an IDE-selected virtual environment is the usual second interpreter: pip reports the new version, the notebook still loads 0.x.
Install with the path that sys.executable printed. In a notebook, the %pip magic installs into the current kernel, and the kernel must be restarted afterwards for the new version to load.
Install into the interpreter that runs the code
# terminal: use the path printed by sys.executable
/path/to/that/python -m pip install --upgrade openai
# Jupyter cell
%pip install --upgrade openaiStage 4
Keeping it from coming back
- Pin a lower bound such as
openai>=1so an old tutorial's pin cannot pull 0.28 back in. - Use one virtual environment per project and select that same interpreter in the IDE and as the notebook kernel.
- Add a CI step that runs
from openai import OpenAIafter dependencies install.
If you only keep one thing
- No OpenAI class exists before version 1.0.0.
- Print sys.executable and the package version from the failing interpreter before changing anything.
- Use python -m pip, and restart a Jupyter kernel after installing.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.