[▲ Vercel Community](/) · [Categories](/categories) · [Latest](/latest) · [Top](/top) · [Live](/live)

[AI SDK](/c/ai-sdk/62)

# streamText Tool Invocation Failure

788 views · 1 like · 3 posts


Jessyan0913 (@jessyan0913) · 2025-03-27

### **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** ✅  
   - **Result:** The tool invocation works correctly. The `execute` function runs as expected and returns the desired result.

2. **Failing `streamText` Call** ❌  
   - **Result:** The tool invocation fails with the error message `"Oops, an error occurred!"`.

---

### **Key Code Snippets**

#### **Working `generateText` Invocation**
```typescript
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**
```typescript
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.**

![image|368x499](upload://mUxHWRs0e9k0bHFBMQTx2yubwBx.png)


---

### **Debugging Steps Taken**

1. **Tool Configuration Consistency** ✅  
   - The `tools` configuration used in both `generateText` and `streamText` is identical.

2. **Logging in the `execute` Function** ✅  
   - 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** ✅  
   - Even with minimal configuration (only the `getWeather` tool enabled), the issue persists.

4. **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 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.


Jessyan0913 (@jessyan0913) · 2025-04-01 · ♥ 1

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:
```json
{
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: '":' } }
```


Anshuman Bhardwaj (@anshumanb) · 2025-04-01

Thanks for sharing the solution @jessyan0913.