streamText Tool Invocation Failure

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

  1. Successful generateText Call :white_check_mark:

    • Result: The tool invocation works correctly. The execute function runs as expected and returns the desired result.
  2. Failing streamText Call :cross_mark:

    • Result: The tool invocation fails with the error message "Oops, an error occurred!".

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));

:white_check_mark: 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 });

:cross_mark: 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

  1. Tool Configuration Consistency :white_check_mark:

    • The tools configuration used in both generateText and streamText is identical.
  2. Logging in the execute Function :white_check_mark:

    • Debug logging shows that in generateText, the execute function is triggered and logs the correct details. In streamText, it appears that the execute function is not being invoked.
  3. Simplifying streamText Configuration :white_check_mark:

    • Even with minimal configuration (only the getWeather tool enabled), the issue persists.
  4. Reviewing SDK Documentation and GitHub Issues :white_check_mark:

    • No official documentation or GitHub issues were found that directly explain this discrepancy.

Questions and Points of Clarification

  • Tool Invocation Difference:
    Why does generateText successfully call the tool, while streamText 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 for streamText to properly recognize and invoke tools?

  • Known SDK Issues:
    Is there a known issue with the Vercel AI SDK regarding tool invocation in streamText 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.

I found the problem, it was because of the tool-call format returned by the model. The tool-call format returned by the Qwen/QwQ-32B I used was as follows:

{
index: 0,
id: '0195ea05b2065bf6a2cf2b8a25e3716d',
type: 'function',
function: { name: 'add', arguments: '' }
}
// Subsequent chunks
{ index: 0, id: '', type: '', function: { arguments: ' {"' } }
{ index: 0, id: '', type: '', function: { arguments: 'a' } }
{ index: 0, id: '', type: '', function: { arguments: '":' } }
1 Like

Thanks for sharing the solution @jessyan0913.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.