createStreamableValue creates a server-managed value that can be updated repeatedly and streamed to a client in real time, making incrementally generated AI responses visible before completion. section 4.1.2
A server action typically uses it in this sequence:
- Create the streamable value after selecting a supported AI model. section 4.1.2
- Call streamText with conversation history and obtain its textStream. section 4.1.2
- Update the streamable value for each generated text chunk, then call done(). section 4.1.2
- Return stream.value alongside the existing messages so the client can render the live response. section 4.1.2
Listing 4.1 Server action that creates a streamable conversation
'use server'; #1
import { streamText } from 'ai';
import { createStreamableValue } from 'ai/rsc'; #2
export async function
continueConversation(history, provider, model) {
'use server';
const supportedModel = getSupportedModel(provider, model);
const stream = createStreamableValue(); #3
(async () => {
const { textStream } = await streamText({
model: supportedModel,
system: "I'm happy to assist you in any way I can.
How can I be of service today?",
messages: history,
});
for await (const text of textStream) { #4
stream.update(text);
}
stream.done();
})();
return { #5
messages: history,
newMessage: stream.value,
};
}
Inspect the complete server action connecting createStreamableValue, streamText, asynchronous iteration, and the returned conversation state. section 4.1.2
