How to Resolve 'module openai has no attribute ChatCompletion' Error in Python Code Using OpenAI Library?

This topic was automatically generated from Slack. You can find the original thread here.

I wrote python code using the openai library that worked a week or so ago but now I am getting the error “module ‘openai’ has no attribute ‘ChatCompletion’” when calling ChatCompletion. How should I use the openai library to successfully create a chat completion? Has the OpenAi library version used by pipedream changed?

It seems like you’re trying to use the OpenAI library in Python. However, Pipedream provides pre-built actions for OpenAI that you can use directly from the Pipedream UI, without writing any code. For creating a chat completion, you can use the OpenAI (ChatGPT) app’s “Chat” action. Here’s how you can do it: 1. Search for the OpenAI (ChatGPT) app from the step menu 2. Select the Chat action 3. Connect your OpenAI (ChatGPT) account 4. Select a Model or pass a string from a previous step. 5. Select a User Message or pass a string from a previous step. 6. At the bottom of the step, click Test to run the action and test the configuration. You can find more details at https://pipedream.com/apps/openai/actions/chat. If you still encounter issues or need further assistance, please visit https://pipedream.com/support for more support options.

I am calling openai in a loop so this will not. help.

Hi . It looks like openai’s v1.0.0 release last week introduced breaking changes to how the completions API is called. And Pipedream uses the latest version of a package when you test or deploy a workflow. To fix the error you’re getting, I think you can either:

A) Use an earlier version of the openai package via package pinning in your Python code step. For example, to use v0.28.1:

# pipedream add-package openai==0.28.1
import openai

B) Migrate your Python code to use the latest version of openai. For example (from the v1.0.0 announcement):

# old
import openai

completion = openai.ChatCompletion.acreate(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])

# new
from openai import AsyncOpenAI

client = AsyncOpenAI()
completion = await client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])