LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
  • Stream
  • Overview
  • Getting started
  • useStream
  • Selectors
  • Interrupts & headless tools
  • Subagents & subgraphs
  • Fork & edit from a checkpoint
  • Submission queue
  • Multimodal media
  • Transports
  • Suspense
  • StreamProvider & context
  • Type safety
  • Migrating to v1
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

Chat LangChain

What can I help with?

Enter to send·Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemoteStream
OverviewGetting starteduseStreamSelectorsInterrupts & headless toolsSubagents & subgraphsFork & edit from a checkpointSubmission queueMultimodal mediaTransportsSuspenseStreamProvider & contextType safetyMigrating to v1
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
LangGraph Checkpoint Redis
ShallowStore
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
Cli
LangGraph API
LangGraph CLI
LangGraph CUA
Utils
LangGraph Supervisor
LangGraph Swarm
Language
Theme
JavaScript@langchain/react

@langchain/react

Description

@langchain/react

React SDK for building AI-powered applications with Deep Agents, LangChain and LangGraph.

@langchain/react v1 ships a v2-native useStream hook together with a small family of companion selector hooks. The root hook gives you always-on access to thread state, messages, tool calls, and interrupts; the selector hooks open ref-counted subscriptions for the things that aren't needed on every view (per-subagent messages, media streams, submission queue, message metadata, raw channels, …).

Highlights

  • v2-native streaming protocol. Session-based transport with automatic re-attach on remount; no more reconnectOnMount / joinStream dance.
  • Selector-based subscriptions. Namespaced data (subagents, subgraphs, media) streams only when a component actually mounts the matching selector hook, and releases on unmount.
  • Always-on root projections. values, messages, toolCalls, and interrupts are live at the root with zero per-subscription cost.
  • Agent-brand type inference. useStream<typeof agent>() unwraps state, tool calls, and subagent state maps from an agent brand.
  • Discriminated options. The hosted Agent Server path and the custom-adapter path are two arms of a single typed union — mixing them is a compile-time error.
  • Multimodal media streams. Built-in assembly for audio, images, video, and files.
  • Suspense integration. useSuspenseStream hands the initial hydration phase to <Suspense> and non-streaming errors to Error Boundaries.

Installation

npm install @langchain/react @langchain/core

Peer dependencies: react (^18 || ^19), @langchain/core (^1.1.27).

Quick Start

import { useStream } from "@langchain/react";

function Chat() {
  const { messages, submit, isLoading } = useStream({
    assistantId: "agent",
    apiUrl: "http://localhost:2024",
  });

  return (
    <div>
      {messages.map((msg, i) => (
        <div key={msg.id ?? i}>{String(msg.content)}</div>
      ))}

      <button
        disabled={isLoading}
        onClick={() =>
          void submit({
            messages: [{ type: "human", content: "Hello!" }],
          })
        }
      >
        Send
      </button>
    </div>
  );
}

Mental model

@langchain/react v1 splits the surface into two layers:

  1. Root hook (useStream). Owns the thread lifecycle, the transport, and a handful of always-on projections (values, messages, toolCalls, interrupts, error, isLoading, discovery maps). Mount it once per thread.
  2. Companion selector hooks. Each one opens a ref-counted subscription when the first component mounts it and releases it when the last consumer unmounts. Use them for anything scoped to a namespace, a subagent / subgraph, a specific message, a specific extension channel, or a media stream.
import {
  useStream,
  useMessages,
  useToolCalls,
  useSubmissionQueue,
} from "@langchain/react";

function Chat() {
  const stream = useStream({ assistantId: "agent", apiUrl: "/api" });

  // Root: free reads, no new subscription.
  const messages = useMessages(stream); // same as stream.messages

  // Scoped: opens a namespaced subscription on mount.
  const queue = useSubmissionQueue(stream);
}

Documentation

Detailed guides live in ./docs. Start with the two files most apps need first:

  • useStream — options, return shape, submit(), stop(), respond(), hydrationPromise.
  • Companion selector hooks — useValues, useMessages, useToolCalls, useMessageMetadata, useChannel, useExtension, and friends.

Feature-specific guides:

  • Transports — SSE, WebSocket, HttpAgentServerAdapter, custom AgentServerAdapter.
  • Custom transports — implementing AgentServerAdapter against your own backend, with a worked walkthrough of examples/ui-react-transport.
  • Interrupts & headless tools — pausing runs, respond(), tools + onTool.
  • Fork / edit from a checkpoint — useMessageMetadata + submit({ forkFrom }).
  • Submission queue — multitaskStrategy: "enqueue" + useSubmissionQueue.
  • Subagents & subgraphs — discovery maps, scoped selector subscriptions.
  • Multimodal media — useAudio / useImages / useVideo / useFiles, useMediaURL, players.
  • useSuspenseStream — Suspense + Error Boundary integration.
  • StreamProvider / useStreamContext — share one stream across a subtree.
  • Type safety — agent-brand inference, prop-drilling, type helpers.

Migrating from v0 to v1

The useStream import name is unchanged, but the return shape, option bag, and protocol semantics all shifted. Most chat apps migrate in well under an hour — the full migration guide with line-by-line diffs lives in ./docs/v1-migration.md.

Legacy type aliases (UseStream, UseSuspenseStream, UseStreamOptions, UseStreamTransport, QueueEntry, GetToolCallsType, SubagentStream, …) and the legacy FetchStreamTransport class are no longer re-exported from @langchain/react. Apps still on the legacy surface can import directly from @langchain/langgraph-sdk/ui during their migration.

Playground

For complete end-to-end examples with full agentic UIs, visit the LangChain UI Playground.

License

MIT

Classes

Class

HttpAgentServerAdapter

Public v1 name for TransportAdapter plus optional high-level capabilities. Renamed to reflect that this interface now denotes the full agent-server protocol contract (not merely wire transport): any o

Class

MediaAssemblyError

Typed error thrown through media.stream / rejected from media.blob / media.objectURL when a handle fails before its message completes. Carries the bytes accumulated up to the failure point on `p

Functions

Function

applyHeadlessToolResumeCommand

Resume a headless-tool batch on the v1 commands transport.

headlessToolsBatchResumeCommand still returns a legacy { resume } command shape for callers on the old runs/stream API. On v1 `StreamContr

Function

executeHeadlessTool

Function

filterOutHeadlessToolInterrupts

Strip headless-tool interrupts from a user-facing interrupt list.

Function

findHeadlessTool

Function

flushPendingHeadlessToolInterrupts

Execute and resume all newly seen headless-tool interrupts from a values payload. Callers own handledIds and should clear it when the thread changes.

Function

handleHeadlessToolInterrupt

Function

headlessToolResumeCommand

Function

isHeadlessToolInterrupt

Function

parseHeadlessToolInterruptPayload

Parses a headless-tool interrupt value from the graph. Accepts both toolCall (LangChain JS) and tool_call (Python / JSON snake_case).

Function

StreamProvider

Provides a shared useStream instance to all descendants via React Context.

