Skip to content

Commit c9fbc57

Browse files
authored
chore(providers/langchain): extract to separate package (#5928)
1 parent 4e4ef43 commit c9fbc57

File tree

26 files changed

+234
-124
lines changed

26 files changed

+234
-124
lines changed

.changeset/spotty-swans-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/langchain': patch
3+
---
4+
5+
chore(providers/langchain): extract to separate package

content/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: LangChainAdapter
3-
description: API Reference for LangChainAdapter.
2+
title: @ai-sdk/langchain Adapter
3+
description: API Reference for the LangChain Adapter.
44
---
55

6-
# `LangChainAdapter`
6+
# `@ai-sdk/langchain`
77

8-
The `LangChainAdapter` module provides helper functions to transform LangChain output streams into data streams and data stream responses.
8+
The `@ai-sdk/langchain` module provides helper functions to transform LangChain output streams into data streams and data stream responses.
99
See the [LangChain Adapter documentation](/providers/adapters/langchain) for more information.
1010

1111
It supports:
@@ -16,7 +16,10 @@ It supports:
1616

1717
## Import
1818

19-
<Snippet text={`import { LangChainAdapter } from "ai"`} prompt={false} />
19+
<Snippet
20+
text={`import { toDataStreamResponse } from "@ai-sdk/langchain"`}
21+
prompt={false}
22+
/>
2023

2124
## API Signature
2225

@@ -49,7 +52,7 @@ It supports:
4952

5053
```tsx filename="app/api/completion/route.ts" highlight={"14"}
5154
import { ChatOpenAI } from '@langchain/openai';
52-
import { LangChainAdapter } from 'ai';
55+
import { toDataStreamResponse } from '@ai-sdk/langchain';
5356

5457
export async function POST(req: Request) {
5558
const { prompt } = await req.json();
@@ -61,15 +64,15 @@ export async function POST(req: Request) {
6164

6265
const stream = await model.stream(prompt);
6366

64-
return LangChainAdapter.toDataStreamResponse(stream);
67+
return toDataStreamResponse(stream);
6568
}
6669
```
6770

6871
### Convert StringOutputParser Stream
6972

7073
```tsx filename="app/api/completion/route.ts" highlight={"16"}
7174
import { ChatOpenAI } from '@langchain/openai';
72-
import { LangChainAdapter } from 'ai';
75+
import { toDataStreamResponse } from '@ai-sdk/langchain';
7376
import { StringOutputParser } from '@langchain/core/output_parsers';
7477

7578
export async function POST(req: Request) {
@@ -83,6 +86,6 @@ export async function POST(req: Request) {
8386
const parser = new StringOutputParser();
8487
const stream = await model.pipe(parser).stream(prompt);
8588

86-
return LangChainAdapter.toDataStreamResponse(stream);
89+
return toDataStreamResponse(stream);
8790
}
8891
```

content/docs/07-reference/04-stream-helpers/16-langchain-stream.mdx

-85
This file was deleted.

content/docs/07-reference/04-stream-helpers/index.mdx

-6
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ collapsed: true
8181
"Transforms the response from LangChain's language models into a readable stream.",
8282
href: '/docs/reference/stream-helpers/langchain-stream',
8383
},
84-
{
85-
title: 'LangChainAdapter',
86-
description:
87-
"Transforms the response from LangChain's stream into data streams.",
88-
href: '/docs/reference/stream-helpers/langchain-adapter',
89-
},
9084
{
9185
title: 'LlamaIndexAdapter',
9286
description:

content/providers/04-adapters/01-langchain.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ However, LangChain does not provide a way to easily build UIs or a standard way
1313

1414
Here is a basic example that uses both the AI SDK and LangChain together with the [Next.js](https://nextjs.org/docs) App Router.
1515

16-
The AI SDK [`LangChainAdapter`](/docs/reference/stream-helpers/langchain-adapter) uses the result from [LangChain ExpressionLanguage streaming](https://js.langchain.com/docs/expression_language/streaming) to pipe text to the client.
17-
`LangChainAdapter.toDataStreamResponse()` is compatible with the LangChain Expression Language `.stream()` function response.
16+
The [`@ai-sdk/langchain` package](/docs/reference/stream-helpers/langchain-adapter) uses the result from [LangChain ExpressionLanguage streaming](https://js.langchain.com/docs/expression_language/streaming) to pipe text to the client.
17+
`toDataStreamResponse()` is compatible with the LangChain Expression Language `.stream()` function response.
1818

1919
```tsx filename="app/api/completion/route.ts" highlight={"16"}
2020
import { ChatOpenAI } from '@langchain/openai';
21-
import { LangChainAdapter } from 'ai';
21+
import { toDataStreamResponse } from '@ai-sdk/langchain';
2222

2323
export const maxDuration = 60;
2424

@@ -32,7 +32,7 @@ export async function POST(req: Request) {
3232

3333
const stream = await model.stream(prompt);
3434

35-
return LangChainAdapter.toDataStreamResponse(stream);
35+
return toDataStreamResponse(stream);
3636
}
3737
```
3838

examples/next-langchain/app/api/chat/route.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ChatOpenAI } from '@langchain/openai';
2-
import { LangChainAdapter, Message } from 'ai';
2+
import { Message } from 'ai';
33
import { AIMessage, HumanMessage } from '@langchain/core/messages';
4+
import { toDataStreamResponse } from '@ai-sdk/langchain';
45

56
// Allow streaming responses up to 30 seconds
67
export const maxDuration = 30;
@@ -25,5 +26,5 @@ export async function POST(req: Request) {
2526
),
2627
);
2728

28-
return LangChainAdapter.toDataStreamResponse(stream);
29+
return toDataStreamResponse(stream);
2930
}

examples/next-langchain/app/api/completion-stream-data/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { toDataStreamResponse } from '@ai-sdk/langchain';
12
import { ChatOpenAI } from '@langchain/openai';
2-
import { LangChainAdapter } from 'ai';
33

44
// Allow streaming responses up to 30 seconds
55
export const maxDuration = 30;
@@ -14,5 +14,5 @@ export async function POST(req: Request) {
1414

1515
const stream = await model.stream(prompt);
1616

17-
return LangChainAdapter.toDataStreamResponse(stream);
17+
return toDataStreamResponse(stream);
1818
}

examples/next-langchain/app/api/completion-string-output-parser/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { toDataStreamResponse } from '@ai-sdk/langchain';
12
import { StringOutputParser } from '@langchain/core/output_parsers';
23
import { ChatOpenAI } from '@langchain/openai';
3-
import { LangChainAdapter } from 'ai';
44

55
// Allow streaming responses up to 30 seconds
66
export const maxDuration = 30;
@@ -17,5 +17,5 @@ export async function POST(req: Request) {
1717

1818
const stream = await model.pipe(parser).stream(prompt);
1919

20-
return LangChainAdapter.toDataStreamResponse(stream);
20+
return toDataStreamResponse(stream);
2121
}

examples/next-langchain/app/api/completion/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { toDataStreamResponse } from '@ai-sdk/langchain';
12
import { ChatOpenAI } from '@langchain/openai';
2-
import { LangChainAdapter } from 'ai';
33

44
// Allow streaming responses up to 30 seconds
55
export const maxDuration = 30;
@@ -14,5 +14,5 @@ export async function POST(req: Request) {
1414

1515
const stream = await model.stream(prompt);
1616

17-
return LangChainAdapter.toDataStreamResponse(stream);
17+
return toDataStreamResponse(stream);
1818
}

examples/next-langchain/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@ai-sdk/react": "workspace:*",
13+
"@ai-sdk/langchain": "workspace:*",
1314
"@langchain/openai": "0.0.28",
1415
"@langchain/core": "0.1.63",
1516
"ai": "workspace:*",

examples/next-langchain/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
},
4646
{
4747
"path": "../../packages/react"
48+
},
49+
{
50+
"path": "../../packages/langchain"
4851
}
4952
]
5053
}

