Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Hook

useChat

useChat is a React hook for building conversational interfaces. It streams provider responses, maintains chat state, and updates the UI as messages arrive.

Since
5.0

How it works

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.

Syntax

useChat(options?)

Returns

Returns chat state and utility functions, including messages and sendMessage; the current API also supports callbacks and transport-based request configuration.

Key points

  • Use the hook as the client-side foundation for a conversational chatbot UI.
  • Provider responses stream into the interface instead of waiting for the complete response.
  • The hook tracks messages and refreshes the UI as streamed data arrives.
  • A normal submission produces progressively available messages for immediate display.
  • Messages expose identifiers, roles, and parts, so rendering should use the message structure rather than a single text field.
  • Call sendMessage to submit a new user message to the chat endpoint.

Common mistakes

  • The hook does not provide input state management; keep the input value and updates in component state.
  • Without custom configuration, submissions target the /api/chat endpoint using POST.

Example: Basic useChat interface

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.

When to use it

Streaming chatbot UI

A web chatbot needs incremental assistant responses while the user watches the conversation.

Provider-backed chat

A client application must submit messages to an AI route and render the resulting conversation.

Interactive tool workflows

A chatbot needs client-side handling for tool calls or user confirmations.

Interview questions

QWhat problem does useChat solve?

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.

QHow do you submit and render a basic chat?

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.

QWhere does useChat send requests by default?

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.

  • useCompletion
  • useObject
  • sendMessage
  • messages
  • streamText
  • onToolCall
  • addToolOutput
  • sendAutomaticallyWhen

Source: Returns. Checked 2026-10-15.