Tickd.ai
API errors

Fix OpenAI AttributeError ChatCompletion Error

Updated 10/25/2026

Why This Error Occurs

If you recently upgraded your Python environment or the openai package, you might encounter the following error when running your scripts:

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

This issue occurs because OpenAI released version 1.0.0 of its Python SDK, which introduced major, backward-incompatible breaking changes. In the new SDK structure, the global module-level functions—such as openai.ChatCompletion.create()—have been entirely deprecated and removed.

Instead, the library now uses a client-based instantiation model (client.chat.completions.create). If your codebase still references the older pre-v1 style of calling the API, Python will throw an AttributeError because those attributes no longer exist in the package you installed.

---

Agent Builder turns a task you repeat into a reusable assistant with setup text for each app.

Tickd is independent and not affiliated with any AI company.

Open Agent Builder

How to Fix "AttributeError: module 'openai' has no attribute 'ChatCompletion'"

1. Check Your Installed SDK Version

First, verify which version of the OpenAI Python library is currently installed in your environment. Run the following command in your terminal:

`bash pip show openai `

Look at the Version line. If it is 1.0.0 or higher (e.g., 1.12.0), your legacy code is incompatible with this library version. If you need to keep using your legacy code, skip to Step 4. If you want to modernize your code, proceed to Step 2.

2. Rewrite Your Code Using the Modern Client Pattern

The modern SDK requires you to instantiate an OpenAI client object instead of importing and modifying global settings.

#### Legacy Code Pattern (Pre-v1.0.0)

`python import openai

openai.api_key = "your-api-key-here"

response = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "user", "content": "Hello!"}] ) print(response['choices'][0]['message']['content']) `

#### Modern Code Pattern (v1.0.0 and Above)

`python from openai import OpenAI

The client automatically pulls from the OPENAI_API_KEY environment variable. # Alternatively, pass it explicitly: client = OpenAI(api_key="your-api-key-here") client = OpenAI()

response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello!"}] ) # Note: Response fields are now attributes, not dictionary keys print(response.choices[0].message.content) `

Key changes to implement: * Change import openai to from openai import OpenAI. * Initialize the client using client = OpenAI(). * Replace openai.ChatCompletion.create(...) with client.chat.completions.create(...). * Update how you access the response payload. Instead of dictionary syntax (response['choices']), use object property syntax (response.choices).

3. Run the Automated Migration Script

If you have a large codebase and do not want to rewrite every file manually, OpenAI provides an automated migration tool built into their CLI toolset.

Run this command in your terminal pointing to your project directory or specific file:

`bash openai migrate `

Or target a specific directory:

`bash openai tools migrate -path /path/to/your/project/dir `

*Warning: Make sure you commit your code to Git or back up your files before running this tool, as it will overwrite your Python files in place.*

4. Downgrade Your SDK Version (Temporary Workaround)

If you are working with legacy codebases, dependencies, or third-party packages that cannot be updated immediately, you can temporarily downgrade your SDK to the last v0 release.

Run the following commands in your terminal:

`bash pip uninstall openai pip install "openai<1.0.0" `

This installs version 0.28.1 (or similar), restoring the legacy openai.ChatCompletion functionality. Note that this is a temporary solution; older versions will not receive new features, safety updates, or support for newer model classes.

5. Clear Cached Python Builds

If you updated your packages but Python still references cached, broken versions, try reinstalling with no cache, or rebuild your virtual environment:

`bash pip install --force-reinstall openai `

---

When to Escalate

If you have updated your syntax or successfully downgraded your library but still receive importing errors, the problem may lie within your Python path. Ensure your IDE (like VS Code or PyCharm) is set to the same virtual environment (venv or conda) where you installed or downgraded the package.

If the error persists even in a completely fresh virtual environment with standard v1 code, check the official OpenAI Python GitHub Repository issues tab to see if a recent minor release introduced an integration bug.

While you're here

Tickd is more than troubleshooting — these three are free and take seconds.

Agent BuilderDesign your own AI agent and export it to ChatGPT, Claude, Gemini or Grok.Build one free