vercel / ai

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

`streamText` tool invocation does not return the result for Node v20.10.0 #3263

Open chozzz opened 6 days ago

chozzz commented 6 days ago

Description

First of all, make sure that you're running on Node v20.10.0, then follow this step:

  1. Clone Vercel AI Chatbot, set it up and get it running locally.
  2. Run in dev mode pnpm dev
  3. Tap on the default suggested action for What is the weather in San Fancisco?
  4. Issue should be reproducible by now, and following is what you're seeing.

image

Once the form is back submittable, you can send any text and the API would return this error;

 ⨯ AI_MessageConversionError: ToolInvocation must have a result: {"state":"call","toolCallId":"call_SAOMz3pWx9TpOZvqFswmxA5F","toolName":"getWeather","args":{"latitude":37.7749,"longitude":-122.4194}}

Code example

const result = await streamText({
    model: customModel,
    system:
      "you are a friendly assistant! keep your responses concise and helpful.",
    messages: coreMessages,
    maxSteps: 5,
    tools: {
      getWeather: {
        description: "Get the current weather at a location",
        parameters: z.object({
          latitude: z.number(),
          longitude: z.number(),
        }),
        execute: async ({ latitude, longitude }) => {
          const response = await fetch(
            `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`,
          );

          const weatherData = await response.json();
          return weatherData;
        },
      },
    },
    onFinish: async ({ responseMessages }) => {
      if (session.user && session.user.id) {
        try {
          await saveChat({
            id,
            messages: [...coreMessages, ...responseMessages],
            userId: session.user.id,
          });
        } catch (error) {
          console.error("Failed to save chat");
        }
      }
    },
    experimental_telemetry: {
      isEnabled: true,
      functionId: "stream-text",
    },
  });

  return result.toDataStreamResponse({});

Additional context

For additional context, this issue does not seem to be reproducible for me on Node v18.19.0. I am not sure about the other node versions yet.

lgrammel commented 12 hours ago

Hi @chozzz this looks like an issue for the Vercel AI Chatbot repo. Have you filed it there?