onStepFinish not being called after stop()

The streamText onStepFinish and onFinish events are not triggered when useChat stop() is called.

Thanks for sharing this, @fazufi and welcome to the Vercel Community!

Could you share any reproducible example for us? I’ll pass it onto the team :smile:

1 Like

When I click the Stop Streaming button, the onStepFinish and onFinish callbacks are not triggered.
This issue has already been reported in November at the following link:

but no response has been provided yet.

may codes:
(NEXT.JS)

FE:

import { useChat } from 'ai/react'

const { messages, stop, append, ...restChat } = useChat({
    api: `/api/chat`,
  });

 <button onClick={() => stop()}>
       stop streaming
  </button>

BE (app/api/chat/route.ts):

  import { streamText } from "ai";

  const res = await streamText({
      model: openaiSdk("gpt-4o-2024-08-06"),
      maxTokens: 2000,
      temperature: 0,
      messages,
      onStepFinish: async () => {
        console.log("stepFinish triggered");
      },
      onFinish: async () => {
        console.log("finish triggered");
      },
    });

    return res.toDataStreamResponse();