packages/ai/internal/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@ export { prepareRetries } from '../core/prompt/prepare-retries';
44
export { prepareCallSettings } from '../core/prompt/prepare-call-settings';
55
export { convertToLanguageModelPrompt } from '../core/prompt/convert-to-language-model-prompt';
66
export { calculateLanguageModelUsage } from '../core/types/usage';
7+
export { formatDataStreamPart } from '../core';
8+
export { type DataStreamWriter } from '../core/data-stream/data-stream-writer';
9+
export { mergeStreams } from '../core/util/merge-streams';
10+
export { prepareResponseHeaders } from '../core/util/prepare-response-headers';
11+
export {
12+
createCallbacksTransformer,
13+
type StreamCallbacks,
14+
} from '../streams/stream-callbacks';
15+
export { StreamData } from '../streams/stream-data';
716

817
export * from '../util/constants';

packages/ai/streams/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from '../core/index';
22
export * from '../errors/index';
33

4-
export * as LangChainAdapter from './langchain-adapter';
54
export * as LlamaIndexAdapter from './llamaindex-adapter';
65
export * from './stream-data';

packages/langchain/CHANGELOG.md

Whitespace-only changes.

packages/langchain/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AI SDK - LangChain Adapter
2+
3+
This package contains a LangChain adapter for the AI SDK.