Use StreamProvider when multiple components in a subtree need access to the same stream state (messages, loading status, e

Function

useAudio

Subscribe to a scoped audio-media stream. Returns an array of AudioMedia handles, one per message containing at least one AudioBlock in the target namespace.

Each handle is yielded on its first mat

Function

useAudioPlayer

Progressive audio playback for AudioMedia handles with a uniform surface across PCM (streamed) and container (HTMLAudioElement) strategies.

Behaviour

  • Strategy selection is derived from `medi
Function

useChannel

Raw-events escape hatch. Subscribes to one or more channels at a namespace and returns a bounded buffer of raw protocol events.

The buffer keeps accumulating across serial runs for the lifetime of th

Function

useChannelEffect

Side-effect counterpart to useChannel. Instead of returning a buffer of events that re-renders the component, it invokes onEvent once per event for as long as the hook is mounted — the idiomatic pla

Function

useExtension

Subscribe to a custom:<name> stream extension — the most-recent payload emitted by the transformer, scoped to the target namespace.

Returns only the latest value and resumes across serial runs, so

Function

useFiles

Subscribe to a scoped file-media stream. See useAudio for shared semantics; pair with useMediaURL for an <a download href> target.

Function

useImages

Subscribe to a scoped image-media stream. See useAudio for shared semantics; pair with useMediaURL for <img src>.

Function

useMediaURL

Resolve the lazy MediaBase.objectURL promise into a string suitable for direct use in <audio src> / <img src> / <video src> / <a href download>. Returns undefined until the URL is available.

Function

useMessageMetadata

Read metadata recorded for a specific message id — today exposes parentCheckpointId, the checkpoint the message was first seen on. Designed for fork / edit flows:

const { parentCheckpointId
Function

useMessages

Subscribe to a scoped messages stream. Pass stream and optionally a subagent/subgraph snapshot (or any namespaced target).

Contract:

  • At the root (no target) this returns stream.messages dire
Function

useProjection

React-side primitive that composes ChannelRegistry.acquire with useSyncExternalStore.

Contract:

  • On mount (or when key changes) this hook acquires a ref-counted projection from the registry an
Function

useStream

React binding for the v2-native stream runtime.

useStream exposes three always-on projections (values / messages / toolCalls) at the thread root plus cheap discovery maps for subagents / subg

Function

useStreamContext

Accesses the shared stream instance from the nearest StreamProvider. Throws if called outside of one.

Function

useSubmissionQueue

Function

useSuspenseStream

Function

useToolCalls

Subscribe to a scoped tools (tool-call) stream. Same target and lifecycle rules as useMessages; at the root this just returns stream.toolCalls.

The optional generic T can be passed to narrow th

Function

useValues

Subscribe to a scoped values stream — most-recent state payload for a namespace. Equivalent to reading stream.values at the root.

When the payload carries a messages array, it is coerced to `Ba

Function

useVideo

Subscribe to a scoped video-media stream. See useAudio for shared semantics; pair with useMediaURL for <video src>.

Function

useVideoPlayer

Bind a VideoMedia handle to a caller-owned <video> element.

Contract

  • The caller renders <video ref={videoRef}> and styles it however they like; the hook never injects DOM nor overrides l

Interfaces

Interface

AgentServerAdapter

Public v1 name for TransportAdapter plus optional high-level capabilities. Renamed to reflect that this interface now denotes the full agent-server protocol contract (not merely wire transport): any o

Interface

AssembledToolCall

Reactive tool handle for framework bindings (stream.toolCalls, useToolCalls, injectToolCalls).

status, error, and output are plain values that the assembler updates in place as tool events arri

Interface

AudioMedia

Shared surface across every media handle returned by MediaAssembler.

The handle is live while its parent message is active:

  • partialBytes is a snapshot of all bytes received so far.
  • stream i
Interface

AudioPlayerHandle

Player controls + live state returned by useAudioPlayer.

Shape is shared with useVideoPlayer where possible — learn one surface, use it for both.

Interface

FileMedia

Shared surface across every media handle returned by MediaAssembler.

The handle is live while its parent message is active:

  • partialBytes is a snapshot of all bytes received so far.
  • stream i
Interface

FlushPendingHeadlessToolInterruptsOptions

Interface

HeadlessToolImplementation

Client-side implementation returned by headlessTool.implement(...).

Interface

HeadlessToolInterrupt

Represents a headless tool interrupt payload emitted by LangChain's schema-only tool({ ... }) overload.

Servers may serialize the nested tool call as toolCall (JS) or tool_call (Python). Use pa

Interface

HttpAgentServerAdapterOptions

Interface

ImageMedia

Shared surface across every media handle returned by MediaAssembler.

The handle is live while its parent message is active:

  • partialBytes is a snapshot of all bytes received so far.
  • stream i
Interface

MediaBase

Shared surface across every media handle returned by MediaAssembler.

The handle is live while its parent message is active:

  • partialBytes is a snapshot of all bytes received so far.
  • stream i
Interface

MessageMetadata

Metadata tracked per message id. Surfaced to applications via useMessageMetadata(stream, messageId).

Interface

StreamStopOptions

Options for StreamController.stop / framework stop().

Interface

StreamSubmitOptions

Interface

SubagentDiscoverySnapshot

Lightweight discovery record for a subagent running inside the thread.

Populated eagerly from the root subscription (tools + lifecycle channels). Content projections (messages, toolCalls, cus

Interface

SubgraphDiscoverySnapshot

Lightweight discovery record for a subgraph running inside the thread.

Interface

SubmissionQueueEntry

A single queued submission.

Interface

ToolEvent

Interface

UseAudioPlayerOptions

Options for useAudioPlayer.

All fields optional; defaults cover OpenAI gpt-4o-audio-preview pcm16 streams (sampleRate: 24000, channels: 1) and any WAV stream the upstream model emits.

Interface

UseChannelEffectOptions

Options for useChannelEffect. Extends the projection options (bufferSize, replay) with the per-event callback, an optional error sink, a target scope, and an enabled gate.

Interface

UseStreamReturn

Interface

UseSubmissionQueueReturn

Reactive handle on the server-side submission queue.

Populated when submit() is invoked with multitaskStrategy: "enqueue" while another run is in flight. The returned object is stable per snapsho

Interface

UseVideoPlayerOptions

Options for useVideoPlayer.

Interface

VideoMedia

Shared surface across every media handle returned by MediaAssembler.

The handle is live while its parent message is active:

  • partialBytes is a snapshot of all bytes received so far.
  • stream i
Interface

VideoPlayerHandle

Controls + live state returned by useVideoPlayer. Mirrors AudioPlayerHandle on the shared subset so callers only ever learn one shape.

Types

Type

AgentServerOptions

Type

AnyHeadlessToolImplementation

Type

AnyMediaHandle

Type

AnyStream

Erased stream handle useful as a parameter type for helpers and wrapper components that pass a stream through to selector hooks (useMessages, useChannel, …) without reading values directly.

A

Type

CustomAdapterOptions

Type

DefaultToolCall

Default tool call type when no specific tool definitions are provided.

Type

InferStateType

Unwrap the state shape from a compiled graph, a create-agent brand, or a plain type. Used by useStream<T>() to resolve T = typeof agent into the state the values/messages projections observe.

Type

InferSubagentStates

Infer the subagent → state map from a DeepAgent brand. Non-brands collapse to DefaultSubagentStates.

Type

InferToolCalls

Infer the discriminated union of AssembledToolCall handles from an agent brand, an array of LangChain tools, or fall back to the untyped default handle.

Pass typeof agent or typeof tools and narr

Type

InferToolOutput

Infer the successful return type of a LangChain tool.

Resolution order mirrors InferToolInput: func, then invoke, then _call, so inference survives cross-package boundaries where _call is `pr

Type

MediaAssemblyErrorKind

Kinds of failure that can terminate a media handle prematurely.

  • "message-error" — the upstream message emitted an error event before completion.
  • "stream-closed" — the transport stream clo
Type

MediaBlockType

Block types this assembler knows how to reassemble into media handles.

Type

MessageMetadataMap

Read-only map exposed via MessageMetadataTracker.store.

Type

OnToolCallback

Type

PlayerStatus

Lifecycle state of an audio or video player returned by useAudioPlayer and useVideoPlayer.

Single-enum design (vs. multiple booleans) eliminates impossible states and gives callers a clean switch t

Type

SelectorTarget

What a selector hook can be targeted at. Callers can pass any of:

  • undefined — root namespace (cheap: served by the always-on root store)
  • a SubagentDiscoverySnapshot — the
Type

StreamProviderCustomProps

Props for StreamProvider when wiring a custom AgentServerAdapter. Mirrors CustomAdapterOptions plus children.

Type

StreamProviderProps

Props for StreamProvider when talking to the default LangGraph Platform agent server. Mirrors AgentServerOptions plus children.

Type

SubmissionQueueSnapshot

Read-only snapshot of the queue. The queue store hands this out directly; consumers must not mutate the array.

Type

ToolCallFromTool

Infer the streaming AssembledToolCall handle for a single LangChain tool.

Parallel to ToolCallFromTool for message-level tool calls — use this when a component receives one entry from `stream.toolCal

Type

ToolCallsFromTools

Infer a union of tool call types from an array of tools. Works with tools created via tool() from @langchain/core/tools.

Type

ToolCallState

The lifecycle state of a tool call.

  • pending: Tool call received, awaiting result
  • completed: Tool execution finished successfully
  • error: Tool execution failed (result.status === "error")
Type

ToolCallStatus

High-level outcome of a single tool call.

Type

ToolCallWithResult

Type

UseStreamOptions

Type

UseStreamResult

Convenience alias for the fully-resolved stream handle type.

Type

UseSuspenseStreamReturn

Return shape of useSuspenseStream. Identical to the UseStreamReturn surface except:

  • isLoading / isThreadLoading / hydrationPromise are removed (Suspense and Error Boundaries handle those ph
Type

WidenUpdateMessages

Widen an update type so its messages field also accepts @langchain/core BaseMessage class instances (single or array). Framework bindings apply this to submit() so callers can write `stream.subm