@ai-sdk/langchain 2.0.0-beta.99 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,202 @@
1
1
  # AI SDK - LangChain Adapter
2
2
 
3
- This package contains a LangChain adapter for the AI SDK.
3
+ The **[AI SDK](https://ai-sdk.dev)** LangChain adapter provides seamless integration between [LangChain](https://langchain.com/) and the AI SDK, enabling you to use LangChain agents and graphs with AI SDK UI components.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @ai-sdk/langchain @langchain/core
9
+ ```
10
+
11
+ > **Note:** `@langchain/core` is a required peer dependency.
12
+
13
+ ## Features
14
+
15
+ - Convert AI SDK `UIMessage` to LangChain `BaseMessage` format
16
+ - Transform LangChain/LangGraph streams to AI SDK `UIMessageStream`
17
+ - `ChatTransport` implementation for LangSmith deployments
18
+ - Full support for text, tool calls, and tool results
19
+ - Custom data streaming with typed events (`data-{type}`)
20
+
21
+ ## Usage
22
+
23
+ ### Converting Messages
24
+
25
+ Use `toBaseMessages` to convert AI SDK messages to LangChain format:
26
+
27
+ ```ts
28
+ import { toBaseMessages } from '@ai-sdk/langchain';
29
+
30
+ // Convert UI messages to LangChain format
31
+ const langchainMessages = await toBaseMessages(uiMessages);
32
+
33
+ // Use with any LangChain model
34
+ const response = await model.invoke(langchainMessages);
35
+ ```
36
+
37
+ ### Streaming from LangGraph
38
+
39
+ Use `toUIMessageStream` to convert LangGraph streams to AI SDK format:
40
+
41
+ ```ts
42
+ import { toBaseMessages, toUIMessageStream } from '@ai-sdk/langchain';
43
+ import { createUIMessageStreamResponse } from 'ai';
44
+
45
+ // Convert messages and stream from a LangGraph graph
46
+ const langchainMessages = await toBaseMessages(uiMessages);
47
+
48
+ const langchainStream = await graph.stream(
49
+ { messages: langchainMessages },
50
+ { streamMode: ['values', 'messages'] },
51
+ );
52
+
53
+ // Convert to UI message stream response
54
+ return createUIMessageStreamResponse({
55
+ stream: toUIMessageStream(langchainStream),
56
+ });
57
+ ```
58
+
59
+ ### Custom Data Streaming
60
+
61
+ LangChain tools can emit custom data events using `config.writer()`. The adapter converts these to typed `data-{type}` parts:
62
+
63
+ ```ts
64
+ import { tool, type ToolRuntime } from 'langchain';
65
+
66
+ const analyzeDataTool = tool(
67
+ async ({ query }, config: ToolRuntime) => {
68
+ // Emit progress updates - becomes 'data-progress' in the UI
69
+ config.writer?.({
70
+ type: 'progress',
71
+ id: 'analysis-1', // Include 'id' to persist in message.parts
72
+ step: 'fetching',
73
+ message: 'Fetching data...',
74
+ progress: 50,
75
+ });
76
+
77
+ // ... perform analysis ...
78
+
79
+ // Emit status update - becomes 'data-status' in the UI
80
+ config.writer?.({
81
+ type: 'status',
82
+ id: 'analysis-1-status',
83
+ status: 'complete',
84
+ message: 'Analysis finished',
85
+ });
86
+
87
+ return 'Analysis complete';
88
+ },
89
+ {
90
+ name: 'analyze_data',
91
+ description: 'Analyze data with progress updates',
92
+ schema: z.object({ query: z.string() }),
93
+ },
94
+ );
95
+ ```
96
+
97
+ Enable the `custom` stream mode to receive these events:
98
+
99
+ ```ts
100
+ const stream = await graph.stream(
101
+ { messages: langchainMessages },
102
+ { streamMode: ['values', 'messages', 'custom'] },
103
+ );
104
+ ```
105
+
106
+ **Custom data behavior:**
107
+
108
+ - Data with an `id` field is **persistent** (added to `message.parts` for rendering)
109
+ - Data without an `id` is **transient** (only delivered via the `onData` callback)
110
+ - The `type` field determines the event name: `{ type: 'progress' }` → `data-progress`
111
+
112
+ ### LangSmith Deployment Transport
113
+
114
+ Use `LangSmithDeploymentTransport` with the AI SDK `useChat` hook to connect directly to a LangGraph deployment from the browser:
115
+
116
+ ```tsx
117
+ import { useChat } from 'ai/react';
118
+ import { LangSmithDeploymentTransport } from '@ai-sdk/langchain';
119
+ import { useMemo } from 'react';
120
+
121
+ function Chat() {
122
+ const transport = useMemo(
123
+ () =>
124
+ new LangSmithDeploymentTransport({
125
+ url: 'https://your-deployment.us.langgraph.app',
126
+ apiKey: process.env.LANGSMITH_API_KEY,
127
+ }),
128
+ [],
129
+ );
130
+
131
+ const { messages, input, handleInputChange, handleSubmit } = useChat({
132
+ transport,
133
+ });
134
+
135
+ return (
136
+ <div>
137
+ {messages.map(m => (
138
+ <div key={m.id}>{m.parts.map(part => part.text).join('')}</div>
139
+ ))}
140
+ <form onSubmit={handleSubmit}>
141
+ <input value={input} onChange={handleInputChange} />
142
+ <button type="submit">Send</button>
143
+ </form>
144
+ </div>
145
+ );
146
+ }
147
+ ```
148
+
149
+ ## API Reference
150
+
151
+ ### `toBaseMessages(messages)`
152
+
153
+ Converts AI SDK `UIMessage` objects to LangChain `BaseMessage` objects.
154
+
155
+ **Parameters:**
156
+
157
+ - `messages`: `UIMessage[]` - Array of AI SDK UI messages
158
+
159
+ **Returns:** `Promise<BaseMessage[]>`
160
+
161
+ ### `convertModelMessages(modelMessages)`
162
+
163
+ Converts AI SDK `ModelMessage` objects to LangChain `BaseMessage` objects.
164
+
165
+ **Parameters:**
166
+
167
+ - `modelMessages`: `ModelMessage[]` - Array of model messages
168
+
169
+ **Returns:** `BaseMessage[]`
170
+
171
+ ### `toUIMessageStream(stream)`
172
+
173
+ Converts a LangChain/LangGraph stream to an AI SDK `UIMessageStream`.
174
+
175
+ **Parameters:**
176
+
177
+ - `stream`: `ReadableStream` - LangGraph stream with `streamMode: ['values', 'messages']`
178
+
179
+ **Returns:** `ReadableStream<UIMessageChunk>`
180
+
181
+ **Supported stream events:**
182
+
183
+ - `messages` - Streaming message chunks (text, tool calls)
184
+ - `values` - State updates that finalize pending message chunks
185
+ - `custom` - Custom data events (emitted as `data-{type}` chunks)
186
+
187
+ ### `LangSmithDeploymentTransport`
188
+
189
+ A `ChatTransport` implementation for LangSmith/LangGraph deployments.
190
+
191
+ **Constructor Parameters:**
192
+
193
+ - `options`: `LangSmithDeploymentTransportOptions` - Configuration for the RemoteGraph connection
194
+ - `url`: `string` - LangSmith deployment URL or local server URL
195
+ - `apiKey?`: `string` - API key for authentication (optional for local development)
196
+ - `graphId?`: `string` - The ID of the graph to connect to (defaults to `'agent'`)
197
+
198
+ **Implements:** `ChatTransport`
199
+
200
+ ## Documentation
201
+
202
+ Please check out the [AI SDK documentation](https://ai-sdk.dev) for more information.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,6 @@
1
- import { UIMessageChunk } from 'ai';
1
+ import { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
2
+ import { UIMessage, UIMessageChunk, ModelMessage, ChatTransport, ChatRequestOptions } from 'ai';
3
+ import { RemoteGraph, RemoteGraphParams } from '@langchain/langgraph/remote';
2
4
 
3
5
  /**
4
6
  * Configuration options and helper callback methods for stream lifecycle events.
@@ -14,38 +16,108 @@ interface StreamCallbacks {
14
16
  onText?: (text: string) => Promise<void> | void;
15
17
  }
16
18
 
17
- type LangChainImageDetail = 'auto' | 'low' | 'high';
18
- type LangChainMessageContentText = {
19
- type: 'text';
20
- text: string;
21
- };
22
- type LangChainMessageContentImageUrl = {
23
- type: 'image_url';
24
- image_url: string | {
25
- url: string;
26
- detail?: LangChainImageDetail;
27
- };
28
- };
29
- type LangChainMessageContentComplex = LangChainMessageContentText | LangChainMessageContentImageUrl | (Record<string, any> & {
30
- type?: 'text' | 'image_url' | string;
31
- }) | (Record<string, any> & {
32
- type?: never;
33
- });
34
- type LangChainMessageContent = string | LangChainMessageContentComplex[];
35
- type LangChainAIMessageChunk = {
36
- content: LangChainMessageContent;
37
- };
38
- type LangChainStreamEvent = {
39
- event: string;
40
- data: any;
41
- };
42
19
  /**
43
- Converts LangChain output streams to an AI SDK Data Stream.
20
+ * Converts AI SDK UIMessages to LangChain BaseMessage objects.
21
+ *
22
+ * This function transforms the AI SDK's message format into LangChain's message
23
+ * format, enabling seamless integration between the two frameworks.
24
+ *
25
+ * @param messages - Array of AI SDK UIMessage objects to convert.
26
+ * @returns Promise resolving to an array of LangChain BaseMessage objects.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { toBaseMessages } from '@ai-sdk/langchain';
31
+ *
32
+ * const langchainMessages = await toBaseMessages(uiMessages);
33
+ *
34
+ * // Use with LangChain
35
+ * const response = await model.invoke(langchainMessages);
36
+ * ```
37
+ */
38
+ declare function toBaseMessages(messages: UIMessage[]): Promise<BaseMessage[]>;
39
+ /**
40
+ * Converts ModelMessages to LangChain BaseMessage objects.
41
+ *
42
+ * @param modelMessages - Array of ModelMessage objects from convertToModelMessages.
43
+ * @returns Array of LangChain BaseMessage objects.
44
+ */
45
+ declare function convertModelMessages(modelMessages: ModelMessage[]): BaseMessage[];
46
+ /**
47
+ * Converts a LangChain stream to an AI SDK UIMessageStream.
48
+ *
49
+ * This function automatically detects the stream type and handles both:
50
+ * - Direct model streams (AsyncIterable from `model.stream()`)
51
+ * - LangGraph streams (ReadableStream with `streamMode: ['values', 'messages']`)
52
+ *
53
+ * @param stream - A stream from LangChain model.stream() or LangGraph graph.stream().
54
+ * @param callbacks - Optional callbacks for stream lifecycle events.
55
+ * @returns A ReadableStream of UIMessageChunk objects.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * // With a direct model stream
60
+ * const model = new ChatOpenAI({ model: 'gpt-4o-mini' });
61
+ * const stream = await model.stream(messages);
62
+ * return createUIMessageStreamResponse({
63
+ * stream: toUIMessageStream(stream),
64
+ * });
65
+ *
66
+ * // With a LangGraph stream
67
+ * const graphStream = await graph.stream(
68
+ * { messages },
69
+ * { streamMode: ['values', 'messages'] }
70
+ * );
71
+ * return createUIMessageStreamResponse({
72
+ * stream: toUIMessageStream(graphStream),
73
+ * });
74
+ * ```
75
+ */
76
+ declare function toUIMessageStream(stream: AsyncIterable<AIMessageChunk> | ReadableStream, callbacks?: StreamCallbacks): ReadableStream<UIMessageChunk>;
44
77
 
45
- The following streams are supported:
46
- - `LangChainAIMessageChunk` streams (LangChain `model.stream` output)
47
- - `string` streams (LangChain `StringOutputParser` output)
78
+ /**
79
+ * Options for configuring a LangSmith deployment transport.
80
+ * Extends RemoteGraphParams but makes graphId optional (defaults to 'agent').
48
81
  */
49
- declare function toUIMessageStream(stream: ReadableStream<LangChainStreamEvent> | ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, callbacks?: StreamCallbacks): ReadableStream<UIMessageChunk>;
82
+ type LangSmithDeploymentTransportOptions = Omit<RemoteGraphParams, 'graphId'> & {
83
+ /**
84
+ * The ID of the graph to connect to.
85
+ * @default 'agent'
86
+ */
87
+ graphId?: string;
88
+ };
89
+ /**
90
+ * A ChatTransport implementation for LangSmith/LangGraph deployments.
91
+ *
92
+ * This transport enables seamless integration between the AI SDK's useChat hook
93
+ * and LangSmith deployed LangGraph agents.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import { LangSmithDeploymentTransport } from '@ai-sdk/langchain';
98
+ *
99
+ * // Use with useChat
100
+ * const { messages, input, handleSubmit } = useChat({
101
+ * transport: new LangSmithDeploymentTransport({
102
+ * url: 'https://your-deployment.us.langgraph.app',
103
+ * apiKey: 'my-api-key',
104
+ * }),
105
+ * });
106
+ * ```
107
+ */
108
+ declare class LangSmithDeploymentTransport<UI_MESSAGE extends UIMessage> implements ChatTransport<UI_MESSAGE> {
109
+ protected graph: RemoteGraph;
110
+ constructor(options: LangSmithDeploymentTransportOptions);
111
+ sendMessages(options: {
112
+ trigger: 'submit-message' | 'regenerate-message';
113
+ chatId: string;
114
+ messageId: string | undefined;
115
+ messages: UI_MESSAGE[];
116
+ abortSignal: AbortSignal | undefined;
117
+ } & ChatRequestOptions): Promise<ReadableStream<UIMessageChunk>>;
118
+ reconnectToStream(_options: {
119
+ chatId: string;
120
+ } & ChatRequestOptions): Promise<ReadableStream<UIMessageChunk> | null>;
121
+ }
50
122
 
51
- export { toUIMessageStream };
123
+ export { LangSmithDeploymentTransport, type LangSmithDeploymentTransportOptions, type StreamCallbacks, convertModelMessages, toBaseMessages, toUIMessageStream };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { UIMessageChunk } from 'ai';
1
+ import { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
2
+ import { UIMessage, UIMessageChunk, ModelMessage, ChatTransport, ChatRequestOptions } from 'ai';
3
+ import { RemoteGraph, RemoteGraphParams } from '@langchain/langgraph/remote';
2
4
 
3
5
  /**
4
6
  * Configuration options and helper callback methods for stream lifecycle events.
@@ -14,38 +16,108 @@ interface StreamCallbacks {
14
16
  onText?: (text: string) => Promise<void> | void;
15
17
  }
16
18
 
17
- type LangChainImageDetail = 'auto' | 'low' | 'high';
18
- type LangChainMessageContentText = {
19
- type: 'text';
20
- text: string;
21
- };
22
- type LangChainMessageContentImageUrl = {
23
- type: 'image_url';
24
- image_url: string | {
25
- url: string;
26
- detail?: LangChainImageDetail;
27
- };
28
- };
29
- type LangChainMessageContentComplex = LangChainMessageContentText | LangChainMessageContentImageUrl | (Record<string, any> & {
30
- type?: 'text' | 'image_url' | string;
31
- }) | (Record<string, any> & {
32
- type?: never;
33
- });
34
- type LangChainMessageContent = string | LangChainMessageContentComplex[];
35
- type LangChainAIMessageChunk = {
36
- content: LangChainMessageContent;
37
- };
38
- type LangChainStreamEvent = {
39
- event: string;
40
- data: any;
41
- };
42
19
  /**
43
- Converts LangChain output streams to an AI SDK Data Stream.
20
+ * Converts AI SDK UIMessages to LangChain BaseMessage objects.
21
+ *
22
+ * This function transforms the AI SDK's message format into LangChain's message
23
+ * format, enabling seamless integration between the two frameworks.
24
+ *
25
+ * @param messages - Array of AI SDK UIMessage objects to convert.
26
+ * @returns Promise resolving to an array of LangChain BaseMessage objects.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { toBaseMessages } from '@ai-sdk/langchain';
31
+ *
32
+ * const langchainMessages = await toBaseMessages(uiMessages);
33
+ *
34
+ * // Use with LangChain
35
+ * const response = await model.invoke(langchainMessages);
36
+ * ```
37
+ */
38
+ declare function toBaseMessages(messages: UIMessage[]): Promise<BaseMessage[]>;
39
+ /**
40
+ * Converts ModelMessages to LangChain BaseMessage objects.
41
+ *
42
+ * @param modelMessages - Array of ModelMessage objects from convertToModelMessages.
43
+ * @returns Array of LangChain BaseMessage objects.
44
+ */
45
+ declare function convertModelMessages(modelMessages: ModelMessage[]): BaseMessage[];
46
+ /**
47
+ * Converts a LangChain stream to an AI SDK UIMessageStream.
48
+ *
49
+ * This function automatically detects the stream type and handles both:
50
+ * - Direct model streams (AsyncIterable from `model.stream()`)
51
+ * - LangGraph streams (ReadableStream with `streamMode: ['values', 'messages']`)
52
+ *
53
+ * @param stream - A stream from LangChain model.stream() or LangGraph graph.stream().
54
+ * @param callbacks - Optional callbacks for stream lifecycle events.
55
+ * @returns A ReadableStream of UIMessageChunk objects.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * // With a direct model stream
60
+ * const model = new ChatOpenAI({ model: 'gpt-4o-mini' });
61
+ * const stream = await model.stream(messages);
62
+ * return createUIMessageStreamResponse({
63
+ * stream: toUIMessageStream(stream),
64
+ * });
65
+ *
66
+ * // With a LangGraph stream
67
+ * const graphStream = await graph.stream(
68
+ * { messages },
69
+ * { streamMode: ['values', 'messages'] }
70
+ * );
71
+ * return createUIMessageStreamResponse({
72
+ * stream: toUIMessageStream(graphStream),
73
+ * });
74
+ * ```
75
+ */
76
+ declare function toUIMessageStream(stream: AsyncIterable<AIMessageChunk> | ReadableStream, callbacks?: StreamCallbacks): ReadableStream<UIMessageChunk>;
44
77
 
45
- The following streams are supported:
46
- - `LangChainAIMessageChunk` streams (LangChain `model.stream` output)
47
- - `string` streams (LangChain `StringOutputParser` output)
78
+ /**
79
+ * Options for configuring a LangSmith deployment transport.
80
+ * Extends RemoteGraphParams but makes graphId optional (defaults to 'agent').
48
81
  */
49
- declare function toUIMessageStream(stream: ReadableStream<LangChainStreamEvent> | ReadableStream<LangChainAIMessageChunk> | ReadableStream<string>, callbacks?: StreamCallbacks): ReadableStream<UIMessageChunk>;
82
+ type LangSmithDeploymentTransportOptions = Omit<RemoteGraphParams, 'graphId'> & {
83
+ /**
84
+ * The ID of the graph to connect to.
85
+ * @default 'agent'
86
+ */
87
+ graphId?: string;
88
+ };
89
+ /**
90
+ * A ChatTransport implementation for LangSmith/LangGraph deployments.
91
+ *
92
+ * This transport enables seamless integration between the AI SDK's useChat hook
93
+ * and LangSmith deployed LangGraph agents.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import { LangSmithDeploymentTransport } from '@ai-sdk/langchain';
98
+ *
99
+ * // Use with useChat
100
+ * const { messages, input, handleSubmit } = useChat({
101
+ * transport: new LangSmithDeploymentTransport({
102
+ * url: 'https://your-deployment.us.langgraph.app',
103
+ * apiKey: 'my-api-key',
104
+ * }),
105
+ * });
106
+ * ```
107
+ */
108
+ declare class LangSmithDeploymentTransport<UI_MESSAGE extends UIMessage> implements ChatTransport<UI_MESSAGE> {
109
+ protected graph: RemoteGraph;
110
+ constructor(options: LangSmithDeploymentTransportOptions);
111
+ sendMessages(options: {
112
+ trigger: 'submit-message' | 'regenerate-message';
113
+ chatId: string;
114
+ messageId: string | undefined;
115
+ messages: UI_MESSAGE[];
116
+ abortSignal: AbortSignal | undefined;
117
+ } & ChatRequestOptions): Promise<ReadableStream<UIMessageChunk>>;
118
+ reconnectToStream(_options: {
119
+ chatId: string;
120
+ } & ChatRequestOptions): Promise<ReadableStream<UIMessageChunk> | null>;
121
+ }
50
122
 
51
- export { toUIMessageStream };
123
+ export { LangSmithDeploymentTransport, type LangSmithDeploymentTransportOptions, type StreamCallbacks, convertModelMessages, toBaseMessages, toUIMessageStream };