openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Linting issues with Pyright

Open worldofgeese opened this issue 1 year ago • 1 comments

Describe the bug

Given reply = response["choices"][0]["message"]["content"], Pyright throws linting errors:

 chatbot.py    49  27 error    reportGeneralTypeIssues "__getitem__" method not defined on type "Generator[Unknown | list[Unknown] | dict[Unknown, Unknown], None, None]" (lsp)
 chatbot.py    49  27 error    reportGeneralTypeIssues Argument of type "Literal['usage']" cannot be assigned to parameter "__s" of type "slice" in function "__getitem__"
   "Literal['usage']" is incompatible with "slice" (lsp)

The code works but something wonky appears to be afoot.

To Reproduce

  1. Use Pyright LSP (likely also affects Pylance)
  2. View errors when using any ChatGPT API example where a response is fetched

Code snippets

import openai

messages = []
system_msg = input("What type of chatbot would you like to create? ")
messages.append({"role": "system", "content": system_msg})

print("Say hello to your new assistant!")
while input != "quit()": 
    message = input()
    messages.append({"role": "user", "content": message})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages)
    reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": reply})
    print("\n" + reply + "\n")

OS

Linux

Python version

Python 3.11.1

Library version

openai 0.27.0

worldofgeese avatar Mar 04 '23 08:03 worldofgeese

you can add pyrightconfig.json { "reportGeneralTypeIssues": false } to aviod the error.

But i got another isuue. it wont show the attributions (method, variable etc) after choices[0].

NateCC0902 avatar Mar 23 '23 19:03 NateCC0902

This also seems to affect completions in vscode (also on Linux, python 3.11.4).

mindkeep avatar Jul 31 '23 00:07 mindkeep

A workaround to fix the linter error is to cast the response, which is a generator, to an actual OpenAIObject:

from typing import cast

import openai
from openai.openai_object import OpenAIObject

messages = []
system_msg = input("What type of chatbot would you like to create? ")
messages.append({"role": "system", "content": system_msg})

print("Say hello to your new assistant!")
while input != "quit()":
    message = input()
    messages.append({"role": "user", "content": message})
    response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
    response = cast(OpenAIObject, response)  # casting the response here!
    reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": reply})
    print("\n" + reply + "\n")

It does not fix the completions in Vscode, however.

pabloesm avatar Aug 12 '23 11:08 pabloesm

Great news, we've just published a beta for the a fully-rewritten v1 of our Python SDK, which includes proper static types for all request parameters & response fields.

We'd love your feedback, so please give it a try and share your thoughts (good and bad, major and minor) in the linked discussion!

rattrayalex avatar Sep 29 '23 21:09 rattrayalex