Skip to content

fix(ai): use errorMode 'text' in approval continuation to preserve tool error messages#13056

Merged
aayush-kapoor merged 2 commits intovercel:mainfrom
sleitor:fix-13048
Mar 4, 2026
Merged

fix(ai): use errorMode 'text' in approval continuation to preserve tool error messages#13056
aayush-kapoor merged 2 commits intovercel:mainfrom
sleitor:fix-13048

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Mar 4, 2026

Problem

When a tool with needsApproval throws an error during execution, the error message is silently lost. The model receives {} instead of the actual error text, causing it to hallucinate success.

Closes #13048

Root Cause

The approval continuation path in both streamText and generateText used:

errorMode: output.type === 'tool-error' ? 'json' : 'none'

errorMode: 'json' routes to createToolModelOutput which does toJSONValue(error)JSON.stringify(new Error('msg'))'{}' because Error properties (message, stack) are non-enumerable.

The normal multi-step flow and to-response-messages.ts correctly use errorMode: 'text' which calls getErrorMessage(error) to extract the message string.

Fix

Change 'json''text' in both locations to align with the normal flow:

// Before (broken):
errorMode: output.type === 'tool-error' ? 'json' : 'none'

// After (fixed):
errorMode: output.type === 'tool-error' ? 'text' : 'none'

Tests

Added test cases for both generateText and streamText that verify when an approved needsApproval tool throws, the model receives { type: 'error-text', value: 'tool execution failed' } instead of { type: 'error-json', value: {} }.

…ion flow

When a tool with needsApproval throws an error during execution, the error
message was silently lost. The approval continuation path in both streamText
and generateText used errorMode: 'text' ? 'json' : 'none' (bug: 'json'
should be 'text'). JSON.stringify(new Error('msg')) returns '{}' because
Error properties are non-enumerable, so the model received '{}' instead of
the actual error message.

The fix aligns the approval continuation path with the normal multi-step
flow and to-response-messages.ts which correctly use errorMode: 'text',
causing getErrorMessage() to extract the message string.

Fixes vercel#13048
@tigent tigent bot added ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented labels Mar 4, 2026
haroldfabla2-hue pushed a commit to haroldfabla2-hue/ai that referenced this pull request Mar 4, 2026
When a tool with needsApproval throws an error during execution, the error
message was being lost because errorMode 'json' routes to createToolModelOutput
which does JSON.stringify(new Error('msg')) resulting in '{}' since Error
properties are non-enumerable.

This fix changes 'json' to 'text' in both generateText and streamText to
align with the normal multi-step flow that correctly uses getErrorMessage.

Closes vercel#13056
@aayush-kapoor aayush-kapoor self-assigned this Mar 4, 2026
Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just made some minor test fixes + added changeset

@aayush-kapoor aayush-kapoor merged commit 70d3980 into vercel:main Mar 4, 2026
17 of 18 checks passed
@sleitor
Copy link
Copy Markdown
Contributor Author

sleitor commented Mar 4, 2026

@aayush-kapoor thank you! :) yeah.. changeset this is painfull for me hahah. everytime forgot it..

@sleitor sleitor deleted the fix-13048 branch March 4, 2026 22:06
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Mar 4, 2026

🚀 Published in:

Package Version
ai 6.0.113
@ai-sdk/angular 2.0.114
@ai-sdk/langchain 2.0.119
@ai-sdk/llamaindex 2.0.113
@ai-sdk/react 3.0.115
@ai-sdk/rsc 2.0.113
@ai-sdk/svelte 4.0.113
@ai-sdk/vue 3.0.113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool errors silently lost for tools with needsApproval (approval continuation flow uses errorMode 'json' on Error objects)

2 participants