Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Const

LangChain

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.

How it works

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.

Syntax

import { toDataStreamResponse } from '@ai-sdk/langchain';
toDataStreamResponse(stream, options?)

Parameters

NameTypeDescription
streamReadableStream<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.

Returns

toDataStream produces an AIStream; toDataStreamResponse produces a Response; mergeIntoDataStream writes into an existing data stream.

Key points

  • The adapter connects LangChain and LangGraph workflows to AI SDK applications.
  • toBaseMessages translates AI SDK UI messages into LangChain message objects.
  • toUIMessageStream converts LangChain or LangGraph output for AI SDK UI streaming.
  • Stream-event output can be preserved for detailed event handling and observability.
  • The integration handles text, tool interactions, tool results, and multimodal data.
  • Applications can send custom typed data events through the stream.

Common mistakes

  • Install @langchain/core alongside the adapter; the adapter does not provide this dependency itself.

Example: Convert a text stream to an AI SDK response

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.

When to use it

Stream chain output

A LangChain chain streams generated text to an AI SDK UI client.

Expose graph agents in UI

A LangGraph workflow needs to appear in an AI SDK chat interface.

Observe agent events

An application needs detailed stream events for monitoring or custom handling.

Stream rich model output

A workflow emits tool calls, results, or multimodal content.

  • @ai-sdk/langchain
  • toBaseMessages
  • toUIMessageStream
  • toDataStream
  • toDataStreamResponse
  • mergeIntoDataStream
  • LangGraph
  • streamEvents

Source: LangChain. Checked 2026-10-15.