Skip to content

Commit c3f8735

Browse files
vercel-ai-sdk[bot]A404codercaomingzhe
authored
Backport: docs(langchain): update adapter examples to use new stream helpers (#9033)
This is an automated backport of #9003 to the release-v5.0 branch. Co-authored-by: A404coder <[email protected]> Co-authored-by: caomingzhe <[email protected]>
1 parent 220b436 commit c3f8735

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ It supports:
5151
### Convert LangChain Expression Language Stream
5252

5353
```tsx filename="app/api/completion/route.ts" highlight={"14"}
54+
import { toUIMessageStream } from '@ai-sdk/langchain';
5455
import { ChatOpenAI } from '@langchain/openai';
55-
import { toDataStreamResponse } from '@ai-sdk/langchain';
56+
import { createUIMessageStreamResponse } from 'ai';
5657

5758
export async function POST(req: Request) {
5859
const { prompt } = await req.json();
@@ -64,16 +65,19 @@ export async function POST(req: Request) {
6465

6566
const stream = await model.stream(prompt);
6667

67-
return toDataStreamResponse(stream);
68+
return createUIMessageStreamResponse({
69+
stream: toUIMessageStream(stream),
70+
});
6871
}
6972
```
7073

7174
### Convert StringOutputParser Stream
7275

7376
```tsx filename="app/api/completion/route.ts" highlight={"16"}
74-
import { ChatOpenAI } from '@langchain/openai';
75-
import { toDataStreamResponse } from '@ai-sdk/langchain';
77+
import { toUIMessageStream } from '@ai-sdk/langchain';
7678
import { StringOutputParser } from '@langchain/core/output_parsers';
79+
import { ChatOpenAI } from '@langchain/openai';
80+
import { createUIMessageStreamResponse } from 'ai';
7781

7882
export async function POST(req: Request) {
7983
const { prompt } = await req.json();
@@ -84,8 +88,11 @@ export async function POST(req: Request) {
8488
});
8589

8690
const parser = new StringOutputParser();
91+
8792
const stream = await model.pipe(parser).stream(prompt);
8893

89-
return toDataStreamResponse(stream);
94+
return createUIMessageStreamResponse({
95+
stream: toUIMessageStream(stream),
96+
});
9097
}
9198
```

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ The [`@ai-sdk/langchain` package](/docs/reference/stream-helpers/langchain-adapt
1717
`toDataStreamResponse()` is compatible with the LangChain Expression Language `.stream()` function response.
1818

1919
```tsx filename="app/api/completion/route.ts" highlight={"16"}
20+
import { toUIMessageStream } from '@ai-sdk/langchain';
2021
import { ChatOpenAI } from '@langchain/openai';
21-
import { toDataStreamResponse } from '@ai-sdk/langchain';
22+
import { createUIMessageStreamResponse } from 'ai';
2223

23-
export const maxDuration = 60;
24+
// Allow streaming responses up to 30 seconds
25+
export const maxDuration = 30;
2426

2527
export async function POST(req: Request) {
2628
const { prompt } = await req.json();
@@ -32,7 +34,9 @@ export async function POST(req: Request) {
3234

3335
const stream = await model.stream(prompt);
3436

35-
return toDataStreamResponse(stream);
37+
return createUIMessageStreamResponse({
38+
stream: toUIMessageStream(stream),
39+
});
3640
}
3741
```
3842

0 commit comments

Comments
 (0)