useChat()
Allows you to easily create a conversational user interface for your chatbot application. It enables the streaming of chat messages from your AI provider, manages the chat state, and updates the UI automatically as new messages are received.
The useChat API has been significantly updated in AI SDK 5.0. It now uses a
transport-based architecture and no longer manages input state internally. See
the migration
guide for
details.
Import
<Tabs items={['React', 'Svelte', 'Vue']}>
API Signature
Parameters
<PropertiesTable
content={[
{
name: 'chat',
type: 'Chat/api/chat endpoint.',
properties: [
{
type: 'DefaultChatTransport',
parameters: [
{
name: 'api',
type: "string = '/api/chat'",
isOptional: true,
description: 'The API endpoint for chat requests.',
},
{
name: 'credentials',
type: 'RequestCredentials',
isOptional: true,
description: 'The credentials mode for fetch requests.',
},
{
name: 'headers',
type: 'Record<string, string> | Headers',
isOptional: true,
description: 'HTTP headers to send with requests.',
},
{
name: 'body',
type: 'object',
isOptional: true,
description: 'Extra body object to send with requests.',
},
{
name: 'prepareSendMessagesRequest',
type: 'PrepareSendMessagesRequest',
isOptional: true,
description:
'A function to customize the request before chat API calls.',
properties: [
{
type: 'PrepareSendMessagesRequest',
parameters: [
{
name: 'options',
type: 'PrepareSendMessageRequestOptions',
description: 'Options for preparing the request',
properties: [
{
type: 'PrepareSendMessageRequestOptions',
parameters: [
{
name: 'id',
type: 'string',
description: 'The chat ID',
},
{
name: 'messages',
type: 'UIMessage[]',
description: 'Current messages in the chat',
},
{
name: 'requestMetadata',
type: 'unknown',
description: 'The request metadata',
},
{
name: 'body',
type: 'Record<string, any> | undefined',
description: 'The request body',
},
{
name: 'credentials',
type: 'RequestCredentials | undefined',
description: 'The request credentials',
},
{
name: 'headers',
type: 'HeadersInit | undefined',
description: 'The request headers',
},
{
name: 'api',
type: 'string',
description: The API endpoint to use for the request. If not specified, it defaults to the transport’s API endpoint: /api/chat.,
},
{
name: 'trigger',
type: "'submit-message' | 'regenerate-message'",
description: 'The trigger for the request',
},
{
name: 'messageId',
type: 'string | undefined',
description: 'The message ID if applicable',
},
],
},
],
},
],
},
],
},
{
name: 'prepareReconnectToStreamRequest',
type: 'PrepareReconnectToStreamRequest',
isOptional: true,
description:
'A function to customize the request before reconnect API call.',
properties: [
{
type: 'PrepareReconnectToStreamRequest',
parameters: [
{
name: 'options',
type: 'PrepareReconnectToStreamRequestOptions',
description:
'Options for preparing the reconnect request',
properties: [
{
type: 'PrepareReconnectToStreamRequestOptions',
parameters: [
{
name: 'id',
type: 'string',
description: 'The chat ID',
},
{
name: 'requestMetadata',
type: 'unknown',
description: 'The request metadata',
},
{
name: 'body',
type: 'Record<string, any> | undefined',
description: 'The request body',
},
{
name: 'credentials',
type: 'RequestCredentials | undefined',
description: 'The request credentials',
},
{
name: 'headers',
type: 'HeadersInit | undefined',
description: 'The request headers',
},
{
name: 'api',
type: 'string',
description: The API endpoint to use for the request. If not specified, it defaults to the transport’s API endpoint combined with the chat ID: /api/chat/{chatId}/stream.,
},
],
},
],
},
],
},
],
},
],
},
],
},
{
name: 'id',
type: 'string',
isOptional: true,
description:
'A unique identifier for the chat. If not provided, a random one will be generated.',
},
{
name: 'messages',
type: 'UIMessage[]',
isOptional: true,
description: 'Initial chat messages to populate the conversation with.',
},
{
name: 'onToolCall',
type: '({toolCall: ToolCall}) => void | PromiseTrue if errors during streaming caused the response to stop early.,
},
{
name: 'finishReason',
type: "'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other' | 'unknown'",
isOptional: true,
description:
'The reason why the model finished generating the response. Undefined if the finish reason was not provided by the model.',
},
],
},
],
},
{
name: 'onError',
type: '(error: Error) => void',
isOptional: true,
description:
'Callback function to be called when an error is encountered.',
},
{
name: 'onData',
type: '(dataPart: DataUIPart) => void',
isOptional: true,
description:
'Optional callback function that is called when a data part is received.',
},
{
name: 'experimental_throttle',
type: 'number',
isOptional: true,
description:
'Custom throttle wait in ms for the chat messages and data updates. Default is undefined, which disables throttling.',
},
{
name: 'resume',
type: 'boolean',
isOptional: true,
description:
'Whether to resume an ongoing chat generation stream. Defaults to false.',
},
]}
/>
Returns
<PropertiesTable content={[ { name: 'id', type: 'string', description: 'The id of the chat.', }, { name: 'messages', type: 'UIMessage[]', description: 'The current array of chat messages.', properties: [ { type: 'UIMessage', parameters: [ { name: 'id', type: 'string', description: 'A unique identifier for the message.', }, { name: 'role', type: "'system' | 'user' | 'assistant'", description: 'The role of the message.', }, { name: 'parts', type: 'UIMessagePart[]', description: 'The parts of the message. Use this for rendering the message in the UI.', }, { name: 'metadata', type: 'unknown', isOptional: true, description: 'The metadata of the message.', }, ], }, ], }, { name: 'status', type: "'submitted' | 'streaming' | 'ready' | 'error'", description: 'The current status of the chat: "ready" (idle), "submitted" (request sent), "streaming" (receiving response), or "error" (request failed).', }, { name: 'error', type: 'Error | undefined', description: 'The error object if an error occurred.', }, { name: 'sendMessage', type: '(message: CreateUIMessage | string, options?: ChatRequestOptions) => void', description: 'Function to send a new message to the chat. This will trigger an API call to generate the assistant response.', properties: [ { type: 'ChatRequestOptions', parameters: [ { name: 'headers', type: 'Record<string, string> | Headers', description: 'Additional headers that should be to be passed to the API endpoint.', }, { name: 'body', type: 'object', description: 'Additional body JSON properties that should be sent to the API endpoint.', }, { name: 'data', type: 'JSONValue', description: 'Additional data to be sent to the API endpoint.', }, ], }, ], }, { name: 'regenerate', type: '(options?: { messageId?: string }) => void', description: 'Function to regenerate the last assistant message or a specific message. If no messageId is provided, regenerates the last assistant message.', }, { name: 'stop', type: '() => void', description: 'Function to abort the current streaming response from the assistant.', }, { name: 'clearError', type: '() => void', description: 'Clears the error state.', }, { name: 'resumeStream', type: '() => void', description: 'Function to resume an interrupted streaming response. Useful when a network error occurs during streaming.', }, { name: 'addToolOutput', type: '(options: { tool: string; toolCallId: string; output: unknown } | { tool: string; toolCallId: string; state: "output-error", errorText: string }) => void', description: 'Function to add a tool result to the chat. This will update the chat messages with the tool result. If sendAutomaticallyWhen is configured, it may trigger an automatic submission.', }, { name: 'setMessages', type: '(messages: UIMessage[] | ((messages: UIMessage[]) => UIMessage[])) => void', description: 'Function to update the messages state locally without triggering an API call. Useful for optimistic updates.', }, ]} />