-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(langchain): ensure message id consistency #11617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+189
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/langchain': patch | ||
| --- | ||
|
|
||
| fix(langchain): ensure message id consistency |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { toBaseMessages, toUIMessageStream } from '@ai-sdk/langchain'; | ||
| import { ChatOpenAI } from '@langchain/openai'; | ||
| import { createUIMessageStreamResponse, UIMessage } from 'ai'; | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| export const maxDuration = 30; | ||
|
|
||
| /** | ||
| * this configuration streams reasoning summaries before the final response (thus triggering the error) | ||
| */ | ||
| const model = new ChatOpenAI({ | ||
| model: 'gpt-5', | ||
| useResponsesApi: true, | ||
| reasoning: { effort: 'medium', summary: 'concise' }, | ||
| }); | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| const { messages }: { messages: UIMessage[] } = await req.json(); | ||
|
|
||
| const langchainMessages = await toBaseMessages(messages); | ||
| const stream = await model.stream(langchainMessages as never); | ||
|
|
||
| return createUIMessageStreamResponse({ | ||
| stream: toUIMessageStream(stream), | ||
| }); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error ? error.message : 'An unknown error occurred'; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 'use client'; | ||
|
|
||
| import { useChat } from '@ai-sdk/react'; | ||
| import { DefaultChatTransport } from 'ai'; | ||
| import { useMemo } from 'react'; | ||
| import { ChatContainer } from '../../components/chat-container'; | ||
| import { type CustomDataMessage } from '../types'; | ||
|
|
||
| export default function ReasoningChat() { | ||
| const transport = useMemo( | ||
| () => new DefaultChatTransport({ api: '/api/reasoning' }), | ||
| [], | ||
| ); | ||
|
|
||
| const { messages, sendMessage, status, error } = useChat<CustomDataMessage>({ | ||
| transport, | ||
| }); | ||
|
|
||
| return ( | ||
| <ChatContainer | ||
| title="Reasoning Example" | ||
| description={ | ||
| <> | ||
| Uses <code>ChatOpenAI</code> with OpenAI Responses API and reasoning | ||
| enabled. This streams reasoning summaries before the final response, | ||
| demonstrating the <code>@ai-sdk/langchain</code> adapter's | ||
| support for reasoning content. | ||
| </> | ||
| } | ||
| messages={messages} | ||
| onSend={text => sendMessage({ text })} | ||
| status={status} | ||
| error={error} | ||
| placeholder="Ask a question that requires reasoning..." | ||
| suggestions={[ | ||
| 'How many rs are in strawberry?', | ||
| 'What is 15% of 80?', | ||
| 'If I have 3 apples and give away half, how many do I have?', | ||
| ]} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.