Closed
Description
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