LangChain Adapter
LangChain is a framework for building applications powered by large language models. It provides tools and abstractions for working with AI models, prompts, chains, vector stores, and other data sources for retrieval augmented generation (RAG). LangGraph is a library built on top of LangChain for creating stateful, multi-actor applications. It enables you to define complex agent workflows as graphs, with support for cycles, persistence, and human-in-the-loop patterns. The@ai-sdk/langchain adapter provides seamless integration between LangChain, LangGraph, and the AI SDK, enabling you to use LangChain models and LangGraph agents with AI SDK UI components.
Installation
- Tab Title
- Tab Title
- Tab Title
@langchain/core is a required peer dependency.Features
- Convert AI SDK
UIMessageto LangChainBaseMessageformat usingtoBaseMessages - Transform LangChain/LangGraph streams to AI SDK
UIMessageStreamusingtoUIMessageStream - Support for
streamEvents()output for granular event streaming and observability LangSmithDeploymentTransportfor connecting directly to a deployed LangGraph graph- Full support for text, tool calls, tool results, and multimodal content
- Custom data streaming with typed events (
data-{type})
Example: Basic Chat
Here is a basic example that uses both the AI SDK and LangChain together with the Next.js App Router.useChat hook in the page component:
Example: LangGraph
Use the adapter with LangGraph to build agent workflows:Example: Custom Data Streaming
LangChain tools can emit custom data events usingconfig.writer(). The adapter converts these to typed data-{type} parts that can be rendered in the UI or handled via the onData callback:
onData callback or render persistent data parts:
Custom data behavior:
- Data with an
idfield is persistent (added tomessage.partsfor rendering) - Data without an
idis transient (only delivered via theonDatacallback) - The
typefield determines the event name:{ type: 'progress' }→data-progress
Example: LangSmith Deployment Transport
Connect directly to a LangGraph deployment from the browser usingLangSmithDeploymentTransport, bypassing the need for a backend API route:
LangSmithDeploymentTransport constructor accepts the following options:
url: The LangSmith deployment URL or local server URL (required)apiKey: API key for authentication (optional for local development)graphId: The ID of the graph to connect to (defaults to'agent')
API Reference
toBaseMessages(messages)
Converts AI SDK UIMessage objects to LangChain BaseMessage objects.
messages:UIMessage[]- Array of AI SDK UI messages
Promise<BaseMessage[]>
toUIMessageStream(stream)
Converts a LangChain/LangGraph stream to an AI SDK UIMessageStream. Automatically detects the stream type and handles direct model streams, LangGraph streams, and streamEvents() output.
stream:AsyncIterable<AIMessageChunk> | ReadableStream- LangChain model stream, LangGraph stream, orstreamEvents()output
ReadableStream<UIMessageChunk>
LangSmithDeploymentTransport
A ChatTransport implementation for LangSmith/LangGraph deployments. Use this with the useChat hook’s transport option.
options:LangSmithDeploymentTransportOptionsurl:string- LangSmith deployment URL or local server URL (required)apiKey?:string- API key for authentication (optional)graphId?:string- The ID of the graph to connect to (defaults to'agent')
ChatTransport