packages/langchain/package.json

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "@ai-sdk/langchain",
3+
"version": "1.0.0-canary.0",
4+
"license": "Apache-2.0",
5+
"sideEffects": false,
6+
"main": "./dist/index.js",
7+
"module": "./dist/index.mjs",
8+
"types": "./dist/index.d.ts",
9+
"files": [
10+
"dist/**/*",
11+
"CHANGELOG.md"
12+
],
13+
"scripts": {
14+
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
15+
"build:watch": "pnpm clean && tsup --watch",
16+
"clean": "rm -rf dist *.tsbuildinfo",
17+
"lint": "eslint \"./**/*.ts*\"",
18+
"type-check": "tsc --build",
19+
"prettier-check": "prettier --check \"./**/*.ts*\"",
20+
"test": "pnpm test:node && pnpm test:edge",
21+
"test:update": "pnpm test:node -u",
22+
"test:watch": "vitest --config vitest.node.config.js",
23+
"test:edge": "vitest --config vitest.edge.config.js --run",
24+
"test:node": "vitest --config vitest.node.config.js --run"
25+
},
26+
"exports": {
27+
"./package.json": "./package.json",
28+
".": {
29+
"types": "./dist/index.d.ts",
30+
"import": "./dist/index.mjs",
31+
"require": "./dist/index.js"
32+
}
33+
},
34+
"dependencies": {
35+
"ai": "workspace:*",
36+
"@ai-sdk/provider-utils": "workspace:*"
37+
},
38+
"devDependencies": {
39+
"@types/node": "20.17.24",
40+
"@vercel/ai-tsconfig": "workspace:*",
41+
"tsup": "^8",
42+
"typescript": "5.8.3"
43+
},
44+
"engines": {
45+
"node": ">=18"
46+
},
47+
"publishConfig": {
48+
"access": "public"
49+
},
50+
"homepage": "https://sdk.vercel.ai/docs",
51+
"repository": {
52+
"type": "git",
53+
"url": "git+https://github.com/vercel/ai.git"
54+
},
55+
"bugs": {
56+
"url": "https://github.com/vercel/ai/issues"
57+
},
58+
"keywords": [
59+
"ai"
60+
]
61+
}

packages/langchain/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './langchain-adapter';

packages/ai/streams/langchain-adapter.test.ts renamed to packages/langchain/src/langchain-adapter.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
convertReadableStreamToArray,
44
convertResponseStreamToArray,
55
} from '@ai-sdk/provider-utils/test';
6-
import { createDataStream } from '../core/data-stream/create-data-stream';
6+
import { createDataStream } from 'ai';
77
import {
88
mergeIntoDataStream,
99
toDataStream,

packages/ai/streams/langchain-adapter.ts renamed to packages/langchain/src/langchain-adapter.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { formatDataStreamPart } from '../core';
2-
import { DataStreamWriter } from '../core/data-stream/data-stream-writer';
3-
import { mergeStreams } from '../core/util/merge-streams';
4-
import { prepareResponseHeaders } from '../core/util/prepare-response-headers';
1+
import { formatDataStreamPart, DataStreamWriter, StreamData } from 'ai';
52
import {
3+
mergeStreams,
4+
prepareResponseHeaders,
65
createCallbacksTransformer,
76
StreamCallbacks,
8-
} from './stream-callbacks';
9-
import { StreamData } from './stream-data';
7+
} from 'ai/internal';
108

119
type LangChainImageDetail = 'auto' | 'low' | 'high';
1210

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
// Disable project configuration for tsup builds
5+
"composite": false
6+
}
7+
}

0 commit comments

Comments
 (0)