fix: re-throw AbortError in generateText multi-step tool loop#13205
Open
MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
Open
fix: re-throw AbortError in generateText multi-step tool loop#13205MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
Conversation
Fixes vercel#12878. generateText() silently swallows AbortError when an AbortSignal fires mid-generation in a multi-step tool-use loop. The root cause is in executeToolCall(), which catches ALL errors (including AbortError) and converts them into tool-error results. This means the abort is never propagated to the caller. Changes: - executeToolCall.ts: Check for AbortError before converting to tool-error result. If the error is an AbortError, record it on the span and re-throw so it propagates correctly. - generate-text.ts: Check mergedAbortSignal.aborted at the start of each loop iteration. This catches the edge case where the signal fires between steps (after tool execution completes but before the next doGenerate() call).
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.
Note: This PR was authored by Claude (AI), operated by @MaxwellCalkin.
Fixes #12878
Summary
generateText()silently swallowsAbortErrorwhen anAbortSignalfires mid-generation in a multi-step tool-use loop. Instead of throwing, it returns a normal result, and the caller has no indication the generation was interrupted.Root cause
In
executeToolCall(), thecatch (error)block catches all errors — includingAbortError— and converts them intotool-errorresults. This means when a tool'sexecutefunction is aborted (or when an abort happens during tool execution), the error is swallowed and returned as a normal tool-error result. Thedo...whileloop ingenerateText()then either continues (and may hit an AbortError on the nextdoGenerate()call) or terminates normally if thewhilecondition evaluates tofalse.Changes
packages/ai/src/generate-text/execute-tool-call.ts: AddedisAbortErrorcheck at the top of thecatchblock. If the error is an AbortError, it is recorded on the telemetry span and re-thrown immediately, rather than being converted to atool-errorresult. This is consistent with how the retry logic inretry-with-exponential-backoff.tsalready handles AbortErrors (line 98:if (isAbortError(error)) { throw error; }).packages/ai/src/generate-text/generate-text.ts: AddedmergedAbortSignal?.abortedcheck at the start of eachdo...whileloop iteration. This catches the edge case where the signal fires between steps — after tool execution completes but before the nextdoGenerate()call would detect it.Test plan
generateText()with anAbortSignalthat fires during tool execution in a multi-step loop, and confirm it now throws anAbortErrorinstead of returning normallytool-errorresults as before