Skip to content

API Reference

useChat<TPart, TEvent>(options): UseChatReturn<TPart>

Section titled “useChat<TPart, TEvent>(options): UseChatReturn<TPart>”

The hook accepts two optional generic parameters:

  • TPart — custom content part types. Defaults to ContentPart (TextPart | ToolCallPart).
  • TEvent — custom SSE event types. Defaults to SSEEvent (TextDeltaEvent | ToolCallEvent).
// Default — uses built-in ContentPart and SSEEvent
const chat = useChat({ api: "/chat" });
// Custom parts — extend with your own part types
const chat = useChat<ContentPart | ThinkingPart>({ api: "/chat" });
// Custom parts and events — extend both
const chat = useChat<ContentPart | ErrorPart, SSEEvent | ErrorEvent>({
api: "/chat",
onEvent: (event, helpers) => {
// event is typed as SSEEvent | ErrorEvent
},
});
OptionTypeRequiredDescription
apistringYesSSE endpoint URL
initialMessagesMessage<TPart>[]NoInitial messages to prepopulate the chat
headersRecord<string, string>NoExtra headers merged into every fetch request
bodyRecord<string, unknown>NoExtra fields merged into POST body alongside message
onEvent(event: TEvent, helpers) => voidNoCustom SSE event handler (replaces default text_delta handling)
onMessage(message) => voidNoCalled when a complete message (user or assistant) is added
onError(error) => voidNoCalled on fetch or stream error
onFinish(messages) => voidNoCalled when the stream ends
FieldTypeDescription
messagesMessage<TPart>[]Array of { id, role, parts }
isLoadingbooleantrue while streaming
errorError | nullMost recent error, or null
sendMessage(text: string) => voidSend a user message and start streaming
stop() => voidAbort the current stream
setMessagesDispatch<SetStateAction<Message<TPart>[]>>Direct state setter for programmatic control

All types are exported for use in your own code:

import type {
TextPart,
ToolCallPart,
ContentPart,
Message,
SSEEvent,
TextDeltaEvent,
ToolCallEvent,
EventHelpers,
UseChatOptions,
UseChatReturn,
} from "@devscalelabs/react-sse-chat";
interface TextPart {
type: "text";
text: string;
}
interface ToolCallPart {
type: "tool_call";
tool_name: string;
argument: string;
callId?: string; // present in agent history, absent during SSE streaming
}

Built-in content part union. Use as a base when defining custom parts:

type ContentPart = TextPart | ToolCallPart;

Generic over part type. Defaults to ContentPart:

interface Message<TPart extends { type: string } = ContentPart> {
id: string;
role: "user" | "assistant";
parts: TPart[];
}
type SSEEvent = TextDeltaEvent | ToolCallEvent;
interface TextDeltaEvent {
type: "text_delta";
delta: string;
}
interface ToolCallEvent {
type: "tool_call";
tool_name: string;
argument: string;
}

Generic over part type. Defaults to ContentPart:

interface EventHelpers<TPart extends { type: string } = ContentPart> {
appendText: (delta: string) => void;
appendPart: (part: TPart) => void;
setMessages: Dispatch<SetStateAction<Message<TPart>[]>>;
}

Generic over part type and event type. Defaults to ContentPart and SSEEvent:

interface UseChatOptions<
TPart extends { type: string } = ContentPart,
TEvent extends { type: string } = SSEEvent,
> {
api: string;
initialMessages?: Message<TPart>[];
headers?: Record<string, string>;
body?: Record<string, unknown>;
onEvent?: (event: TEvent, helpers: EventHelpers<TPart>) => void;
onFinish?: (messages: Message<TPart>[]) => void;
onMessage?: (message: Message<TPart>) => void;
onError?: (error: Error) => void;
}

Generic over part type. Defaults to ContentPart:

interface UseChatReturn<TPart extends { type: string } = ContentPart> {
messages: Message<TPart>[];
isLoading: boolean;
error: Error | null;
sendMessage: (text: string) => void;
stop: () => void;
setMessages: Dispatch<SetStateAction<Message<TPart>[]>>;
}

convertOpenAIAgentsMessages(items, options?)

Section titled “convertOpenAIAgentsMessages(items, options?)”

Converts an array of raw OpenAI Agents SDK message items into Message<OpenAIAgentsContentPart>[]. Consecutive assistant-side items are merged into a single message with multiple parts.

import { convertOpenAIAgentsMessages } from "@devscalelabs/react-sse-chat";
const messages = convertOpenAIAgentsMessages(agentData);
const messagesWithReasoning = convertOpenAIAgentsMessages(agentData, {
includeReasoning: true,
});
interface ConvertOpenAIAgentsOptions {
/** Include reasoning items as ReasoningPart. Defaults to false. */
includeReasoning?: boolean;
}
interface ReasoningPart {
type: "reasoning";
text: string;
}
interface ToolResultPart {
type: "tool_result";
callId: string;
output: string;
}

Union of all content parts the converter can produce:

type OpenAIAgentsContentPart =
| TextPart
| ToolCallPart
| ReasoningPart
| ToolResultPart;