openai-python
openai-python copied to clipboard
Linting issues with Pyright
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
- Use Pyright LSP (likely also affects Pylance)
- 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
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].
This also seems to affect completions in vscode (also on Linux, python 3.11.4).
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.
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!