The streamRunnableUI function executes the streamEvents method on a LangChain Runnable or CompiledStateGraph and converts the resulting generator into a React Server Component (RSC) friendly stream.
It returns an object containing:
ui: A streamable UI value created via createStreamableUI that can be consumed by the client.lastEvent: A promise that resolves to the final output of the runnable (e.g., lastEventValue.data.output).
To update the UI or trigger client-side callbacks during the stream, provide eventHandlers. Each handler receives the current StreamEvent and an EventHandlerFields object containing the ui stream and a callbacks record.
import { streamRunnableUI } from './server';
const uiStream = streamRunnableUI(
myLangChainRunnable,
{ input: 'hello' },
{
eventHandlers: [
async (event, { ui, callbacks }) => {
// Example: Update UI based on specific stream events
if (event.event === 'on_chat_model_stream') {
// logic to update ui
}
}
]
}
);
// uiStream.ui can be returned from a React Server Component
// uiStream.lastEvent is a promise resolving to the final output