content/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx
createUIMessageStreamResponseThe createUIMessageStreamResponse function creates a Response object that streams UI messages to the client.
<Snippet
text={import { createUIMessageStreamResponse } from "ai"}
prompt={false}
/>
import {
createUIMessageStream,
createUIMessageStreamResponse,
streamText,
} from 'ai';
__PROVIDER_IMPORT__;
const response = createUIMessageStreamResponse({
status: 200,
statusText: 'OK',
headers: {
'Custom-Header': 'value',
},
stream: createUIMessageStream({
execute({ writer }) {
// Write custom data (type must be 'data-<name>')
writer.write({
type: 'data-message',
data: { content: 'Hello' },
});
// Write text content using start/delta/end pattern
writer.write({
type: 'text-start',
id: 'greeting-text',
});
writer.write({
type: 'text-delta',
id: 'greeting-text',
delta: 'Hello, world!',
});
writer.write({
type: 'text-end',
id: 'greeting-text',
});
// Write source information (flat properties, not nested)
writer.write({
type: 'source-url',
sourceId: 'source-1',
url: 'https://example.com',
title: 'Example Source',
});
// Merge with LLM stream
const result = streamText({
model: __MODEL__,
prompt: 'Say hello',
});
writer.merge(result.toUIMessageStream());
},
}),
});
<PropertiesTable content={[ { name: 'stream', type: 'ReadableStream<UIMessageChunk>', description: 'The UI message stream to send to the client.', }, { name: 'status', type: 'number', isOptional: true, description: 'The status code for the response. Defaults to 200.', }, { name: 'statusText', type: 'string', isOptional: true, description: 'The status text for the response.', }, { name: 'headers', type: 'Headers | Record<string, string>', isOptional: true, description: 'Additional headers for the response.', }, { name: 'consumeSseStream', type: '(options: { stream: ReadableStream<string> }) => PromiseLike<void> | void', isOptional: true, description: 'Optional callback to consume the Server-Sent Events stream.', }, ]} />
Response
A Response object that streams UI message chunks with the specified status, headers, and content.