Issue Summary
When using the Vercel AI SDK, the tool invocation via streamText
fails by returning "Oops, an error occurred!"
, while the same tool invocation works correctly with generateText
.
Code Structure
-
Successful
generateText
Call- Result: The tool invocation works correctly. The
execute
function runs as expected and returns the desired result.
- Result: The tool invocation works correctly. The
-
Failing
streamText
Call- Result: The tool invocation fails with the error message
"Oops, an error occurred!"
.
- Result: The tool invocation fails with the error message
Key Code Snippets
Working generateText
Invocation
const result = await generateText({
model: myProvider.languageModel('chat-model'),
tools: {
getWeather: {
description: 'Get the weather for a location',
parameters: z.object({
city: z.string().describe('The city to get the weather for'),
unit: z.enum(['C', 'F']).describe('The unit to display the temperature in'),
}),
execute: async ({ city, unit }) => {
const weather = { value: 24, description: 'Sunny' };
console.log('Weather:', city, unit, weather);
return `It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!`;
},
},
},
prompt: 'How is the weather in San Francisco?',
});
console.log('Result:', JSON.stringify(result.response.messages));
This call successfully invokes the tool and returns the expected weather details.
Failing streamText
Invocation
const result = streamText({
model: myProvider.languageModel(selectedChatModel),
system: systemPrompt({ selectedChatModel }),
messages,
maxSteps: 5,
experimental_generateMessageId: generateUUID,
tools: {
getWeather: {
description: 'Get the weather for a location',
parameters: z.object({
city: z.string().describe('The city to get the weather for'),
unit: z.enum(['C', 'F']).describe('The unit to display the temperature in'),
}),
execute: async ({ city, unit }) => {
const weather = { value: 24, description: 'Sunny' };
console.log('Weather:', city, unit, weather);
return `It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!`;
},
},
},
onFinish: async ({ response }) => {
console.log('Stream completed:', response);
},
onError: () => {
return 'Oops, an error occurred!';
},
});
result.consumeStream();
result.mergeIntoDataStream(dataStream, { sendReasoning: true });
This call results in the error message
"Oops, an error occurred!"
, and the tool’s execute
function does not seem to be triggered.
Debugging Steps Taken
-
Tool Configuration Consistency
- The
tools
configuration used in bothgenerateText
andstreamText
is identical.
- The
-
Logging in the
execute
Function- Debug logging shows that in
generateText
, theexecute
function is triggered and logs the correct details. InstreamText
, it appears that theexecute
function is not being invoked.
- Debug logging shows that in
-
Simplifying
streamText
Configuration- Even with minimal configuration (only the
getWeather
tool enabled), the issue persists.
- Even with minimal configuration (only the
-
Reviewing SDK Documentation and GitHub Issues
- No official documentation or GitHub issues were found that directly explain this discrepancy.
Questions and Points of Clarification
-
Tool Invocation Difference:
Why doesgenerateText
successfully call the tool, whilestreamText
fails? Is there a difference in how these two functions handle tool invocations? -
Tool Activation Configuration:
Should additional configuration (e.g.,experimental_activeTools
) be enabled forstreamText
to properly recognize and invoke tools? -
Known SDK Issues:
Is there a known issue with the Vercel AI SDK regarding tool invocation instreamText
mode?
I’m seeking community assistance to understand why the tool invocation behaves differently between generateText
and streamText
, and how to configure or debug streamText
so that it can successfully invoke tools.
Any insights or suggestions would be greatly appreciated! Thank you.