LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
React SDK
Vue SDK
Svelte SDK
Angular SDK
LangGraph SDK
  • Ui
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Utils
  • 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
React SDK
Vue SDK
Svelte SDK
Angular SDK
LangGraph SDK
UiClientAuthReactLoggingReact UiUtilsServer
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/reactStreamProvider
Function●Since v0.1

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, errors, interrupts, etc.) without prop drilling.

Copy
StreamProvider<
  T = Record<string, unknown>,
  Bag extends BagTemplate = BagTemplate
>(props: StreamProviderProps<T, Bag> | StreamProviderCustomProps<T, Bag>): ReactNode

Parameters

NameTypeDescription
props*StreamProviderProps<T, Bag> | StreamProviderCustomProps<T, Bag>

Example 1

Copy
import { StreamProvider, useStreamContext } from "@langchain/react";

function App() {
  return (
    <StreamProvider assistantId="agent" apiUrl="http://localhost:2024">
      <ChatHeader />
      <MessageList />
      <MessageInput />
    </StreamProvider>
  );
}

function ChatHeader() {
  const { isLoading, error } = useStreamContext();
  return (
    <header>
      {isLoading && <span>Thinking...</span>}
      {error && <span>Error</span>}
    </header>
  );
}

function MessageList() {
  const { messages } = useStreamContext();
  return messages.map((msg, i) => <div key={msg.id ?? i}>{msg.content}</div>);
}

function MessageInput() {
  const { submit } = useStreamContext();
  return (
    <button onClick={() => submit({ messages: [{ type: "human", content: "Hi" }] })}>
      Send
    </button>
  );
}

Example 2

Copy
<StreamProvider assistantId="researcher" apiUrl="http://localhost:2024">
  <ResearchPanel />
</StreamProvider>
<StreamProvider assistantId="writer" apiUrl="http://localhost:2024">
  <WriterPanel />
</StreamProvider>
View source on GitHub