Skip to content

AttributeError: module 'openai' has no attribute 'Completions' when using the latest version and APIRemovedInV1 #1739

@N-eng66

Description

@N-eng66

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

I'm encountering an AttributeError when trying to use the openai.Completions attribute in my Python code. I'm using the latest version of the OpenAI Python library (version 1.47.1).
Here is the brief description of code:
import openai
input = "I love to travel"
response = sentiment_analysis(input)
print(input, "The sentiment is" , response)
When I run this code, I get the following error:
APIRemovedInV1:

You tried to access openai.Completion, 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 can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28

A detailed migration guide is available here: #742

To Reproduce

The code should successfully create a completion using the openai.Completions.create function.
Please let me know if there are any known issues or changes related to the openai.Completions attribute in the latest version of the library.
I've verified that I'm using the correct API key and that my internet connection is stable. I've also tried reinstalling the OpenAI library to ensure I'm using the latest version.
Any suggestions or workarounds would be greatly appreciated.

Code snippets

import openai
def sentiment_analysis(text):
  messages = [{"Role":"system", "content":"""You are trained to analyze an detect the sentiment of the given text.
                                             if you are unsure of answer you can say"Not sure" and recommend users to review manually."""},
              {"Role":"user", "content":"""Analyze the following text and determine if the sentiment is: positive or negative.
                                           return answer in single word as either positive or negative: {text}"""}]
  response = openai.Completion.create(
      engine="text-davinci-003", 
      messages=messages,
      max_tokens=1,
      n=1,
      stop=None,
      temperature=0)
  sentiment = response.choices[0].message.content.strip().lower()
  return sentiment
input = "I love to travel"
response = sentiment_analysis(input)
print(input, "The sentiment is" , response)

OS

windows

Python version

3.12.6

Library version

1.47.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions