Skip to content

Commit 250edbd

Browse files
vercel-ai-sdk[bot]KirschXgr2m
authored
Backport: feat(ui): add finishReason in useChat onFinish callback (#10126)
This is an automated backport of #9857 to the release-v5.0 branch. FYI @KirschX --------- Co-authored-by: KirschX <[email protected]> Co-authored-by: Gregor Martynus <[email protected]>
1 parent 9401f06 commit 250edbd

File tree

14 files changed

+261
-799
lines changed

14 files changed

+261
-799
lines changed

.changeset/six-roses-peel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ai-sdk/react': patch
3+
'ai': patch
4+
---
5+
6+
Added finishReason on useChat onFinish callbck

content/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ Allows you to easily create a conversational user interface for your chatbot app
275275
type: 'boolean',
276276
description: `True if errors during streaming caused the response to stop early.`,
277277
},
278+
{
279+
name: 'finishReason',
280+
type: "'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other' | 'unknown'",
281+
isOptional: true,
282+
description:
283+
'The reason why the model finished generating the response. Undefined if the finish reason was not provided by the model.',
284+
},
278285
],
279286
},
280287
],

examples/next-openai/app/use-chat-data-ui-parts/page.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import ChatInput from '@/components/chat-input';
44
import { useChat } from '@ai-sdk/react';
5-
import { DefaultChatTransport, UIMessage } from 'ai';
5+
import { DefaultChatTransport, UIMessage, type FinishReason } from 'ai';
6+
import { useState } from 'react';
67

78
type MyMessage = UIMessage<
89
never,
@@ -16,6 +17,9 @@ type MyMessage = UIMessage<
1617
>;
1718

1819
export default function Chat() {
20+
const [lastFinishReason, setLastFinishReason] = useState<
21+
FinishReason | undefined
22+
>(undefined);
1923
const { error, status, sendMessage, messages, regenerate, stop } =
2024
useChat<MyMessage>({
2125
transport: new DefaultChatTransport({
@@ -24,6 +28,9 @@ export default function Chat() {
2428
onData: dataPart => {
2529
console.log('dataPart', JSON.stringify(dataPart, null, 2));
2630
},
31+
onFinish: ({ finishReason }) => {
32+
setLastFinishReason(finishReason);
33+
},
2734
});
2835

2936
return (
@@ -94,6 +101,12 @@ export default function Chat() {
94101
</div>
95102
)}
96103

104+
{messages.length > 0 && (
105+
<div className="mt-4 text-gray-500">
106+
Finish reason: {String(lastFinishReason)}
107+
</div>
108+
)}
109+
97110
<ChatInput status={status} onSubmit={text => sendMessage({ text })} />
98111
</div>
99112
);

packages/ai/src/generate-text/__snapshots__/stream-text.test.ts.snap

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ exports[`streamText > result.pipeUIMessageStreamToResponse > should mask error m
433433
"data: {"type":"finish-step"}
434434
435435
",
436-
"data: {"type":"finish"}
436+
"data: {"type":"finish","finishReason":"error"}
437437
438438
",
439439
"data: [DONE]
@@ -456,7 +456,7 @@ exports[`streamText > result.pipeUIMessageStreamToResponse > should support cust
456456
"data: {"type":"finish-step"}
457457
458458
",
459-
"data: {"type":"finish"}
459+
"data: {"type":"finish","finishReason":"error"}
460460
461461
",
462462
"data: [DONE]
@@ -579,6 +579,7 @@ exports[`streamText > result.toUIMessageStream > should mask error messages by d
579579
"type": "finish-step",
580580
},
581581
{
582+
"finishReason": "error",
582583
"type": "finish",
583584
},
584585
]
@@ -624,6 +625,7 @@ exports[`streamText > result.toUIMessageStream > should send tool call, tool cal
624625
"type": "finish-step",
625626
},
626627
{
628+
"finishReason": "stop",
627629
"type": "finish",
628630
},
629631
]
@@ -645,6 +647,7 @@ exports[`streamText > result.toUIMessageStream > should support custom error mes
645647
"type": "finish-step",
646648
},
647649
{
650+
"finishReason": "error",
648651
"type": "finish",
649652
},
650653
]
@@ -664,7 +667,7 @@ exports[`streamText > result.toUIMessageStreamResponse > should mask error messa
664667
"data: {"type":"finish-step"}
665668
666669
",
667-
"data: {"type":"finish"}
670+
"data: {"type":"finish","finishReason":"error"}
668671
669672
",
670673
"data: [DONE]
@@ -687,7 +690,7 @@ exports[`streamText > result.toUIMessageStreamResponse > should support custom e
687690
"data: {"type":"finish-step"}
688691
689692
",
690-
"data: {"type":"finish"}
693+
"data: {"type":"finish","finishReason":"error"}
691694
692695
",
693696
"data: [DONE]

0 commit comments

Comments
 (0)