Skip to content

TypeError: 'LegacyAPIResponse' object is not iterable #1115

Closed as not planned
@psymbio

Description

@psymbio

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

Not able to get a streaming response because 'LegacyAPIResponse' object is not iterable.

To Reproduce

The following code:

import os
import openai
from openai import AzureOpenAI

os.environ['AZURE_OPENAI_API_KEY'] = "xxx"
client = AzureOpenAI(
    api_version="2023-07-01-preview",
    azure_endpoint="https://xxx.openai.azure.com/",
)

stream = client.chat.completions.with_raw_response.create(
    messages=[{
        "role": "user",
        "content": "sing me a song",
    }],
    model="gpt-35-turbo",
    max_tokens=30,
    temperature=0.7,
    stream=True
)
print(stream)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Results in:

<APIResponse [200 OK] type=<class 'openai.types.chat.chat_completion.ChatCompletion'>>
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    for chunk in stream:
TypeError: 'LegacyAPIResponse' object is not iterable

Code snippets

No response

OS

WSL2 Ubuntu

Python version

Python v3.9.7

Library version

openai-python v1.10.0

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