LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
  • Store
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemote
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
Store
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/langgraph-sdkreact
Module●Since v0.0

react

Copy
import { ... } from "@langchain/langgraph-sdk/react";

Functions

function
calculateDepthFromNamespace→ number
function
extractParentIdFromNamespace→ string | null
function
extractToolCallIdFromNamespace→ string | undefined
function
isSubagentNamespace→ boolean
function
useStream→ ResolveStreamInterface<T, InferBag<T, Bag>>

Classes

class
FetchStreamTransport
class
SubagentManager

Interfaces

Type Aliases

View source on GitHub
interface
AgentTypeConfigLike

Minimal interface matching the structure of AgentTypeConfig from @langchain/langgraph. This allows type inference from ReactAgent without requiring the langchain dependency.

interface
CompiledSubAgentLike

Minimal interface matching the structure of a CompiledSubAgent from deepagents. Used for structural type matching without importing deepagents.

interface
DeepAgentTypeConfigLike

Minimal interface matching the structure of DeepAgentTypeConfig from deepagents. Extends AgentTypeConfigLike to include subagent type information.

interface
SubAgentLike

Minimal interface matching the structure of a SubAgent from deepagents. Used for structural type matching without importing deepagents.

interface
SubagentStreamInterface

Base interface for a single subagent stream. Tracks the lifecycle of a subagent from invocation to completion.

Extends StreamBase to share common properties with UseStream, allowing subagents to be treated similarly to the main stream.

Prefer using SubagentStream which supports passing an agent type directly for automatic type inference.

interface
SubagentToolCall

Represents a tool call that initiated a subagent.

interface
UseStream
interface
UseStreamOptions
interface
UseStreamThread
interface
UseStreamTransport

Transport used to stream the thread. Only applicable for custom endpoints using toLangGraphEventStream or toLangGraphEventStreamResponse.

interface
BaseStream

Base stream interface shared by all stream types.

Contains core properties for state management, messaging, and stream control that are common to CompiledStateGraph, ReactAgent, and DeepAgent streams.

This interface provides the foundation that all stream types build upon:

  • State management (values, isLoading, error)
  • Message handling (messages)
  • Interrupt handling (interrupt)
  • Stream lifecycle (submit, stop)
  • Branching and history (branch, history)
interface
UseAgentStream

Stream interface for ReactAgent instances created with createAgent.

Extends UseGraphStream with tool calling capabilities. Tool calls are automatically typed based on the agent's tools configuration.

Use this interface when streaming from an agent created with createAgent. For subagent streaming capabilities, use UseDeepAgentStream with createDeepAgent.

This interface is subject to change.

interface
UseAgentStreamOptions

Options for configuring an agent stream.

Use this options interface when calling useStream with a ReactAgent created via createAgent.

This interface is subject to change.

interface
UseDeepAgentStream

Stream interface for DeepAgent instances created with createDeepAgent.

Extends UseAgentStream with subagent streaming capabilities. Subagent streams are automatically typed based on the agent's subagent configuration, enabling type-safe access to subagent state and messages.

Use this interface when streaming from an agent created with createDeepAgent that orchestrates multiple specialized subagents.

This interface is subject to change.

interface
UseDeepAgentStreamOptions

Options for configuring a deep agent stream.

Use this options interface when calling useStream with a DeepAgent created via createDeepAgent. Includes all agent options plus subagent-specific configuration.

typeAlias
BaseSubagentState

Base state type for subagents. All subagents have at least a messages array, similar to the main agent.

typeAlias
DefaultSubagentStates: Record<string, Record<string, unknown>>

Default subagent state map used when no specific subagent types are provided. Maps any string key to Record<string, unknown>.

typeAlias
DefaultToolCall

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

typeAlias
ExtractAgentConfig: T extends __type ? Config extends AgentTypeConfigLike ? Config : never : never

Extract the AgentTypeConfig from an agent-like type.

typeAlias
ExtractDeepAgentConfig: T extends __type ? Config extends DeepAgentTypeConfigLike ? Config : never : never

Extract the DeepAgentTypeConfig from a DeepAgent-like type.

typeAlias
ExtractSubAgentMiddleware: T extends __type ? M extends readonly AgentMiddlewareLike[] ? M : M extends AgentMiddlewareLike[] ? M : readonly [] : readonly []

Helper type to extract middleware from a SubAgent definition. Handles both mutable and readonly middleware arrays.

typeAlias
GetToolCallsType: ExtractToolCallsFromState<StateType> extends never ? DefaultToolCall : ExtractToolCallsFromState<StateType>

Extract the tool call type from a StateType's messages property. This is the canonical way to get typed tool calls in useStream.

Tool call types are now extracted from the messages property of StateType, rather than being specified separately in the Bag.

typeAlias
InferAgentToolCalls: ExtractAgentConfig<T>["Tools"] extends readonly Tool[] ? ToolCallFromAgentTool<Tool> extends never ? DefaultToolCall : ToolCallFromAgentTool<Tool> : DefaultToolCall

