Commit 70d3980
fix(ai): use errorMode 'text' in approval continuation to preserve tool error messages (#13056)
## 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:
```ts
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:
```ts
// 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: {} }`.
---------
Co-authored-by: Dmitrii Troitskii <jsleitor@gmail.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>1 parent a676c69 commit 70d3980
File tree
5 files changed
+244
-2
lines changed- .changeset
- packages/ai/src/generate-text
5 files changed
+244
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7564 | 7564 | | |
7565 | 7565 | | |
7566 | 7566 | | |
| 7567 | + | |
| 7568 | + | |
| 7569 | + | |
| 7570 | + | |
| 7571 | + | |
| 7572 | + | |
| 7573 | + | |
| 7574 | + | |
| 7575 | + | |
| 7576 | + | |
| 7577 | + | |
| 7578 | + | |
| 7579 | + | |
| 7580 | + | |
| 7581 | + | |
| 7582 | + | |
| 7583 | + | |
| 7584 | + | |
| 7585 | + | |
| 7586 | + | |
| 7587 | + | |
| 7588 | + | |
| 7589 | + | |
| 7590 | + | |
| 7591 | + | |
| 7592 | + | |
| 7593 | + | |
| 7594 | + | |
| 7595 | + | |
| 7596 | + | |
| 7597 | + | |
| 7598 | + | |
| 7599 | + | |
| 7600 | + | |
| 7601 | + | |
| 7602 | + | |
| 7603 | + | |
| 7604 | + | |
| 7605 | + | |
| 7606 | + | |
| 7607 | + | |
| 7608 | + | |
| 7609 | + | |
| 7610 | + | |
| 7611 | + | |
| 7612 | + | |
| 7613 | + | |
| 7614 | + | |
| 7615 | + | |
| 7616 | + | |
| 7617 | + | |
| 7618 | + | |
| 7619 | + | |
| 7620 | + | |
| 7621 | + | |
| 7622 | + | |
| 7623 | + | |
| 7624 | + | |
| 7625 | + | |
| 7626 | + | |
| 7627 | + | |
| 7628 | + | |
| 7629 | + | |
| 7630 | + | |
| 7631 | + | |
| 7632 | + | |
| 7633 | + | |
| 7634 | + | |
| 7635 | + | |
| 7636 | + | |
| 7637 | + | |
| 7638 | + | |
| 7639 | + | |
| 7640 | + | |
| 7641 | + | |
| 7642 | + | |
| 7643 | + | |
| 7644 | + | |
| 7645 | + | |
| 7646 | + | |
| 7647 | + | |
| 7648 | + | |
| 7649 | + | |
| 7650 | + | |
| 7651 | + | |
| 7652 | + | |
| 7653 | + | |
| 7654 | + | |
| 7655 | + | |
| 7656 | + | |
| 7657 | + | |
| 7658 | + | |
| 7659 | + | |
| 7660 | + | |
| 7661 | + | |
| 7662 | + | |
| 7663 | + | |
| 7664 | + | |
| 7665 | + | |
| 7666 | + | |
| 7667 | + | |
| 7668 | + | |
| 7669 | + | |
| 7670 | + | |
| 7671 | + | |
| 7672 | + | |
| 7673 | + | |
| 7674 | + | |
| 7675 | + | |
| 7676 | + | |
| 7677 | + | |
| 7678 | + | |
| 7679 | + | |
| 7680 | + | |
7567 | 7681 | | |
7568 | 7682 | | |
7569 | 7683 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
595 | 595 | | |
596 | 596 | | |
597 | 597 | | |
598 | | - | |
| 598 | + | |
599 | 599 | | |
600 | 600 | | |
601 | 601 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20705 | 20705 | | |
20706 | 20706 | | |
20707 | 20707 | | |
| 20708 | + | |
| 20709 | + | |
| 20710 | + | |
| 20711 | + | |
| 20712 | + | |
| 20713 | + | |
| 20714 | + | |
| 20715 | + | |
| 20716 | + | |
| 20717 | + | |
| 20718 | + | |
| 20719 | + | |
| 20720 | + | |
| 20721 | + | |
| 20722 | + | |
| 20723 | + | |
| 20724 | + | |
| 20725 | + | |
| 20726 | + | |
| 20727 | + | |
| 20728 | + | |
| 20729 | + | |
| 20730 | + | |
| 20731 | + | |
| 20732 | + | |
| 20733 | + | |
| 20734 | + | |
| 20735 | + | |
| 20736 | + | |
| 20737 | + | |
| 20738 | + | |
| 20739 | + | |
| 20740 | + | |
| 20741 | + | |
| 20742 | + | |
| 20743 | + | |
| 20744 | + | |
| 20745 | + | |
| 20746 | + | |
| 20747 | + | |
| 20748 | + | |
| 20749 | + | |
| 20750 | + | |
| 20751 | + | |
| 20752 | + | |
| 20753 | + | |
| 20754 | + | |
| 20755 | + | |
| 20756 | + | |
| 20757 | + | |
| 20758 | + | |
| 20759 | + | |
| 20760 | + | |
| 20761 | + | |
| 20762 | + | |
| 20763 | + | |
| 20764 | + | |
| 20765 | + | |
| 20766 | + | |
| 20767 | + | |
| 20768 | + | |
| 20769 | + | |
| 20770 | + | |
| 20771 | + | |
| 20772 | + | |
| 20773 | + | |
| 20774 | + | |
| 20775 | + | |
| 20776 | + | |
| 20777 | + | |
| 20778 | + | |
| 20779 | + | |
| 20780 | + | |
| 20781 | + | |
| 20782 | + | |
| 20783 | + | |
| 20784 | + | |
| 20785 | + | |
| 20786 | + | |
| 20787 | + | |
| 20788 | + | |
| 20789 | + | |
| 20790 | + | |
| 20791 | + | |
| 20792 | + | |
| 20793 | + | |
| 20794 | + | |
| 20795 | + | |
| 20796 | + | |
| 20797 | + | |
| 20798 | + | |
| 20799 | + | |
| 20800 | + | |
| 20801 | + | |
| 20802 | + | |
| 20803 | + | |
| 20804 | + | |
| 20805 | + | |
| 20806 | + | |
| 20807 | + | |
| 20808 | + | |
| 20809 | + | |
| 20810 | + | |
| 20811 | + | |
| 20812 | + | |
| 20813 | + | |
| 20814 | + | |
| 20815 | + | |
| 20816 | + | |
| 20817 | + | |
| 20818 | + | |
| 20819 | + | |
| 20820 | + | |
| 20821 | + | |
| 20822 | + | |
| 20823 | + | |
| 20824 | + | |
| 20825 | + | |
| 20826 | + | |
| 20827 | + | |
| 20828 | + | |
| 20829 | + | |
| 20830 | + | |
20708 | 20831 | | |
20709 | 20832 | | |
20710 | 20833 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1451 | 1451 | | |
1452 | 1452 | | |
1453 | 1453 | | |
1454 | | - | |
| 1454 | + | |
1455 | 1455 | | |
1456 | 1456 | | |
1457 | 1457 | | |
| |||
0 commit comments