Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keepLastMessageOnError does not keep last message on DataStream #3579

Open
Aarr0w opened this issue Nov 8, 2024 · 0 comments
Open

keepLastMessageOnError does not keep last message on DataStream #3579

Aarr0w opened this issue Nov 8, 2024 · 0 comments
Labels
ai/ui bug Something isn't working

Comments

@Aarr0w
Copy link

Aarr0w commented Nov 8, 2024

Description

"ai": "^3.4.10"
Even with keepLastMessageOnError: true, the user's last message disappears when an error is thrown from a tool in streamText
the route.ts api is returning streamText result.toDataStreamResponse()

Code example

const {
    input,
    handleInputChange,
    handleSubmit,
    messages,
    setMessages,
  } = useChat({
    id,
    api,
    keepLastMessageOnError: true,
    onError(error) {
      console.log(error);
      console.log(JSON.stringify(messages.at(-1)) // does not see previous user message
    },
  });
export async function POST(req: NextRequest) {
  const { messages } = (await req.json()) as {
    messages: Message[];
  };
  const dataStream = new StreamData();

    const result =  await streamText({
    	model: gpt4o,
    	messages: convertToCoreMessages(messages),
    	system: systemPrompt,

    	tools: {
      		getWeather: {
        		description: "gets weather info",
        		parameters: z.object({}),
        		execute: async function (args: any) {
          			// using AISDKError b/c regular Error does not show up in useChat onError(), same message behavior
          			throw new AISDKError({
            			name: "simulated error",
            			message: "user message disappears",
          			});
        		},
      		},
    });
    
 return result.toDataStreamResponse({data: dataStream});
}

Additional context

No response

@lgrammel lgrammel added bug Something isn't working ai/ui labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/ui bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants