Search code examples
pythonvisual-studio-codeerror-handlingconfigurationopenai-api

OpenAI API error: "You tried to access openai.Model, but this is no longer supported in openai\>=1.0.0"


Using Visual Studio Code and PyCharm, after install openai (pip install openai) a strange error is bugging me - please help.

If for example I write:

import openai

openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

lista_de_modelos = openai.Model.list()
print(lista_de_modelos)

it fails and I get an this error:

PS C:\\proyectoVS_Python\> & "C:/Users/kitkatuser/AppData/Local/Programs/Python/Python312/python.exe" "c:/proyectoVS_Python/import os.py"
Traceback (most recent call last):
File "c:\\proyectoVS_Python\\import os.py", line 5, in \<module\>
lista_de_modelos = openai.Model.list()
^^^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai_utils_proxy.py", line 22, in __getattr__
return getattr(self.__get_proxied__(), attr)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai_utils_proxy.py", line 43, in __get_proxied__  
return self.__load__()
^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai\\lib_old_api.py", line 33, in __load__
raise APIRemovedInV1(symbol=self.\_symbol)
openai.lib.\_old_api.APIRemovedInV1:

You tried to access openai.Model, 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: https://github.com/openai/openai-python/discussions/742

PS C:\\proyectoVS_Python\>

What I´m doing wrong? why I can't access openAI, I've try several keys, the same program and process to install works well with other friends. Using Pycharm shows similar. I'm used several programs to try but always similar response! I don't find a solution or similar problems! I'm really confused!Please Help


Solution

  • Problem

    The method name you're trying to use doesn't work with the OpenAI Python SDK version 1.0.0 or newer.

    The old SDK (i.e., version 0.28) works with the following method name:

    client.Model.list
    

    The new SDK (i.e., version 1.0.0 or newer) works with the following method name:

    client.models.list
    

    Note: Be careful because the API is case-sensitive (i.e., client.Models.list will not work with the new SDK version).

    Solution

    Try this:

    import os
    from openai import OpenAI
    client = OpenAI()
    OpenAI.api_key = os.getenv('OPENAI_API_KEY')
    
    client.models.list()