[{"id":"/docs/reference/ai-sdk-ui/use-chat","type":"page","content":"useChat","contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}}],"breadcrumbs":["Docs","Reference","AI SDK UI"],"url":"/docs/reference/ai-sdk-ui/use-chat"},{"id":"/docs/reference/ai-sdk-ui/use-chat-0","content":"API reference for the useChat hook.","contentWithHighlights":[{"type":"text","content":"API reference for the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook."}],"type":"text","url":"/docs/reference/ai-sdk-ui/use-chat"},{"id":"/docs/reference/ai-sdk-ui/use-chat-8","content":"The useChat API has been significantly updated in AI SDK 5.0. It now uses a\ntransport-based architecture and no longer manages input state internally. See\nthe migration\nguide for\ndetails.","contentWithHighlights":[{"type":"text","content":"The "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" API has been significantly updated in AI SDK 5.0. It now uses a\ntransport-based architecture and no longer manages input state internally. See\nthe migration\nguide for\ndetails."}],"type":"text","url":"/docs/reference/ai-sdk-ui/use-chat"},{"type":"page","content":"useChat No Response","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" No Response"}],"id":"/docs/troubleshooting/use-chat-tools-no-response","url":"/docs/troubleshooting/use-chat-tools-no-response"},{"id":"/docs/troubleshooting/use-chat-tools-no-response-3","content":"I am using useChat.\nWhen I log the incoming messages on the server, I can see the tool call and the tool result, but the model does not respond with anything.","contentWithHighlights":[{"type":"text","content":"I am using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":".\nWhen I log the incoming messages on the server, I can see the tool call and the tool result, but the model does not respond with anything."}],"type":"text","url":"/docs/troubleshooting/use-chat-tools-no-response#issue"},{"type":"page","content":"useChat \"An error occurred\"","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" \"An error occurred\""}],"id":"/docs/troubleshooting/use-chat-an-error-occurred","url":"/docs/troubleshooting/use-chat-an-error-occurred"},{"id":"/docs/troubleshooting/use-chat-an-error-occurred-0","content":"Troubleshooting errors related to the \"An error occurred\" error in useChat.","contentWithHighlights":[{"type":"text","content":"Troubleshooting errors related to the \"An error occurred\" error in "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"."}],"type":"text","url":"/docs/troubleshooting/use-chat-an-error-occurred"},{"id":"/docs/troubleshooting/use-chat-an-error-occurred-4","content":"I am using useChat and I get the error \"An error occurred\".","contentWithHighlights":[{"type":"text","content":"I am using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" and I get the error \"An error occurred\"."}],"type":"text","url":"/docs/troubleshooting/use-chat-an-error-occurred#issue"},{"type":"page","content":"Repeated assistant messages in useChat","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"Repeated assistant messages in "},{"type":"text","content":"useChat","styles":{"highlight":true}}],"id":"/docs/troubleshooting/repeated-assistant-messages","url":"/docs/troubleshooting/repeated-assistant-messages"},{"id":"/docs/troubleshooting/repeated-assistant-messages-0","content":"Troubleshooting duplicate assistant messages when using useChat with streamText","contentWithHighlights":[{"type":"text","content":"Troubleshooting duplicate assistant messages when using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" with streamText"}],"type":"text","url":"/docs/troubleshooting/repeated-assistant-messages"},{"id":"/docs/troubleshooting/repeated-assistant-messages-4","content":"When using useChat with streamText on the server, the assistant's messages appear duplicated in the UI - showing both the previous message and the new message, or showing the same message multiple times. This can occur when using tool calls or complex message flows.","contentWithHighlights":[{"type":"text","content":"When using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" with streamText on the server, the assistant's messages appear duplicated in the UI - showing both the previous message and the new message, or showing the same message multiple times. This can occur when using tool calls or complex message flows."}],"type":"text","url":"/docs/troubleshooting/repeated-assistant-messages#issue"},{"type":"page","content":"Stale body values with useChat","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"Stale body values with "},{"type":"text","content":"useChat","styles":{"highlight":true}}],"id":"/docs/troubleshooting/use-chat-stale-body-data","url":"/docs/troubleshooting/use-chat-stale-body-data"},{"id":"/docs/troubleshooting/use-chat-stale-body-data-0","content":"Troubleshooting stale values when passing information via the body parameter of useChat","contentWithHighlights":[{"type":"text","content":"Troubleshooting stale values when passing information via the body parameter of "},{"type":"text","content":"useChat","styles":{"highlight":true}}],"type":"text","url":"/docs/troubleshooting/use-chat-stale-body-data"},{"id":"/docs/troubleshooting/use-chat-stale-body-data-6","content":"When using useChat and passing dynamic information via the body parameter at the hook level, the data remains stale and only reflects the value from the initial component render. This occurs because the body configuration is captured once when the hook is initialized and doesn't update with subsequent component re-renders.","contentWithHighlights":[{"type":"text","content":"When using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" and passing dynamic information via the body parameter at the hook level, the data remains stale and only reflects the value from the initial component render. This occurs because the body configuration is captured once when the hook is initialized and doesn't update with subsequent component re-renders."}],"type":"text","url":"/docs/troubleshooting/use-chat-stale-body-data#issue"},{"type":"page","content":"Send Custom Body from useChat","breadcrumbs":["Docs","Next.js"],"contentWithHighlights":[{"type":"text","content":"Send Custom Body from "},{"type":"text","content":"useChat","styles":{"highlight":true}}],"id":"/cookbook/next/send-custom-body-from-use-chat","url":"/cookbook/next/send-custom-body-from-use-chat"},{"id":"/cookbook/next/send-custom-body-from-use-chat-0","content":"Learn how to send a custom body from the useChat hook using the AI SDK and Next.js","contentWithHighlights":[{"type":"text","content":"Learn how to send a custom body from the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook using the AI SDK and Next.js"}],"type":"text","url":"/cookbook/next/send-custom-body-from-use-chat"},{"id":"/cookbook/next/send-custom-body-from-use-chat-5","content":"By default, useChat sends all messages as well as information from the request to the server.\nHowever, it is often desirable to control the entire body content that is sent to the server, e.g. to:","contentWithHighlights":[{"type":"text","content":"By default, "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" sends all messages as well as information from the request to the server.\nHowever, it is often desirable to control the entire body content that is sent to the server, e.g. to:"}],"type":"text","url":"/cookbook/next/send-custom-body-from-use-chat"},{"type":"page","content":"Share useChat State Across Components","breadcrumbs":["Docs","Next.js"],"contentWithHighlights":[{"type":"text","content":"Share "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" State Across Components"}],"id":"/cookbook/next/use-shared-chat-context","url":"/cookbook/next/use-shared-chat-context"},{"id":"/cookbook/next/use-shared-chat-context-0","content":"Learn how to share a chat instance across multiple components with useChat and easily reset the chat.","contentWithHighlights":[{"type":"text","content":"Learn how to share a chat instance across multiple components with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" and easily reset the chat."}],"type":"text","url":"/cookbook/next/use-shared-chat-context"},{"type":"page","content":"useChat Failed to Parse Stream","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" Failed to Parse Stream"}],"id":"/docs/troubleshooting/use-chat-failed-to-parse-stream","url":"/docs/troubleshooting/use-chat-failed-to-parse-stream"},{"id":"/docs/troubleshooting/use-chat-failed-to-parse-stream-6","content":"You can switch useChat and useCompletion to raw text stream processing with the streamProtocol parameter.\nSet it to text as follows:","contentWithHighlights":[{"type":"text","content":"You can switch "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" and useCompletion to raw text stream processing with the streamProtocol parameter.\nSet it to text as follows:"}],"type":"text","url":"/docs/troubleshooting/use-chat-failed-to-parse-stream#solution"},{"id":"/docs/troubleshooting/use-chat-failed-to-parse-stream-4","content":"I am using useChat or useCompletion, and I am getting a \"Failed to parse stream string. Invalid code\" error. I am using version 3.0.20 or newer of the AI SDK.","contentWithHighlights":[{"type":"text","content":"I am using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" or useCompletion, and I am getting a \"Failed to parse stream string. Invalid code\" error. I am using version 3.0.20 or newer of the AI SDK."}],"type":"text","url":"/docs/troubleshooting/use-chat-failed-to-parse-stream#issue"},{"type":"page","content":"Custom headers, body, and credentials not working with useChat","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"Custom headers, body, and credentials not working with "},{"type":"text","content":"useChat","styles":{"highlight":true}}],"id":"/docs/troubleshooting/use-chat-custom-request-options","url":"/docs/troubleshooting/use-chat-custom-request-options"},{"id":"/docs/troubleshooting/use-chat-custom-request-options-0","content":"Troubleshooting errors related to custom request configuration in useChat hook","contentWithHighlights":[{"type":"text","content":"Troubleshooting errors related to custom request configuration in "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook"}],"type":"text","url":"/docs/troubleshooting/use-chat-custom-request-options"},{"id":"/docs/troubleshooting/use-chat-custom-request-options-10","content":"There are three ways to properly configure request options with useChat:","contentWithHighlights":[{"type":"text","content":"There are three ways to properly configure request options with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":":"}],"type":"text","url":"/docs/troubleshooting/use-chat-custom-request-options#solution"},{"type":"page","content":"UI","breadcrumbs":["Docs","AI SDK Harnesses"],"contentWithHighlights":[{"type":"text","content":"UI"}],"id":"/docs/ai-sdk-harnesses/ui","url":"/docs/ai-sdk-harnesses/ui"},{"id":"/docs/ai-sdk-harnesses/ui-0","content":"Use AI SDK harnesses with useChat.","contentWithHighlights":[{"type":"text","content":"Use AI SDK harnesses with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"."}],"type":"text","url":"/docs/ai-sdk-harnesses/ui"},{"id":"/docs/ai-sdk-harnesses/ui-25","content":"Then use useChat<HarnessMessage>() on the client.","contentWithHighlights":[{"type":"text","content":"Then use "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"<HarnessMessage>() on the client."}],"type":"text","url":"/docs/ai-sdk-harnesses/ui#type-safe-tool-parts"},{"type":"page","content":"Tool Invocation Missing Result Error","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"Tool Invocation Missing Result Error"}],"id":"/docs/troubleshooting/tool-invocation-missing-result","url":"/docs/troubleshooting/tool-invocation-missing-result"},{"id":"/docs/troubleshooting/tool-invocation-missing-result-5","content":"The error occurs when you define a tool without an execute function and don't provide the result through other means (like useChat's onToolCall or addToolOutput functions).","contentWithHighlights":[{"type":"text","content":"The error occurs when you define a tool without an execute function and don't provide the result through other means (like "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"'s onToolCall or addToolOutput functions)."}],"type":"text","url":"/docs/troubleshooting/tool-invocation-missing-result#cause"},{"id":"/docs/troubleshooting/tool-invocation-missing-result-9","content":"For client-side execution with useChat (omitting the execute function), you must provide results using addToolOutput:","contentWithHighlights":[{"type":"text","content":"For client-side execution with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" (omitting the execute function), you must provide results using addToolOutput:"}],"type":"text","url":"/docs/troubleshooting/tool-invocation-missing-result#solution"},{"type":"page","content":"Streaming Status Shows But No Text Appears","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"Streaming Status Shows But No Text Appears"}],"id":"/docs/troubleshooting/streaming-status-delay","url":"/docs/troubleshooting/streaming-status-delay"},{"id":"/docs/troubleshooting/streaming-status-delay-0","content":"Why useChat shows \"streaming\" status without any visible content","contentWithHighlights":[{"type":"text","content":"Why "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" shows \"streaming\" status without any visible content"}],"type":"text","url":"/docs/troubleshooting/streaming-status-delay"},{"id":"/docs/troubleshooting/streaming-status-delay-5","content":"When using useChat, the status changes to \"streaming\" immediately, but no text appears for several seconds.","contentWithHighlights":[{"type":"text","content":"When using "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":", the status changes to \"streaming\" immediately, but no text appears for several seconds."}],"type":"text","url":"/docs/troubleshooting/streaming-status-delay#issue"},{"type":"page","content":"useChat/useCompletion stream output contains 0:... instead of text","breadcrumbs":["Docs","Troubleshooting"],"contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"/useCompletion stream output contains 0:... instead of text"}],"id":"/docs/troubleshooting/strange-stream-output","url":"/docs/troubleshooting/strange-stream-output"},{"type":"page","content":"Chatbot","breadcrumbs":["Docs","AI SDK UI"],"contentWithHighlights":[{"type":"text","content":"Chatbot"}],"id":"/docs/ai-sdk-ui/chatbot","url":"/docs/ai-sdk-ui/chatbot"},{"id":"/docs/ai-sdk-ui/chatbot-0","content":"Learn how to use the useChat hook.","contentWithHighlights":[{"type":"text","content":"Learn how to use the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook."}],"type":"text","url":"/docs/ai-sdk-ui/chatbot"},{"id":"/docs/ai-sdk-ui/chatbot-35","content":"To summarize, the useChat hook provides the following features:","contentWithHighlights":[{"type":"text","content":"To summarize, the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook provides the following features:"}],"type":"text","url":"/docs/ai-sdk-ui/chatbot"},{"type":"page","content":"Transport","breadcrumbs":["Docs","AI SDK UI"],"contentWithHighlights":[{"type":"text","content":"Transport"}],"id":"/docs/ai-sdk-ui/transport","url":"/docs/ai-sdk-ui/transport"},{"id":"/docs/ai-sdk-ui/transport-0","content":"Learn how to use custom transports with useChat.","contentWithHighlights":[{"type":"text","content":"Learn how to use custom transports with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"."}],"type":"text","url":"/docs/ai-sdk-ui/transport"},{"id":"/docs/ai-sdk-ui/transport-13","content":"By default, useChat uses HTTP POST requests to send messages to /api/chat:","contentWithHighlights":[{"type":"text","content":"By default, "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" uses HTTP POST requests to send messages to /api/chat:"}],"type":"text","url":"/docs/ai-sdk-ui/transport#default-transport"},{"type":"page","content":"convertToModelMessages","breadcrumbs":["Docs","Reference","AI SDK UI"],"contentWithHighlights":[{"type":"text","content":"convertToModelMessages"}],"id":"/docs/reference/ai-sdk-ui/convert-to-model-messages","url":"/docs/reference/ai-sdk-ui/convert-to-model-messages"},{"id":"/docs/reference/ai-sdk-ui/convert-to-model-messages-0","content":"Convert useChat messages to ModelMessages for AI functions (API Reference)","contentWithHighlights":[{"type":"text","content":"Convert "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" messages to ModelMessages for AI functions (API Reference)"}],"type":"text","url":"/docs/reference/ai-sdk-ui/convert-to-model-messages"},{"id":"/docs/reference/ai-sdk-ui/convert-to-model-messages-12","content":"The convertToModelMessages function is used to transform an array of UI messages from the useChat hook into an array of ModelMessage objects. These ModelMessage objects are compatible with AI core functions like streamText.","contentWithHighlights":[{"type":"text","content":"The convertToModelMessages function is used to transform an array of UI messages from the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook into an array of ModelMessage objects. These ModelMessage objects are compatible with AI core functions like streamText."}],"type":"text","url":"/docs/reference/ai-sdk-ui/convert-to-model-messages"},{"type":"page","content":"Chatbot Tool Usage","breadcrumbs":["Docs","AI SDK UI"],"contentWithHighlights":[{"type":"text","content":"Chatbot Tool Usage"}],"id":"/docs/ai-sdk-ui/chatbot-tool-usage","url":"/docs/ai-sdk-ui/chatbot-tool-usage"},{"id":"/docs/ai-sdk-ui/chatbot-tool-usage-0","content":"Learn how to use tools with the useChat hook.","contentWithHighlights":[{"type":"text","content":"Learn how to use tools with the "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook."}],"type":"text","url":"/docs/ai-sdk-ui/chatbot-tool-usage"},{"id":"/docs/ai-sdk-ui/chatbot-tool-usage-18","content":"With useChat and streamText, you can use tools in your chatbot application.\nThe AI SDK supports three tool execution patterns in this context:","contentWithHighlights":[{"type":"text","content":"With "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" and streamText, you can use tools in your chatbot application.\nThe AI SDK supports three tool execution patterns in this context:"}],"type":"text","url":"/docs/ai-sdk-ui/chatbot-tool-usage"},{"type":"page","content":"Migrate AI SDK 3.2 to 3.3","breadcrumbs":["Docs","Migration Guides"],"contentWithHighlights":[{"type":"text","content":"Migrate AI SDK 3.2 to 3.3"}],"id":"/docs/migration-guides/migration-guide-3-3","url":"/docs/migration-guides/migration-guide-3-3"},{"id":"/docs/migration-guides/migration-guide-3-3-16","content":"Fix useChat's reload function, now correctly sending data, body, and headers.","contentWithHighlights":[{"type":"text","content":"Fix "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"'s reload function, now correctly sending data, body, and headers."}],"type":"text","url":"/docs/migration-guides/migration-guide-3-3#ai-sdk-ui-improvements"},{"id":"/docs/migration-guides/migration-guide-3-3-34","content":"Svelte: Added tool calling support to useChat.","contentWithHighlights":[{"type":"text","content":"Svelte: Added tool calling support to "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"."}],"type":"text","url":"/docs/migration-guides/migration-guide-3-3#ui-framework-support-expansion"},{"type":"page","content":"InferUITools","breadcrumbs":["Docs","Reference","AI SDK UI"],"contentWithHighlights":[{"type":"text","content":"InferUITools"}],"id":"/docs/reference/ai-sdk-ui/infer-ui-tools","url":"/docs/reference/ai-sdk-ui/infer-ui-tools"},{"id":"/docs/reference/ai-sdk-ui/infer-ui-tools-13","content":"useChat - Chat hook that supports typed tools","contentWithHighlights":[{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" - Chat hook that supports typed tools"}],"type":"text","url":"/docs/reference/ai-sdk-ui/infer-ui-tools#related"},{"type":"page","content":"Stream Text with Image Prompt","breadcrumbs":["Docs","Next.js"],"contentWithHighlights":[{"type":"text","content":"Stream Text with Image Prompt"}],"id":"/cookbook/next/stream-text-with-image-prompt","url":"/cookbook/next/stream-text-with-image-prompt"},{"id":"/cookbook/next/stream-text-with-image-prompt-6","content":"On the client side, we use the new useChat hook and send multimodal messages using the parts array.","contentWithHighlights":[{"type":"text","content":"On the client side, we use the new "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":" hook and send multimodal messages using the parts array."}],"type":"text","url":"/cookbook/next/stream-text-with-image-prompt#client"},{"id":"/cookbook/next/stream-text-with-image-prompt-4","content":"Vision models such as GPT-6 Astra can process both text and images. In this example, we will show you how to send an image URL along with the user's message to the model with useChat.","contentWithHighlights":[{"type":"text","content":"Vision models such as GPT-6 Astra can process both text and images. In this example, we will show you how to send an image URL along with the user's message to the model with "},{"type":"text","content":"useChat","styles":{"highlight":true}},{"type":"text","content":"."}],"type":"text","url":"/cookbook/next/stream-text-with-image-prompt"}]