Align the Vercel UI message stream with the data stream protocol - #906
Merged
Conversation
- Rename `toVercelProtocolLine()` to `toVercelProtocolFormat()` for clarity - Distinguish between `StreamErrorException` (already surfaced by provider) and unexpected errors (should be reported) - Flatten usage object in protocol to match Vercel AI SDK schema: `inputTokens`, `outputTokens`, `totalTokens`, `reasoningTokens`, `cachedInputTokens` - Add test for unexpected exceptions after error parts
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.
Currently the Vercel protocol stream drops information the AI SDK client is built to consume. A completed run looks like this on the wire:
And when a provider fails after streaming has started, the connection just drops, so
useChatsits on a dead spinner with no error state.After this PR, the same run emits every part the client processes, validated field by field against the
UIMessageChunkschema invercel/ai:On the client that means
message.metadata.usageis populated,finishReasonlands in the chat state, and multi-step tool runs get proper step boundaries (start-stepalso makes the client insert itsstep-startparts, andfinish-stepresets its active text and reasoning state between steps, which guards against text block id reuse across steps).A mid-stream failure now closes the stream properly instead of severing it:
The exception is still reported to the handler, it just no longer escapes into an already-open response.