---
title: useChat
description: API reference for the useChat hook.
---
# `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 state for chat input, and updates the UI automatically as new messages are received.
## Import
## API Signature
### Parameters
void | unknown| Promise',
description:
'Optional callback function that is invoked when a tool call is received. Intended for automatic client-side tool execution. You can optionally return a result for the tool call, either synchronously or asynchronously.',
},
{
name: 'onResponse',
type: '(response: Response) => void',
description:
'An optional callback that will be called with the response from the API endpoint. Useful for throwing customized errors or logging',
},
{
name: 'onFinish',
type: '(message: Message, options: OnFinishOptions) => void',
description:
'An optional callback function that is called when the completion stream ends.',
properties: [
{
type: 'OnFinishOptions',
parameters: [
{
name: 'usage',
type: 'CompletionTokenUsage',
description: 'The token usage for the completion.',
properties: [
{
type: 'CompletionTokenUsage',
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: 'finishReason',
type: "'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other' | 'unknown'",
description: 'The reason why the generation ended.',
},
],
},
],
},
{
name: 'onError',
type: '(error: Error) => void',
description:
'An optional callback that will be called when the chat stream encounters an error.',
},
{
name: 'generateId',
type: '() => string',
description:
'Optional. A way to provide a function that is going to be used for ids for messages. If not provided generateId is used by default.',
},
{
name: 'headers',
type: 'Record | Headers',
description:
'An optional object of headers to be passed to the API endpoint.',
},
{
name: 'body',
type: 'any',
description:
'An optional, additional body object to be passed to the API endpoint.',
},
{
name: 'credentials',
type: "'omit' | 'same-origin' | 'include'",
description:
'An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.',
},
{
name: 'sendExtraMessageFields',
type: 'boolean',
description:
"An optional boolean that determines whether to send extra fields you've added to `messages`. Defaults to `false` and only the `content` and `role` fields will be sent to the API endpoint. If set to `true`, the `name`, `data`, and `annotations` fields will also be sent.",
},
{
name: 'maxToolRoundtrips',
type: 'number',
description:
'React and SolidJS only. Maximal number of automatic roundtrips for tool calls. An automatic tool call roundtrip is a call to the server with the tool call results when all tool calls in the last assistant message have results. A maximum number is required to prevent infinite loops in the case of misconfigured tools. By default, it is set to 0, which will disable the feature.',
},
{
name: 'streamProtocol',
type: "'text' | 'data'",
optional: true,
description:
'An optional literal that sets the type of stream to be used. Defaults to `data`. If set to `text`, the stream will be treated as a text stream.',
},
{
name: 'fetch',
type: 'FetchFunction',
optional: true,
description:
'Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.',
},
{
name: 'experimental_prepareRequestBody',
type: '(options: { messages: Message[]; requestData?: JSONValue; requestBody?: object }) => JSONValue',
optional: true,
description:
'Experimental (React only). When a function is provided, it will be used to prepare the request body for the chat API. This can be useful for customizing the request body based on the messages and data in the chat.',
},
]}
/>
### Returns
',
isOptional: true,
description:
'Additional annotations sent along with the message.',
},
{
name: 'toolInvocations',
type: 'Array',
isOptional: true,
description:
'An array of tool invocations that are associated with the (assistant) message.',
properties: [
{
type: 'ToolInvocation',
parameters: [
{
name: 'state',
type: "'partial-call'",
description:
'The state of the tool call when it was partially created.',
},
{
name: 'toolCallId',
type: 'string',
description:
'ID of the tool call. This ID is used to match the tool call with the tool result.',
},
{
name: 'toolName',
type: 'string',
description: 'Name of the tool that is being called.',
},
{
name: 'args',
type: 'any',
description:
'Partial arguments of the tool call. This is a JSON-serializable object.',
},
],
},
{
type: 'ToolInvocation',
parameters: [
{
name: 'state',
type: "'call'",
description:
'The state of the tool call when it was fully created.',
},
{
name: 'toolCallId',
type: 'string',
description:
'ID of the tool call. This ID is used to match the tool call with the tool result.',
},
{
name: 'toolName',
type: 'string',
description: 'Name of the tool that is being called.',
},
{
name: 'args',
type: 'any',
description:
'Arguments of the tool call. This is a JSON-serializable object that matches the tools input schema.',
},
],
},
{
type: 'ToolInvocation',
parameters: [
{
name: 'state',
type: "'result'",
description:
'The state of the tool call when the result is available.',
},
{
name: 'toolCallId',
type: 'string',
description:
'ID of the tool call. This ID is used to match the tool call with the tool result.',
},
{
name: 'toolName',
type: 'string',
description: 'Name of the tool that is being called.',
},
{
name: 'args',
type: 'any',
description:
'Arguments of the tool call. This is a JSON-serializable object that matches the tools input schema.',
},
{
name: 'result',
type: 'any',
description: 'The result of the tool call.',
},
],
},
],
},
{
name: 'experimental_attachments',
type: 'Array',
isOptional: true,
description:
'Additional attachments sent along with the message.',
properties: [
{
type: 'Attachment',
description:
'An attachment object that can be used to describe the metadata of the file.',
parameters: [
{
name: 'name',
type: 'string',
isOptional: true,
description:
'The name of the attachment, usually the file name.',
},
{
name: 'contentType',
type: 'string',
isOptional: true,
description:
'A string indicating the media type of the file.',
},
{
name: 'url',
type: 'string',
description:
'The URL of the attachment. It can either be a URL to a hosted file or a Data URL.',
},
],
},
],
},
],
},
],
},
{
name: 'error',
type: 'Error | undefined',
description: 'An error object returned by SWR, if any.',
},
{
name: 'append',
type: '(message: Message | CreateMessage, options?: ChatRequestOptions) => Promise',
description:
'Function to append a message to the chat, triggering an API call for the AI response. It returns a promise that resolves to full response message content when the API call is successfully finished, or throws an error when the API call fails.',
properties: [
{
type: 'ChatRequestOptions',
parameters: [
{
name: 'headers',
type: 'Record | 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: 'reload',
type: '() => Promise',
description:
"Function to reload the last AI chat response for the given chat history. If the last message isn't from the assistant, it will request the API to generate a new response.",
},
{
name: 'stop',
type: '() => void',
description: 'Function to abort the current API request.',
},
{
name: 'setMessages',
type: '(messages: Message[] | ((messages: Message[]) => Message[]) => void',
description:
'Function to update the `messages` state locally without triggering an API call.',
},
{
name: 'input',
type: 'string',
description: 'The current value of the input field.',
},
{
name: 'setInput',
type: 'React.Dispatch>',
description: 'Function to update the `input` value.',
},
{
name: 'handleInputChange',
type: '(event: any) => void',
description:
"Handler for the `onChange` event of the input field to control the input's value.",
},
{
name: 'handleSubmit',
type: '(event?: { preventDefault?: () => void }, options?: ChatRequestOptions) => void',
description:
'Form submission handler that automatically resets the input field and appends a user message. You can use the `options` parameter to send additional data, headers and more to the server.',
properties: [
{
type: 'ChatRequestOptions',
parameters: [
{
name: 'headers',
type: 'Record | 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: 'allowEmptySubmit',
type: 'boolean',
isOptional: true,
description:
'A boolean that determines whether to allow submitting an empty input that triggers a generation. Defaults to `false`.',
},
{
name: 'experimental_attachments',
type: 'FileList | Array',
isOptional: true,
description:
'An array of attachments to be sent to the API endpoint.',
properties: [
{
type: 'FileList',
parameters: [
{
name: '',
type: '',
description:
"A list of files that have been selected by the user using an element. It's also used for a list of files dropped into web content when using the drag and drop API.",
},
],
},
{
type: 'Attachment',
description:
'An attachment object that can be used to describe the metadata of the file.',
parameters: [
{
name: 'name',
type: 'string',
isOptional: true,
description:
'The name of the attachment, usually the file name.',
},
{
name: 'contentType',
type: 'string',
isOptional: true,
description:
'A string indicating the media type of the file.',
},
{
name: 'url',
type: 'string',
description:
'The URL of the attachment. It can either be a URL to a hosted file or a Data URL.',
},
],
},
],
},
],
},
],
},
{
name: 'isLoading',
type: 'boolean',
description:
'Boolean flag indicating whether a request is currently in progress.',
},
{
name: 'data',
type: 'JSONValue[]',
description: 'Data returned from StreamData',
},
{
name: 'addToolResult',
type: '({toolCallId: string; result: any;}) => void',
description:
'React and SolidJS only. Function to add a tool result to the chat. This will update the chat messages with the tool result and call the API route if all tool results for the last message are available.',
},
]}
/>
## Learn more
- [Chatbot](/docs/ai-sdk-ui/chatbot)
- [Chatbot with Tools](/docs/ai-sdk-ui/chatbot-with-tool-calling)