vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.59k stars 1.41k forks source link

`onFinish` is never called in `useChat` args #1248

Closed rob-gordon closed 2 months ago

rob-gordon commented 6 months ago

Description

Currently running a bit of code in the onFinish handler of useChat. It's never called. The streaming works just fine but the handler isn't called.

Here you can see the specific line where we would get a console log if it were running: https://github.com/Frostwork-Media/back-of-the-envelope/blob/19a3e1fdf3d24b36206b01fcc1c1c9088c88b4db/src/app/_components/Chat.tsx#L30-L31

You can test it for yourself at https://backoftheenvelope.app/ or pull the code and try locally.

Code example

const {
    messages,
    input,
    handleInputChange,
    handleSubmit,
    isLoading,
    setMessages,
    reload,
  } = useChat({
    onFinish() {
      console.log("onFinish() ran.");
    },
});

Additional context

Running Next 14, App Router, Served on vercel, running with bun Repository: https://github.com/Frostwork-Media/back-of-the-envelope Public URL: https://backoftheenvelope.app/

chanmathew commented 5 months ago

I'm having the same issue on 3.0.22, Sveltekit. onFinish and onResponse is not firing at all.

Downgraded to 3.0.19 and that seems to work.

hu-qi commented 5 months ago

I'm having the same issue on 3.0.22, Sveltekit. onFinish and onResponse is not firing at all.

Downgraded to 3.0.19 and that seems to work.

In my case, the same issue on 3.0.22, and 3.0.19 is work.
Ask for help: are there any incompatible changes?

lgrammel commented 5 months ago

This is potentially a duplicate of https://github.com/vercel/ai/issues/1316

You can check out https://github.com/vercel/ai/issues/1316#issuecomment-2048346351 for upgrade instructions.

myudak commented 4 months ago

me too the onFinish in nextjs 14 doesnt work