API: No response after OpenAI upgrade to version 1.3.5

UPDATE: Figured it out myself and put working version within code snippets, below, with help on pypi-dot-org documentation for openai version 1.3.#.

I’ve a python code that had been working just fine with OpenAI module version 0.28.1. However, after upgrading the module to version 1.3.5 and migrating my codes with the command “openai migrate”, API calls do not successfully receive response data.

Looks like the following two code snippets receive empty contents in “r.choices[0].message[“content”]” as well as each of “r_item.choices[0].delta”.

Code Snippet #1 (for non-streaming messages)

r = await aclient.chat.completions.create(model=self.model,
messages=messages,
**OPENAI_COMPLETION_OPTIONS)

doesn’t work

answer = r.choices[0].message[“content”]

working version

answer = r.messages[“content”]

Code Snippet #2 (for streaming messages)

r_gen = await aclient.chat.completions.create(model=self.model,
messages=messages,
stream=True,
**OPENAI_COMPLETION_OPTIONS)

answer = “”
async for r_item in r_gen:

doesn’t work

delta = r_item.choices[0].delta
if "content" in delta:
    answer += delta.content

working version

delta = r_item.choices[0].delta.content
    if delta:
        answer += delta

I ran into a similar error when upgrading to 1.1.1. I had to modify the response from

return response.choices[0].message[“content”]

to

return response.choices[0].message.content

After my update the original code was giving me the error message:
TypeError: ‘ChatCompletionMessage’ object is not subscriptable

Thanks for your reply but your version hadn’t worked for me. However, I’ve later resolved the error: see the “UPDATE” line in my original posting.

For your code try the following, which simply drops out “choices[0]” from your original code.

return response.message[“content”]

Note the working code for streaming messages in my post if you actually have code for that.