fix(ai): propagate AbortError in generateText multi-step tool loop#12915
Open
sleitor wants to merge 1 commit intovercel:mainfrom
Open
fix(ai): propagate AbortError in generateText multi-step tool loop#12915sleitor wants to merge 1 commit intovercel:mainfrom
sleitor wants to merge 1 commit intovercel:mainfrom
Conversation
2 tasks
When an AbortSignal fires between steps (e.g. during tool execution), some providers return a partial result with finishReason:'other/unknown' instead of throwing. The multi-step do-while loop now checks mergedAbortSignal.aborted at the start of each iteration and re-throws the signal reason, ensuring callers reliably receive an AbortError rather than a silent partial result. Fixes vercel#12878
0ef3389 to
d57e70e
Compare
Contributor
Author
|
👋 Gentle ping — just checking if this is still on the radar for review. Happy to address any feedback or rebase if needed! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #12878
When an
AbortSignalfires between steps in a multi-step tool loop (e.g. during tool execution), some providers return a partial result withfinishReason: 'other'instead of throwing. ThegenerateTextfunction silently returned the partial result with no error, giving callers no indication the generation was interrupted.Root Cause
The
do { ... } while (...)loop ingenerate-text.tspasses themergedAbortSignalto each model call and tool execution, but it never checks whether the signal was already aborted between iterations. If a provider happens to return rather than throw on an aborted request, the loop continues into the next step as if nothing happened.Fix
Add an abort guard at the top of each loop iteration:
This ensures that regardless of how the provider handled the abort,
generateTextalways throws anAbortErrorwhen the signal is fired — consistent with standard Web API abort semantics.Test
Added a test that:
executefunctiongenerateTextthrows anAbortErrorand the second model call is never madeAlso updated the existing
should forward abort signal to tool executiontest: its previous pattern (aborting externally thenawait generateTextPromise) relied on the function silently absorbing the abort, which is the bug we're fixing. The revised test simply verifies the abort signal is forwarded to the toolexecutecallback, which is its actual intent.