Stream chain output
A LangChain chain streams generated text to an AI SDK UI client.
AI Tools · Const
The @ai-sdk/langchain adapter connects LangChain and LangGraph with the AI SDK. It converts their streams and messages for AI SDK UI components, including text, tools, multimodal content, and typed data events.
The @ai-sdk/langchain adapter bridges LangChain and LangGraph output with the AI SDK by converting text, message-chunk, or event streams into AI SDK data streams and responses. It also supports translating UI messages into LangChain base messages and can merge output into an existing data stream.
import { toDataStreamResponse } from '@ai-sdk/langchain';
toDataStreamResponse(stream, options?)| Name | Type | Description |
|---|---|---|
stream | ReadableStream<LangChainAIMessageChunk> | ReadableStream<string> | ReadableStream<LangChainStreamEvent> | A LangChain output stream containing strings, AI message chunks, or stream events, depending on the adapter method. |
options | { init?: ResponseInit; data?: StreamData; callbacks?: AIStreamCallbacksAndOptions } | Optional response initialization, stream data, and callback configuration for toDataStreamResponse. |
toDataStream produces an AIStream; toDataStreamResponse produces a Response; mergeIntoDataStream writes into an existing data stream.
import { toDataStreamResponse } from '@ai-sdk/langchain';
export async function POST() {
const stream = new ReadableStream({
start(controller) {
controller.enqueue('Hello from LangChain');
controller.close();
},
});
return toDataStreamResponse(stream);
}This route converts a LangChain-compatible text stream into an AI SDK data-stream response.
A LangChain chain streams generated text to an AI SDK UI client.
A LangGraph workflow needs to appear in an AI SDK chat interface.
An application needs detailed stream events for monitoring or custom handling.
A workflow emits tool calls, results, or multimodal content.
@ai-sdk/langchaintoBaseMessagestoUIMessageStreamtoDataStreamtoDataStreamResponsemergeIntoDataStreamLangGraphstreamEvents