useStream()
React hook provides a seamless way to integrate LangGraph into your React applications. It handles all the complexities of streaming, state management, and branching logic, letting you focus on building great chat experiences.
Key features:
useStream()
in your React application.
The useStream()
provides a solid foundation for creating bespoke chat experiences. For pre-built chat components and interfaces, we also recommend checking out CopilotKit and assistant-ui.
useStream()
hook takes care of all the complex state management behind the scenes, providing you with simple interfaces to build your UI. Here’s what you get out of the box:
isLoading
property tells you when a stream is active, enabling you to:
useStream()
hook can automatically resume an ongoing run upon mounting by setting reconnectOnMount: true
. This is useful for continuing a stream after a page refresh, ensuring no messages and events generated during the downtime are lost.
window.sessionStorage
, which can be swapped by passing a custom storage in reconnectOnMount
instead. The storage is used to persist the in-flight run ID for a thread (under lg:stream:${threadId}
key).
joinStream
function to resume the stream. Make sure to pass streamResumable: true
when creating the run; otherwise some events might be lost.
threadId
in your URL’s query parameters to let users resume conversations after page refreshes.
useStream()
hook will keep track of the message chunks received from the server and concatenate them together to form a complete message. The completed message chunks can be retrieved via the messages
property.
By default, the messagesKey
is set to messages
, where it will append the new messages chunks to values["messages"]
. If you store messages in a different key, you can change the value of messagesKey
.
useStream()
hook will use the streamMode: "messages-tuple"
to receive a stream of messages (i.e. individual LLM tokens) from any LangChain chat model invocations inside your graph nodes. Learn more about messages streaming in the streaming guide.
useStream()
hook exposes the interrupt
property, which will be filled with the last interrupt from the thread. You can use interrupts to:
getMessagesMetadata()
to get the first checkpoint from which the message has been first seen. You can then create a new run from the checkpoint preceding the first seen checkpoint to create a new branch in a thread.
A branch can be created in following ways:
experimental_branchTree
property to get the tree representation of the thread, which can be used to render branching controls for non-message based graphs.
initialValues
option to display cached thread data immediately while the history is being loaded from the server. This improves user experience by showing cached data instantly when navigating to existing threads.
threadId
option in submit
function to enable optimistic UI patterns where you need to know the thread ID before the thread is actually created.
useStream()
hook is friendly for apps written in TypeScript and you can specify types for the state to get better type safety and IDE support.
ConfigurableType
: Type for the config.configurable
property (default: Record<string, unknown>
)InterruptType
: Type for the interrupt value - i.e. contents of interrupt(...)
function (default: unknown
)CustomEventType
: Type for the custom events (default: unknown
)UpdateType
: Type for the submit function (default: Partial<State>
)import type { ... }
directive).
useStream()
hook provides several callback options to help you respond to different events:
onError
: Called when an error occurs.onFinish
: Called when the stream is finished.onUpdateEvent
: Called when an update event is received.onCustomEvent
: Called when a custom event is received. See the streaming guide to learn how to stream custom events.onMetadataEvent
: Called when a metadata event is received, which contains the Run ID and Thread ID.