`toUIMessageStreamResponse` also includes tool calls - how to filter out?

Hi all, I'm using AI SDK 5 with NextJS. When I call toUIMessageStreamResponse in /chat endpoint, the function returns all data, like messages and tool calls. As I want to build a public agent, I only want to return text messages, but not tool calls. How can I achieve this? I followed this tutorial here, so I guess my setup is fine: https://mastra.ai/en/examples/agents/ai-sdk-v5-integration This is the code that is relevant:
export async function POST(req: Request) {
const { messages, id } = (await req.json()) as {
messages: UIMessage[];
id: string;
};
const testAgent = mastra.getAgent('testAgent');
const agentResult = await testAgent.stream(messages, {
format: 'aisdk',
memory: {
thread: id,
resource: 'test',
},
});
return agentResult.toUIMessageStreamResponse();
}
export async function POST(req: Request) {
const { messages, id } = (await req.json()) as {
messages: UIMessage[];
id: string;
};
const testAgent = mastra.getAgent('testAgent');
const agentResult = await testAgent.stream(messages, {
format: 'aisdk',
memory: {
thread: id,
resource: 'test',
},
});
return agentResult.toUIMessageStreamResponse();
}
installed packages:
"@ai-sdk/openai": "^2.0.52",
"@ai-sdk/react": "^2.0.75",
"@mastra/braintrust": "^0.1.7",
"@mastra/core": "^0.21.1",
"@mastra/libsql": "^0.15.2",
"@mastra/loggers": "^0.10.16",
"@mastra/memory": "^0.15.7",
"ai": "^5.0.75",
"@ai-sdk/openai": "^2.0.52",
"@ai-sdk/react": "^2.0.75",
"@mastra/braintrust": "^0.1.7",
"@mastra/core": "^0.21.1",
"@mastra/libsql": "^0.15.2",
"@mastra/loggers": "^0.10.16",
"@mastra/memory": "^0.15.7",
"ai": "^5.0.75",
6 Replies
hefe.woiza
hefe.woizaOP5d ago
I check the implementation, and I guess the reason why all data is included in the toUIMessageStreamResponse is, that it is using fullStream: https://github.com/mastra-ai/mastra/blob/b6a492903f63650e9b7e9d6ce95c232f24a4ce45/packages/core/src/stream/aisdk/v5/output.ts#L121
Mastra Triager
GitHub
[DISCORD:1428677895936807026] toUIMessageStreamResponse also incl...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1428677895936807026 Hi all, I'm using AI SDK 5 with NextJS. When I call toUIMessageStreamResponse in /...
Ward
Ward4d ago
@hefe.woiza why not only filter it out on the frontend? If not you could write a custom transformer in your post to clean the stream
Guria
Guria4d ago
why not only filter it out on the frontend
because:
As I want to build a public agent,
Some implementation details must be hidden I believe
hefe.woiza
hefe.woizaOP4d ago
exactly. also, the tool is calling a rag api which is returning quite some data and i don't want to have that massive traffic

Did you find this page helpful?