Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

Old openai.ChatCompletion or openai.Embedding code breaks on a new SDK

ErrorYou tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0

Short 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.

  • Applies to openai Python SDK 1.x and later, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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__)"
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2Dozens 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

What does the error look like?

  • You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0
  • APIRemovedInV1

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.py

Confirmed by github.com

Did this fix it?

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 migrate

2Fix

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.0

3Verify

Confirm the installed version matches the pin, then run the app.

pip install -r requirements.txt && pip show openai

Confirmed by github.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.