An async iterable asynchronously produces values for consumption with for await...of. In streaming applications, each yielded value can be processed or displayed as soon as it arrives. section 3.2.2
A practical async-iterable flow has three moving parts:
- An async generator or streaming API produces values over time. section 3.2.2
- Each yield makes the next value available without waiting for the complete result. section 3.2.2
- A for await...of loop consumes values until the iterable finishes; infinite generators require an explicit termination condition. section 3.2.2
Streaming generated text
Calling streamText returns textStream, an async iterable. Iterating with for await...of lets an application write each textPart to the output immediately instead of waiting for the full response. section 3.2.2
Listing 3.3 streamText example
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { streamText } from 'ai'; #1
const model = createGoogleGenerativeAI({
apiKey: process.env.GEMINI_API_KEY || '',
});
const { textStream } = await streamText({. #2
model: model('gemini-2.0-flash'),
prompt: 'Generate 400 words of random content for me please',
});
for await (const textPart of textStream) {. #3
process.stdout.write(textPart);
}
Inspect the runnable streamText pattern, including model setup, environment configuration, asynchronous iteration, and incremental output. section 3.2.2

