Streaming chatbot UI
A web chatbot needs incremental assistant responses while the user watches the conversation.
AI Tools · Hook
useChat is a React hook for building conversational interfaces. It streams provider responses, maintains chat state, and updates the UI as messages arrive.
useChat provides client-side chat state and streaming behavior. In the current API, a transport handles communication with the provider, while the component owns input state and calls sendMessage to submit user text.
useChat(options?)Returns chat state and utility functions, including messages and sendMessage; the current API also supports callbacks and transport-based request configuration.
import { useChat } from '@ai-sdk/react';
import { useState } from 'react';
export default function Chat() {
const [input, setInput] = useState('');
const { messages, sendMessage } = useChat();
return (
<form onSubmit={event => {
event.preventDefault();
sendMessage({ text: input });
setInput('');
}}>
{messages.map(message => (
<div key={message.id}>
{message.parts.map((part, index) =>
part.type === 'text' ? <span key={index}>{part.text}</span> : null
)}
</div>
))}
<input value={input} onChange={event => setInput(event.target.value)} />
<button type="submit">Send</button>
</form>
);
}Creates a streamed chat interface while keeping input state in the component.
A web chatbot needs incremental assistant responses while the user watches the conversation.
A client application must submit messages to an AI route and render the resulting conversation.
A chatbot needs client-side handling for tool calls or user confirmations.
useChat is a UI hook for conversational interfaces. It streams messages from the AI provider, maintains chat state, and updates the interface as new messages arrive.
Follow-up: How does the current API differ from older versions? It uses a transport-based architecture and leaves input-state management to the application.
The hook exposes the current messages and a sendMessage function. The application renders message parts and invokes sendMessage when the user submits text.
Follow-up: Why render parts instead of assuming every message is plain text? Message parts can represent structured content, including tool invocations and results.
The default request is an HTTP POST to /api/chat, with the message list sent as the request body. Request configuration can be customized when the application needs different headers, body data, credentials, or transport behavior.
Follow-up: How can client-side tools participate in the flow? Handle automatic client-side tool calls with onToolCall and provide their results with addToolOutput.
useCompletionuseObjectsendMessagemessagesstreamTextonToolCalladdToolOutputsendAutomaticallyWhen