File: langchain-adapter.md | Updated: 11/15/2025
Menu
v5 (Latest)
AI SDK 5.x
Model Context Protocol (MCP) Tools
AWSBedrockAnthropicMessagesStream
Copy markdown
==========================================================================================================
The @ai-sdk/langchain module provides helper functions to transform LangChain output streams into data streams and data stream responses. See the LangChain Adapter documentation
for more information.
It supports:
import { toDataStreamResponse } from "@ai-sdk/langchain"
(stream: ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, AIStreamCallbacksAndOptions) => AIStream
Converts LangChain output streams to data stream.
(stream: ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, options?: {init?: ResponseInit, data?: StreamData, callbacks?: AIStreamCallbacksAndOptions}) => Response
Converts LangChain output streams to data stream response.
(stream: ReadableStream<LangChainStreamEvent> | ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, options: { dataStream: DataStreamWriter; callbacks?: StreamCallbacks }) => void
Merges LangChain output streams into an existing data stream.
app/api/completion/route.ts
import { toUIMessageStream } from '@ai-sdk/langchain';import { ChatOpenAI } from '@langchain/openai';import { createUIMessageStreamResponse } from 'ai';
export async function POST(req: Request) { const { prompt } = await req.json();
const model = new ChatOpenAI({ model: 'gpt-3.5-turbo-0125', temperature: 0, });
const stream = await model.stream(prompt);
return createUIMessageStreamResponse({ stream: toUIMessageStream(stream), });}
app/api/completion/route.ts
import { toUIMessageStream } from '@ai-sdk/langchain';import { StringOutputParser } from '@langchain/core/output_parsers';import { ChatOpenAI } from '@langchain/openai';import { createUIMessageStreamResponse } from 'ai';
export async function POST(req: Request) { const { prompt } = await req.json();
const model = new ChatOpenAI({ model: 'gpt-3.5-turbo-0125', temperature: 0, });
const parser = new StringOutputParser();
const stream = await model.pipe(parser).stream(prompt);
return createUIMessageStreamResponse({ stream: toUIMessageStream(stream), });}
On this page
Convert LangChain Expression Language Stream
Convert StringOutputParser Stream
Deploy and Scale AI Apps with Vercel.
Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.
Trusted by industry leaders: