SSE Format
Expected Format
Section titled “Expected Format”The backend should emit events as data: {JSON}\n\n:
data: {"type": "text_delta", "delta": "Hello"}data: {"type": "text_delta", "delta": " world"}data: {"type": "tool_call", "tool_name": "search", "argument": "..."}The stream can optionally end with:
data: [DONE]Event Types
Section titled “Event Types”text_delta
Section titled “text_delta”The primary event type. Each event contains a chunk of the assistant’s response:
{"type": "text_delta", "delta": "Hello"}By default, useChat appends all delta values as text parts in the assistant message’s parts array.
tool_call
Section titled “tool_call”Emitted when the AI invokes a tool:
{"type": "tool_call", "tool_name": "search_web", "argument": "{\"query\": \"...\" }"}These are not handled by default. Use the onEvent callback to process them — for example, by calling helpers.appendPart() to add a tool_call part to the message.
HTTP Request
Section titled “HTTP Request”useChat sends a POST request with:
- Content-Type:
application/json - Body:
{ "message": "user text", ...body }(wherebodyis the extra fields you passed touseChat)
The response must be a streaming response with Content-Type: text/event-stream.
How Streaming Works
Section titled “How Streaming Works”Under the hood, useChat uses native fetch with ReadableStream:
- A
POSTrequest is sent to theapiURL - The response body is read as a stream
- Each
data:line is parsed as JSON - Events are dispatched to the event handler (default or custom
onEvent) - The stream ends when the connection closes or
data: [DONE]is received