📄 ai-sdk/docs/reference/stream-helpers/langchain-adapter

File: langchain-adapter.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/docs/reference/stream-helpers/langchain-adapter

AI SDK

Menu

v5 (Latest)

AI SDK 5.x

AI SDK by Vercel

AI SDK 6 Beta

Foundations

Overview

Providers and Models

Prompts

Tools

Streaming

Getting Started

Navigating the Library

Next.js App Router

Next.js Pages Router

Svelte

Vue.js (Nuxt)

Node.js

Expo

Agents

Agents

Building Agents

Workflow Patterns

Loop Control

AI SDK Core

Overview

Generating Text

Generating Structured Data

Tool Calling

Model Context Protocol (MCP) Tools

Prompt Engineering

Settings

Embeddings

Image Generation

Transcription

Speech

Language Model Middleware

Provider & Model Management

Error Handling

Testing

Telemetry

AI SDK UI

Overview

Chatbot

Chatbot Message Persistence

Chatbot Resume Streams

Chatbot Tool Usage

Generative User Interfaces

Completion

Object Generation

Streaming Custom Data

Error Handling

Transport

Reading UIMessage Streams

Message Metadata

Stream Protocols

AI SDK RSC

Advanced

Reference

AI SDK Core

AI SDK UI

AI SDK RSC

Stream Helpers

AIStream

StreamingTextResponse

streamToResponse

OpenAIStream

AnthropicStream

AWSBedrockStream

AWSBedrockAnthropicStream

AWSBedrockAnthropicMessagesStream

AWSBedrockCohereStream

AWSBedrockLlama2Stream

CohereStream

GoogleGenerativeAIStream

HuggingFaceStream

@ai-sdk/langchain Adapter

@ai-sdk/llamaindex Adapter

MistralStream

ReplicateStream

InkeepStream

AI SDK Errors

Migration Guides

Troubleshooting

Copy markdown

@ai-sdk/langchain

==========================================================================================================

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:

  • LangChain StringOutputParser streams
  • LangChain AIMessageChunk streams
  • LangChain StreamEvents v2 streams

Import


import { toDataStreamResponse } from "@ai-sdk/langchain"

API Signature


Methods

toDataStream:

(stream: ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, AIStreamCallbacksAndOptions) => AIStream

Converts LangChain output streams to data stream.

toDataStreamResponse:

(stream: ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, options?: {init?: ResponseInit, data?: StreamData, callbacks?: AIStreamCallbacksAndOptions}) => Response

Converts LangChain output streams to data stream response.

mergeIntoDataStream:

(stream: ReadableStream<LangChainStreamEvent> | ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, options: { dataStream: DataStreamWriter; callbacks?: StreamCallbacks }) => void

Merges LangChain output streams into an existing data stream.

Examples


Convert LangChain Expression Language 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),  });}

Convert StringOutputParser 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

@ai-sdk/langchain

Import

API Signature

Methods

Examples

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:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert