Old openai.ChatCompletion or openai.Embedding code breaks on a new SDK
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0Short answer
The Python SDK was rewritten in version 1.0. Module-level helpers such as openai.ChatCompletion.create and openai.Embedding.create were removed. Either move the code to a client object (client.chat.completions.create, client.embeddings.create), which openai migrate can do for you, or pin an old 0.28 release so your old code keeps running unchanged.
Fix it fast
1Find out which cause you have
Print the installed SDK version. Anything from 1.0.0 upward cannot run the old module-level calls; the error comes from that mismatch, not from your key or network.
pip show openai python -c "import openai; print(openai.__version__)"2Match what you see, then jump to its fix
- 1
The version printed is 1.0.0 or higher and the traceback points at a line such as openai.ChatCompletion.create(...).Code written for the 0.x SDK is running on the 1.x or later SDKFix - 2
Dozens of call sites use the old names and you need the app working before you can rewrite them.A large code base that cannot be changed todayFix
- 1
What does the error look like?
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0APIRemovedInV1
Cause 1 of 2
Code written for the 0.x SDK is running on the 1.x or later SDK
1Check
Search the project for the removed names. In the 1.x SDK, openai.ChatCompletion is only a stub that raises an error when called.
grep -rnE 'openai\.(ChatCompletion|Embedding|Completion)' --include='*.py' .2Fix
Create a client once and call the new method names. Chat calls become client.chat.completions.create and embedding calls become client.embeddings.create. The async a* methods were removed; use AsyncOpenAI with await instead.
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": "Say hello"}],
)
print(completion.choices[0].message.content)
emb = client.embeddings.create(model=EMBEDDING_MODEL, input="hello")3Verify
Run the script again. The answer text prints and no APIRemovedInV1 error appears.
python app.pyConfirmed by github.com
Cause 2 of 2
A large code base that cannot be changed today
1Check
Decide whether you can touch the code now. The migration guide offers an automatic rewrite command for the first case, and pinning for the second.
pip install --upgrade openai
openai migrate2Fix
Run openai migrate (Mac or Linux; Windows needs WSL) on a clean git working tree and review the diff. If you cannot migrate yet, pin the old SDK in your requirements file so a fresh install does not jump to the new major version.
# requirements.txt
openai==0.28.03Verify
Confirm the installed version matches the pin, then run the app.
pip install -r requirements.txt && pip show openaiConfirmed by github.com
How do I stop it coming back?
- Pin the SDK major version in requirements.txt so an unattended upgrade does not change the API under you.
- Create one client object and import it, instead of calling module-level helpers from many files.
- Commit a clean tree before running openai migrate, then read the whole diff.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. 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.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
AI
TypeError when you index an OpenAI response like a dictionary
TypeError: 'ChatCompletion' object is not subscriptable
Since SDK 1.0 the OpenAI Python library returns Pydantic model objects instead of dictionaries, so response['choices'] fails.
2 causes, each with a fixAI
Azure OpenAI says the API deployment for this resource does not exist
The API deployment for this resource does not exist
Azure OpenAI does not take a model name in the model field.
2 causes, each with a fixAI
Azure OpenAI fails with an SSL certificate verification error
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]
The client checks the endpoint's certificate against its CA bundle, which is certifi by default.
2 causes, each with a fixAI
The OpenAI client says the API key is missing
Missing credentials. Please pass an `api_key`
OpenAI() looks for an api_key argument first and then the OPENAI_API_KEY environment variable.
3 causes, each with a fixAI
OpenAI says the model is a chat model and not supported in the v1/completions endpoint
This is a chat model and not supported in the v1/completions endpoint
The v1/completions endpoint accepts completion models such as gpt-3.5-turbo-instruct.
3 causes, each with a fixAI
TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
httpx 0.28.0 removed its deprecated proxies argument.
2 causes, each with a fix