Extract tool calls type from an agent's tools. Converts the tools array to a discriminated union of tool calls.

This handles both tuple types (e.g., readonly [Tool1, Tool2]) and array-of-union types (e.g., readonly (Tool1 | Tool2)[]) which is how createAgent captures tool types.

typeAlias
InferDeepAgentSubagents: ExtractDeepAgentConfig<T> extends never ? never : ExtractDeepAgentConfig<T>["Subagents"]

Extract the Subagents array type from a DeepAgent.

typeAlias
InferSubagentByName: InferDeepAgentSubagents<T> extends readonly SA[] ? SA extends __type ? SA : never : never

Helper type to extract a subagent by name from a DeepAgent.

typeAlias
InferSubagentNames: InferDeepAgentSubagents<T> extends readonly SA[] ? SA extends __type ? N extends string ? N : never : never : never

Extract all subagent names as a string union from a DeepAgent.

typeAlias
InferSubagentState: InferSubagentByName<T, TName> extends never ? Record<string, unknown> : InferSubagentByName<T, TName> extends SA ? BaseSubagentState<ToolCall> & InferMiddlewareStatesFromArray<ExtractSubAgentMiddleware<SA>> : Record<string, unknown>

Infer the state type for a specific subagent by extracting and merging its middleware state schemas, plus the base agent state (messages).

typeAlias
IsAgentLike: T extends __type ? true : false

Check if a type is agent-like (has ~agentTypes phantom property). This property is present on ReactAgent instances created with createAgent.

typeAlias
IsDeepAgentLike: T extends __type ? true : false

Check if a type is a DeepAgent (has ~deepAgentTypes phantom property). This property is present on DeepAgent instances created with createDeepAgent.

typeAlias
MessageMetadata
typeAlias
SubagentStateMap: { [K in InferSubagentNames<T>]: InferSubagentState<T, K, ToolCall> }

Create a map of subagent names to their state types. This is useful for type-safe getSubagentsByType calls.

typeAlias
SubagentStatus: "pending" | "running" | "complete" | "error"

The execution status of a subagent.

  • "pending" - The subagent has been invoked but hasn't started processing yet. This is the initial state when a tool call is detected but before any streaming events are received from the subgraph.

  • "running" - The subagent is actively executing and streaming updates. The subagent transitions to this state when the first update event is received from its namespace.

  • "complete" - The subagent has finished execution successfully. A tool message with the result has been received, and the result property contains the final output.

  • "error" - The subagent encountered an error during execution. The error property on the SubagentStream contains error details.

typeAlias
SubagentStream: IsDeepAgentLike<T> extends true ? SubagentStreamInterface<SubagentStateMap<T, InferAgentToolCalls<T>>[InferSubagentNames<T>], InferAgentToolCalls<T>, InferSubagentNames<T>> : IsAgentLike<T> extends true ? SubagentStreamInterface<InferAgentState<T>, InferAgentToolCalls<T>> : SubagentStreamInterface<T, ToolCall>

Represents a single subagent stream.

Supports two usage patterns:

  1. Agent type inference (recommended): Pass a DeepAgent type directly and let TypeScript infer the correct state and tool call types.
import type { agent } from "./agent";

// Automatically infers state and tool call types from the agent
const subagent: SubagentStream<typeof agent> = ...;
  1. Explicit generics: Pass state and tool call types manually.
type ResearcherState = { research_notes: string };
const researcher: SubagentStream<ResearcherState, MyToolCall> = ...;
typeAlias
ToolCallFromTool: T extends __type ? InferToolInput<T> extends Args ? Args extends never ? never : Args extends Record<string, any> ? __type : never : never : never

Infer a tool call type from a single tool. Works with tools created via tool() from @langchain/core/tools.

For DynamicStructuredTool, this extracts the input type from the _call method, which is the most reliable source as it's the pre-computed input type.

typeAlias
ToolCallsFromTools: T extends readonly [First, ...Rest] ? ToolCallFromTool<First> | ToolCallsFromTools<Rest> : never

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

typeAlias
ToolCallState: "pending" | "completed" | "error"

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")
typeAlias
ToolCallWithResult

Represents a tool call paired with its result. Useful for rendering tool invocations and their outputs together.

typeAlias
UseStreamCustom: Pick<UseStream<StateType, Bag, SubagentStates>, "values" | "error" | "isLoading" | "stop" | "interrupt" | "interrupts" | "messages" | "toolCalls" | "getToolCalls" | "subagents" | "activeSubagents" | "getSubagent" | "getSubagentsByType" | "getSubagentsByMessage"> & __type
typeAlias
UseStreamCustomOptions: Pick<UseStreamOptions<StateType, Bag>, "messagesKey" | "threadId" | "onThreadId" | "onError" | "onCreated" | "onUpdateEvent" | "onCustomEvent" | "onMetadataEvent" | "onLangChainEvent" | "onDebugEvent" | "onCheckpointEvent" | "onTaskEvent" | "onStop" | "initialValues" | "throttle"> & __type
typeAlias
InferBag: T extends __type ? BagTemplate : B

Infer the Bag type from an agent, defaulting to the provided Bag.

Currently returns the provided Bag for all types. Can be extended in the future to extract Bag from agent types.

typeAlias
InferNodeNames: T extends __type ? N extends string ? Exclude<N, "__start__"> : string : string

Infer the node names from a compiled graph.

Extracts the ~NodeType phantom property from CompiledGraph instances, providing a union of all node names defined in the graph.

typeAlias
InferStateType: T extends __type ? InferAgentState<T> : T extends __type ? S extends Record<string, unknown> ? S : Record<string, unknown> : T extends __type ? O extends Record<string, unknown> ? O : Record<string, unknown> : T extends Record<string, unknown> ? T : Record<string, unknown>
typeAlias
InferSubagentStates: T extends __type ? SubagentStateMap<T, InferAgentToolCalls<T>> : DefaultSubagentStates

Infer subagent state map from a DeepAgent.

For DeepAgent, creates a map of subagent names to their state types. For non-DeepAgent, returns DefaultSubagentStates.

typeAlias
InferToolCalls: T extends __type ? InferAgentToolCalls<T> : DefaultToolCall

Infer tool call types from an agent.

For agents, extracts typed tool calls from the agent's tools. For non-agents, returns DefaultToolCall.

typeAlias
ResolveStreamInterface: IsDeepAgent<T> extends true ? UseDeepAgentStream<InferStateType<T>, InferToolCalls<T>, InferSubagentStates<T>, Bag> : IsReactAgent<T> extends true ? UseAgentStream<InferStateType<T>, InferToolCalls<T>, Bag> : UseDeepAgentStream<InferStateType<T>, InferToolCalls<T>, DefaultSubagentStates, Bag>

Resolves the appropriate stream interface based on the agent/graph type.

This type automatically selects the correct stream interface based on the type of agent or graph passed to useStream:

  1. DeepAgent (~deepAgentTypes) → UseDeepAgentStream

    • Includes: values, messages, toolCalls, subagents, getSubagentsByType, getSubagentsByMessage
  2. ReactAgent (~agentTypes) → UseAgentStream

    • Includes: values, messages, toolCalls, getToolCalls
    • Excludes: subagents, getSubagentsByType
  3. CompiledGraph / Default → UseDeepAgentStream with DefaultSubagentStates

    • Includes: values, messages, submit, stop, toolCalls, subagents
    • SubagentStates defaults to DefaultSubagentStates (loosely typed)
typeAlias
ResolveStreamOptions: IsDeepAgent<T> extends true ? UseDeepAgentStreamOptions<InferStateType<T>, Bag> : IsReactAgent<T> extends true ? UseAgentStreamOptions<InferStateType<T>, Bag> : UseStreamOptions<InferStateType<T>, Bag>

Resolves the appropriate options interface based on the agent/graph type.

This type automatically selects the correct options interface based on the type of agent or graph:

  1. DeepAgent → UseDeepAgentStreamOptions

    • Includes: filterSubagentMessages option
  2. ReactAgent → UseAgentStreamOptions

  3. CompiledGraph / Default → UseGraphStreamOptions

Calculates the depth of a subagent based on its namespace. Counts the number of "tools:" segments in the namespace.

Extracts the parent tool call ID from a namespace.

For nested subagents, the namespace looks like: ["tools:parent_id", "tools:child_id", ...]

Extracts the tool call ID from a namespace path.

Namespaces follow the pattern: ["tools:call_abc123", "model_request:xyz", ...] This function extracts "call_abc123" from the first "tools:" segment.

Checks if a namespace indicates a subagent/subgraph message.

Subagent namespaces contain a "tools:" segment indicating they originate from a tool call that spawned a subgraph.

A React hook that provides seamless integration with LangGraph streaming capabilities.

The useStream hook handles all the complexities of streaming, state management, and branching logic, letting you focus on building great chat experiences. It provides automatic state management for messages, interrupts, loading states, subagent streams, and errors.

Usage with ReactAgent (recommended for createAgent users)

When using createAgent from @langchain/langgraph, you can pass typeof agent as the type parameter to automatically infer tool call types:

Transport used to stream the thread. Only applicable for custom endpoints using toLangGraphEventStream or toLangGraphEventStreamResponse.

Manages subagent execution state.

Tracks subagents from the moment they are invoked (AI message with tool calls) through streaming to completion (tool message result).

Infer the state type from an agent, graph, or direct state type.

Detection order:

  1. Agent-like (~agentTypes) → InferAgentState
  2. CompiledGraph (~RunOutput) → Extract RunOutput
  3. Pregel (~OutputType) → Extract OutputType
  4. Direct state type → Return as-is