Integrate Vercel AI SDK LangChain into Next.js
Many tutorials that connect LangChain to client chat interfaces rely on outdated adapters that fail in the Next.js App Router. This guide is for full-stack TypeScript developers looking to orchestrate backend chains while streaming to reactive UI components. By implementing the dedicated adapter package, you will create a reliable bridge between your server chains and client hooks.
Before you start
- Node.js 20 or higher installed
- Basic familiarity with Next.js App Router and React hooks
- An OpenAI API key or a compatible model endpoint
Understanding the Modern Architecture
LangChain serves as an orchestration engine for state machines, complex chains, and tool calling. The Vercel AI SDK serves as the network transport and client-state layer that handles streaming responses in React.
Earlier setups imported adapters directly from the core ai package. Modern architectures rely on @ai-sdk/langchain, which supplies toBaseMessages for handling inputs and toUIMessageStream for pushing chunk streams back to the browser.
Project Scaffolding and Dependencies
Project setup starts by scaffolding a new Next.js application using the App Router and TypeScript with the project creation command shown below.
After entering the project directory, install ai, @ai-sdk/react, @ai-sdk/langchain, @langchain/core, and @langchain/openai. Note that this scaffolding step did not pass automated sandbox testing, so verify your package installation locally.
# Scaffold Next.js project files non-interactively without immediately installing dependencies
npx --yes create-next-app@latest vercel-langchain-demo --typescript --tailwind --app --eslint --no-src-dir --import-alias "@/*" --use-npm --skip-install
# Navigate into the project directory and install all required packages alongside project dependencies
cd vercel-langchain-demo && npm install ai @ai-sdk/react @ai-sdk/langchain @langchain/core @langchain/openai
Output from our test run
$ npx --yes create-next-app@latest vercel-langchain-demo --typescript --tailwind --app --eslint --no-src-dir --i
mport-alias "@/*" --use-npm --skip-install
Using defaults for unprovided options:
--ts TypeScript (use --js for JavaScript)
--no-react-compiler No React Compiler (use --react-compiler for React Compiler)
--cache-components Cache Components (use --no-cache-components for No Cache Components)
--agents-md AGENTS.md (use --no-agents-md for No AGENTS.md)
--no-agent-feedback No agent feedback (use --agent-feedback for Agent feedback)
Creating a new Next.js app in /root/vercel-langchain-demo.
Using npm.
Initializing project with template: app-tw
Initialized a git repository.
Success! Created vercel-langchain-demo at /root/vercel-langchain-demo
npm notice
npm notice New major version of npm available! 10.9.9 -> 12.2.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v12.2.1
npm notice To update run: npm install -g [email protected]
npm notice
$ cd vercel-langchain-demo && npm install ai @ai-sdk/react @ai-sdk/langchain @langchain/core @langchain/openaiBuilding the Chat Route Handler
Create the API route handler in app/api/chat/route.ts using the provided code. The file exports runtime set to nodejs because several LangChain internals rely on Node APIs not present in Edge runtimes.
The POST handler accepts an incoming array of UIMessage objects from the client and converts them into LangChain messages using toBaseMessages. It then invokes the stream method on ChatOpenAI.
The resulting chunk stream passes directly into toUIMessageStream. Wrapping that stream inside createUIMessageStreamResponse outputs a standard response formatted for client consumption.
import { createUIMessageStreamResponse, UIMessage } from 'ai';
import { toBaseMessages, toUIMessageStream } from '@ai-sdk/langchain';
import { ChatOpenAI } from '@langchain/openai';
export const runtime = 'nodejs';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const model = new ChatOpenAI({
model: 'gpt-4o-mini',
streaming: true,
});
const langchainMessages = toBaseMessages(messages);
const stream = await model.stream(langchainMessages);
return createUIMessageStreamResponse({
stream: toUIMessageStream(stream),
});
}
Avoiding Common Streaming Pitfalls
A common pitfall is adding StringOutputParser to a LangChain execution pipeline before sending it to the client. The toUIMessageStream function requires raw AIMessageChunk objects with token deltas rather than plain text strings.
Another frequent issue is sending raw LangChain message classes directly to the client. Frontend hooks expect standard message objects with identifiers, roles, and content fields, making conversion helpers essential on both ends.
Creating the React Chat Interface
The client chat interface in app/page.tsx uses the useChat hook from @ai-sdk/react. This keeps your client bundle free from LangChain dependencies while managing form state, loading indicators, and message history automatically.
The interface maps over the message array to show user and assistant text while disabling the input and submit button whenever a message is currently streaming or submitted.
'use client';
import { useChat } from '@ai-sdk/react';
export default function ChatPage() {
const { messages, input, handleInputChange, handleSubmit, status } = useChat();
return (
<main className="max-w-2xl mx-auto p-6 space-y-4 font-sans">
<h1 className="text-xl font-bold text-gray-900 dark:text-gray-100">
LangChain + Vercel AI SDK
</h1>
<div className="space-y-3 min-h-[300px] border rounded-lg p-4 bg-gray-50 dark:bg-zinc-900">
{messages.length === 0 && (
<p className="text-gray-500 text-sm">Send a message to start streaming.</p>
)}
{messages.map((m) => (
<div key={m.id} className="text-sm">
<span className="font-semibold capitalize">{m.role}: </span>
<span>{m.content}</span>
</div>
))}
</div>
<form onSubmit={handleSubmit} className="flex gap-2">
<input
value={input}
onChange={handleInputChange}
placeholder="Ask something..."
className="flex-1 p-2 border rounded-md dark:bg-zinc-800 dark:text-white"
disabled={status === 'submitted' || status === 'streaming'}
/>
<button
type="submit"
className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:opacity-50"
disabled={status === 'submitted' || status === 'streaming'}
>
Send
</button>
</form>
</main>
);
}
Verifying Types and Production Build
Running the build script checks TypeScript types and ensures the dynamic API routes compile properly under Next.js.
During automated test execution in the Debian sandbox, this build step failed to pass. Confirm that your local environment variables and package versions match before deploying to production.
# Verify Next.js build and TypeScript type-checking
npm run build
Output from our test run
$ npm run build
> [email protected] build
> next build
▲ Next.js 16.4.3 (Turbopack)
✓ Running next.config.ts took 430ms
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visi
ting the following URL:
https://nextjs.org/telemetry
- Cache Components enabled
- Partial Prefetching enabled
Creating an optimized production build ...
✓ Compiled successfully in 10.7s
Running TypeScript ...
Finished TypeScript in 5.4s ...
Collecting page data using 5 workers ...
✓ Collecting page data using 5 workers in 1157ms
Generating static pages using 5 workers (0/4) ...
Generating static pages using 5 workers (1/4)
Generating static pages using 5 workers (2/4)
Generating static pages using 5 workers (3/4)
✓ Generating static pages using 5 workers (4/4) in 1189msWrap-up
Using the dedicated @ai-sdk/langchain adapter provides a stable bridge between LangChain backend workflows and reactive Next.js client hooks. Enforcing raw message chunk streams and standard Node.js runtimes keeps your streaming chat interface clean and maintainable.
FAQ
Why does toUIMessageStream break when using StringOutputParser?
The toUIMessageStream function expects raw AIMessageChunk objects containing token metadata and delta fields. Converting output into plain text strings strips this structure and causes the stream converter to fail.
Why should the chat route handler use the nodejs runtime instead of edge?
LangChain modules, tokenizers, and related dependencies often depend on standard Node.js APIs and cryptographic modules that are unsupported on the Edge runtime.
What replaces the deprecated LangChainAdapter from the core ai package?
The modern approach uses the standalone @ai-sdk/langchain package, which provides toBaseMessages for incoming messages and toUIMessageStream for outbound chunk streams.