Skip to main content

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

@langchain/core is a required peer dependency.

Features

  • Convert AI SDK UIMessage to LangChain BaseMessage format using toBaseMessages
  • Transform LangChain/LangGraph streams to AI SDK UIMessageStream using toUIMessageStream
  • Support for streamEvents() output for granular event streaming and observability
  • LangSmithDeploymentTransport for 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.
Then, use the AI SDK’s 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 using config.writer(). The adapter converts these to typed data-{type} parts that can be rendered in the UI or handled via the onData callback:
Handle custom data on the client with the onData callback or render persistent data parts:
Custom data behavior:
  • Data with an id field is persistent (added to message.parts for rendering)
  • Data without an id is transient (only delivered via the onData callback)
  • The type field determines the event name: { type: 'progress' }data-progress

Example: LangSmith Deployment Transport

Connect directly to a LangGraph deployment from the browser using LangSmithDeploymentTransport, bypassing the need for a backend API route:
The 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.
Parameters:
  • messages: UIMessage[] - Array of AI SDK UI messages
Returns: 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.
Parameters:
  • stream: AsyncIterable<AIMessageChunk> | ReadableStream - LangChain model stream, LangGraph stream, or streamEvents() output
Returns: ReadableStream<UIMessageChunk>

LangSmithDeploymentTransport

A ChatTransport implementation for LangSmith/LangGraph deployments. Use this with the useChat hook’s transport option.
Constructor Parameters:
  • options: LangSmithDeploymentTransportOptions
    • url: 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')
Implements: ChatTransport

More Examples

You can find additional examples in the AI SDK examples/next-langchain folder.