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 streamText args #1841

Closed myudak closed 4 months ago

myudak commented 4 months ago

Description

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

i already copied from documentation and still doesn't work

Code example

import { openai } from '@ai-sdk/openai';
import { streamText, convertToCoreMessages } from 'ai';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: openai('gpt-4-turbo'),
    messages: convertToCoreMessages(messages),
    async onFinish({ text, toolCalls, toolResults, usage, finishReason }) {
      // implement your own storage logic:
      console.log("onFinish")
      await saveChat({ text, toolCalls, toolResults });
    },
  });

  return result.toAIStreamResponse();
}

Additional context

Running Next 14, App Router, Served on vercel, "ai": "^3.1.14",

lgrammel commented 4 months ago

onFinish was added in 3.1.15. Can you confirm which exact versions of ai and of @ai-sdk/openai you are using?

lgrammel commented 4 months ago

I just tested it locally and it works, so it's most likely a version or deployment issue.

myudak commented 4 months ago

onFinish was added in 3.1.15. Can you confirm which exact versions of ai and of @ai-sdk/openai you are using?

oh wow yeah i didnt know this must been the issue thank you

myudak commented 4 months ago

fixed it