This error occurs because AsyncStream objects from the OpenAI Python client need to be handled differently than regular iterables. Here are the correct ways to work with streaming responses:
Method 1: Use async for to iterate over the stream
python
import asyncio
from openai import AsyncOpenAI
async def stream_chat():
client = AsyncOpenAI()
stream = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
asyncio.run(stream_chat())
Method 2: Use the __aiter__() method explicitly
python
async def stream_chat():
client = AsyncOpenAI()
stream = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
async_iter = stream.__aiter__()
try:
while True:
chunk = await async_iter.__anext__()
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
except StopAsyncIteration:
pass
Method 3: Collect all chunks first (less efficient for streaming)
python
async def stream_chat():
client = AsyncOpenAI()
stream = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
chunks = []
async for chunk in stream:
chunks.append(chunk)
# Process chunks
for chunk in chunks:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
The key point is that AsyncStream is an async iterator, so you need to use async for or manually handle the async iteration protocol. Regular for loops or trying to convert it directly to a list won't work.
User
The issue is that the function is recursive and has yield from in it.
Assistant
Ah, that's the issue! When you have a recursive function with yield from and an AsyncStream, you need to handle the async iteration properly. Here are the solutions:
Solution 1: Use async for with yield
python
async def recursive_stream_handler(stream, depth=0):
if depth > some_limit:
return
async for chunk in stream:
yield chunk
# If you need to recurse based on chunk content
if some_condition(chunk):
# Create new stream for recursion
new_stream = await client.chat.completions.create(...)
async for nested_chunk in recursive_stream_handler(new_stream, depth + 1):
yield nested_chunk
Solution 2: Convert AsyncStream to async generator first
python
async def async_stream_to_generator(stream):
async for chunk in stream:
yield chunk
async def recursive_handler(stream, depth=0):
if depth > some_limit:
return
# Convert to async generator
generator = async_stream_to_generator(stream)
async for chunk in generator:
yield chunk
if some_condition(chunk):
new_stream = await client.chat.completions.create(...)
yield from recursive_handler(new_stream, depth + 1)
Solution 3: Collect chunks first, then yield from
python
async def recursive_handler(stream, depth=0):
if depth > some_limit:
return
# Collect all chunks from the stream
chunks = []
async for chunk in stream:
chunks.append(chunk)
# Now you can use yield from with the list
yield from chunks
# Handle recursion if needed
for chunk in chunks:
if some_condition(chunk):
new_stream = await client.chat.completions.create(...)
yield from recursive_handler(new_stream, depth + 1)
Solution 4: Manual async iteration
python
async def recursive_handler(stream, depth=0):
if depth > some_limit:
return
try:
async_iter = aiter(stream)
while True:
chunk = await anext(async_iter)
yield chunk
if some_condition(chunk):
new_stream = await client.chat.completions.create(...)
yield from recursive_handler(new_stream, depth + 1)
except StopAsyncIteration:
pass
The key issue is that yield from expects a regular iterable, but AsyncStream is an async iterable. You need to either manually iterate with async for and yield, or convert the async stream to a regular iterable first.
Which approach fits best depends on whether you want true streaming (Solutions 1, 2, 4) or if you're okay with buffering chunks first (Solution 3).