streamUI
AI SDK RSC is currently experimental. We recommend using AI SDK UI for production. For guidance on migrating from RSC to UI, see our migration guide.
A helper function to create a streamable UI from LLM providers. This function is similar to AI SDK Core APIs and supports the same model interfaces.
To see streamUI in action, check out these examples.
Import
<Snippet text={import { streamUI } from "@ai-sdk/rsc"} prompt={false} />
Parameters
<PropertiesTable
content={[
{
name: 'model',
type: 'LanguageModel',
description: 'The language model to use. Example: openai("gpt-4.1")',
},
{
name: 'initial',
isOptional: true,
type: 'ReactNode',
description: 'The initial UI to render.',
},
{
name: 'system',
type: 'string',
description:
'The system prompt to use that specifies the behavior of the model.',
},
{
name: 'prompt',
type: 'string',
description: 'The input prompt to generate the text from.',
},
{
name: 'messages',
type: 'Array<CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage> | Arraytemperature or topP, but not both.',
},
{
name: 'topP',
type: 'number',
isOptional: true,
description:
'Nucleus sampling. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either temperature or topP, but not both.',
},
{
name: 'topK',
type: 'number',
isOptional: true,
description:
'Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.',
},
{
name: 'presencePenalty',
type: 'number',
isOptional: true,
description:
'Presence penalty setting. It affects the likelihood of the model to repeat information that is already in the prompt. The value is passed through to the provider. The range depends on the provider and model.',
},
{
name: 'frequencyPenalty',
type: 'number',
isOptional: true,
description:
'Frequency penalty setting. It affects the likelihood of the model to repeatedly use the same words or phrases. The value is passed through to the provider. The range depends on the provider and model.',
},
{
name: 'stopSequences',
type: 'string[]',
isOptional: true,
description:
'Sequences that will stop the generation of the text. If the model generates any of these sequences, it will stop generating further text.',
},
{
name: 'seed',
type: 'number',
isOptional: true,
description:
'The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.',
},
{
name: 'maxRetries',
type: 'number',
isOptional: true,
description:
'Maximum number of retries. Set to 0 to disable retries. Default: 2.',
},
{
name: 'abortSignal',
type: 'AbortSignal',
isOptional: true,
description:
'An optional abort signal that can be used to cancel the call.',
},
{
name: 'headers',
type: 'Record<string, string>',
isOptional: true,
description:
'Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.',
},
{
name: 'tools',
type: 'ToolSet',
description:
'Tools that are accessible to and can be called by the model.',
properties: [
{
type: 'Tool',
parameters: [
{
name: 'description',
isOptional: true,
type: 'string',
description:
'Information about the purpose of the tool including details on how and when it can be used by the model.',
},
{
name: 'parameters',
type: 'zod schema',
description:
'The typed schema that describes the parameters of the tool that can also be used to validation and error handling.',
},
{
name: 'generate',
isOptional: true,
type: '(async (parameters) => ReactNode) | AsyncGenerator<ReactNode, ReactNode, void>',
description:
'A function or a generator function that is called with the arguments from the tool call and yields React nodes as the UI.',
},
],
},
],
},
{
name: 'toolChoice',
isOptional: true,
type: '"auto" | "none" | "required" | { "type": "tool", "toolName": string }',
description:
'The tool choice setting. It specifies how tools are selected for execution. The default is "auto". "none" disables tool execution. "required" requires tools to be executed. { "type": "tool", "toolName": string } specifies a specific tool to execute.',
},
{
name: 'text',
isOptional: true,
type: '(Text) => ReactNode',
description: 'Callback to handle the generated tokens from the model.',
properties: [
{
type: 'Text',
parameters: [
{
name: 'content',
type: 'string',
description: 'The full content of the completion.',
},
{ name: 'delta', type: 'string', description: 'The delta.' },
{ name: 'done', type: 'boolean', description: 'Is it done?' },
],
},
],
},
{
name: 'providerOptions',
type: 'Record<string,Record<string,JSONValue>> | undefined',
isOptional: true,
description:
'Provider-specific options. The outer key is the provider name. The inner values are the metadata. Details depend on the provider.',
},
{
name: 'onFinish',
type: '(result: OnFinishResult) => void',
isOptional: true,
description:
'Callback that is called when the LLM response and all request tool executions (for tools that have a generate function) are finished.',
properties: [
{
type: 'OnFinishResult',
parameters: [
{
name: 'usage',
type: 'TokenUsage',
description: 'The token usage of the generated text.',
properties: [
{
type: 'TokenUsage',
parameters: [
{
name: 'promptTokens',
type: 'number',
description: 'The total number of tokens in the prompt.',
},
{
name: 'completionTokens',
type: 'number',
description:
'The total number of tokens in the completion.',
},
{
name: 'totalTokens',
type: 'number',
description: 'The total number of tokens generated.',
},
],
},
],
},
{
name: 'value',
type: 'ReactNode',
description: 'The final ui node that was generated.',
},
{
name: 'warnings',
type: 'Warning[] | undefined',
description:
'Warnings from the model provider (e.g. unsupported settings).',
},
{
name: 'response',
type: 'Response',
description: 'Optional response data.',
properties: [
{
type: 'Response',
parameters: [
{
name: 'headers',
isOptional: true,
type: 'Record<string, string>',
description: 'Response headers.',
},
],
},
],
},
],
},
],
},
]}
/>
Returns
<PropertiesTable
content={[
{
name: 'value',
type: 'ReactNode',
description: 'The user interface based on the stream output.',
},
{
name: 'response',
type: 'Response',
isOptional: true,
description: 'Optional response data.',
properties: [
{
type: 'Response',
parameters: [
{
name: 'headers',
isOptional: true,
type: 'Record<string, string>',
description: 'Response headers.',
},
],
},
],
},
{
name: 'warnings',
type: 'Warning[] | undefined',
description:
'Warnings from the model provider (e.g. unsupported settings).',
},
{
name: 'stream',
type: 'AsyncIterable
Examples
<ExampleLinks examples={[ { title: 'Learn to render a React component as a function call using a language model in Next.js', link: '/examples/next-app/state-management/ai-ui-states', }, { title: 'Learn to persist and restore states UI/AI states in Next.js', link: '/examples/next-app/state-management/save-and-restore-states', }, { title: 'Learn to route React components using a language model in Next.js', link: '/examples/next-app/interface/route-components', }, { title: 'Learn to stream component updates to the client in Next.js', link: '/examples/next-app/interface/stream-component-updates', }, ]} />