Skip to content

Conversation

@EurFelux
Copy link
Contributor

@EurFelux EurFelux commented Dec 27, 2025

Background

Issue #11442 requests abort reason visibility on abort chunks so callers can distinguish manual vs timeout aborts when combining signals.

Summary

  • Attach AbortSignal.reason to abort chunks in streamText, and extend stream/UI chunk types and schemas to carry reason.
  • Add tests covering abort reason propagation and update affected snapshots.
  • Add an example under examples/ai-core/src/stream-text demonstrating abort reasons with AbortSignal.any (manual vs timeout).
  • Document the optional reason field on abort stream parts in core and UI stream protocol docs.

Manual Verification

  • Ran pnpm -C /Volumes/CIRCLE/dev/ai/packages/ai exec tsx /Volumes/CIRCLE/dev/ai/examples/ai-core/src/stream-text/openai-compatible-abort-reason.ts and observed abort reasons for both manual and timeout cases.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

Fixes #11442

"warnings": [],
},
{
"reason": [AbortError: This operation was aborted],
Copy link
Collaborator

Choose a reason for hiding this comment

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

this isn't json serializable. is there way to make the reason a string?

}),
z.strictObject({
type: z.literal('abort'),
reason: z.unknown().optional(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

can this be an optional string instead to ensure that it is serializable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You’re right; it should be a serializable value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed it to an optional string.

Pass through abort reason when provided in abort signals to improve debugging and user feedback. Includes updates to types, schemas, and tests to support the new field.
Add documentation for the abort part in stream protocol and include optional reason field in stream-text reference
Use getErrorMessage to serialize reason for safe JSON transmission in stream text results
Remove redundant error type check for abort reason and streamline environment variable naming for Deepseek API
@EurFelux EurFelux requested a review from lgrammel January 6, 2026 12:37
Comment on lines 1016 to 1019
// Use getErrorMessage to serialize reason (unknown) to string for safe JSON transmission
...(abortSignal?.reason !== undefined
? { reason: getErrorMessage(abortSignal.reason) }
: {}),
Copy link
Collaborator

Choose a reason for hiding this comment

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

getErrorMessage seems incorrect here. Even if it constrains the functionality, would it be possible to check if abortSignal?.reason is a string and ignore it otherwise? (i.e. change the condition in 1017 such that we know that the reason is a string)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can’t just accept a string only. Please see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason: by default, the reason field is set to a value of type DOMException, not a string. In fact, you can set it to any value using the abort function, which is why I previously designed the field’s type as unknown. Given that this is the default behavior, if we only accepted string, we would lose the reason in most use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used getErrorMessage for the sake of code reuse, since this function can serialize errors of type unknown. I’m also aware that the fact that reason is likely an Error instance isn’t immediately obvious from its name, which might confuse maintainers who aren’t familiar with this behavior. To address this, I added a comment explaining why I’m using this function, but it seems the explanation wasn’t clear enough. I’ll provide a more precise explanation of this behavior in the comment.

@lgrammel lgrammel merged commit 3a73fb3 into vercel:main Jan 7, 2026
17 of 18 checks passed
@EurFelux EurFelux deleted the feat/abort-reason branch January 8, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: provide abort reason for abort chunk

2 participants