Skip to content

Surface text stream status and handle stream read failures - #1508

Merged
1egoman merged 20 commits into
mainfrom
fix/textstream-error-status
Nov 22, 2026
Merged

1egoman merged 20 commits into
mainfrom
fix/textstream-error-status

Conversation

@1egoman

@1egoman 1egoman commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What

Today, when a participant disconnects while one of its text streams is still open, livekit-client errors the open reader with a DataStreamError (AbnormalEnd), by design.

However, setupTextStream and setupChat read each stream with from(reader).pipe(...).subscribe(next), which has no error handler. Any error that does surface is handled by this and ends up as an uncatchable global error.

For voice-agent apps, an error fires whenever the agent leaves mid-segment (interrupted farewell, process exit, crash). That happens often enough to be a nuisance and to clog up error reporting.

This issue was already attempted in #1426, which has been dormant for a few months. That attempt aimed to fix everything within setupTextStream, so it inherited a lot of extra complexity (e.g. attachments) and grew large. In this PR I've instead exposed the stream's status on the data itself (TextStreamData, ReceivedChatMessage, and the transcript messages from useSessionMessages). Any more complex error handling can then live in the hooks, not in the rx-heavy core.

Changes

  • Two new enums, each with Streaming, Complete and Failed. TextStreamStatus is for TextStreamData. ReceivedMessageStatus is for received messages (chat and transcripts); it doesn't reference text streams, since a message's status isn't tied to how it was carried. Both are re-exported from @livekit/components-react.
  • TextStreamData (useTextStream / useTranscriptions) and ReceivedChatMessage (useChat) are now discriminated unions on status. Failed entries also expose error, the error that ended the read.
  • Each stream subscription now has full next / complete / error handling:
    • next → Streaming, accumulating text as before.
    • complete (writer closed cleanly) → Complete; the text is whole.
    • error (any read failure) → Failed, keeping whatever text arrived. Non-Error values are wrapped in an Error. For chat messages with attachments, the received text is kept even if the stream fails before the attachments resolve.
    • A stream that never received a chunk adds no entry, whether it completes or fails. This avoids empty transcripts and empty chat bubbles.
    • In a merged transcription segment, only the reader that currently owns the entry can set it to Complete or Failed. An older reader ending later doesn't override the newer one's status or streamInfo.
  • Messages that are whole when produced are marked Complete: the local copy from send(), legacy data-packet chat messages, and the dataChannel chat send.
  • In the chat message list, only a text change to an already-finished message sets editTimestamp. New chunks of a streaming message and status-only re-emits no longer do, so received messages aren't shown as "edited".
  • useSessionMessages now carries status (and error) onto agent and user transcript messages.
  • No error type is special-cased. If a read errored, it's Failed. Any segment-level interpretation (e.g. treating a benign AbnormalEnd after a complete transcript as acceptable) is left to consumers.

Compatibility

TextStreamData and ReceivedChatMessage change from object types to unions. Each union keeps a status-less variant, so code that builds these objects without a status still type-checks. A downstream interface X extends ReceivedChatMessage stops compiling, though, so @livekit/components-core is bumped as minor.

Scope

Attachment byte streams are not given a status; only the text stream behind a chat message is tracked.

Tests

New tests drive setupTextStream and setupChat with readers that complete normally, error mid-stream, and error before emitting. They cover the Streaming → Complete transition, failed streams keeping their text and exposing the error, no entry for streams that fail before any chunk, text kept when a stream with attachments fails, send() returning a Complete message, no editTimestamp on status-only re-emits or new streaming chunks, older segment readers not overriding newer ones, and non-Error failures being wrapped. Nothing rethrows globally: the error-path tests fail on the unfixed code with the uncaught error. @livekit/components-core (132) and @livekit/components-react (36) suites pass; both packages typecheck and build, and the react api-check passes.

setupTextStream subscribed to each reader without an error handler, so
when a sender disconnected mid-stream the errored reader's DataStreamError
was reported by RxJS as an unhandled observable error — rethrown globally
from a timer and surfacing as an uncaught DataStreamError that no
application code could catch.

Give the subscribe full next/complete/error handling and model the result
on TextStreamData via a new `status` field ('pending' | 'complete' |
'failed'), with the error exposed on failed entries. A clean close marks a
stream complete; any read error marks it failed while keeping the text
accumulated so far. No error type is special-cased here — consumers such
as useTranscriptions decide what a partial or failed stream means.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
components-js-storybook-5kld Ready Ready Preview Nov 22, 2026 10:08pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7128cb6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@livekit/components-core Minor
@livekit/components-react Patch
@livekit/agents-ui Patch
@livekit/component-example-next Patch
@livekit/components-js-docs Patch
@livekit/component-docs-storybook Patch
@livekit/components-docs-gen Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
LiveKitRoom only 7.88 KB (0%)
LiveKitRoom with VideoConference 32.85 KB (+0.69% 🔺)
All exports 44.84 KB (+1% 🔺)

Split the inline union into internal TextStreamDataCommon plus
Pending/Complete/Failed variants and a Legacy (status?: never) member for
backward compatibility, unioned as the exported TextStreamData. Variants
stay unexported; only TextStreamData is public.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@1egoman 1egoman changed the title feat: surface text stream status and handle stream read failures Surface text stream status and handle stream read failures Nov 8, 2026
The chat text-stream handler in setupChat subscribed without an error
handler, so a sender disconnecting mid-message errored the reader and
RxJS rethrew it globally from a timer — the same uncaught DataStreamError
that useTextStream had. Give that subscribe an error handler too.

Every error is treated equally, with no special-casing by reason: text
streams surface the failure through status, and chat has no such channel
so it logs. The attachment byte-stream hang/leak is a separate failure
mode and remains a follow-up.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Rather than logging chat stream read errors, model them in the data the
same way TextStreamData does. ReceivedChatMessage becomes a discriminated
union with a status of 'pending' | 'complete' | 'failed' (error exposed on
failed), plus a legacy member with no status so the other construction
sites keep type-checking.

The chat text-stream subscribe now emits pending as the message streams,
complete on a clean close, and failed (keeping the text that arrived) when
the read errors — which also stops RxJS from rethrowing the error globally
as an uncaught exception. Every error is treated the same; no reason is
special-cased. LegacyReceivedChatMessage becomes a type intersection since
an interface cannot extend a union.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Without an explicit return type, `send`'s inferred type narrowed the
`const receivedChatMsg: ReceivedChatMessage` to the no-status (legacy)
member of the new discriminated union and emitted it expanded inline,
which broke the react api-extractor report. Annotating the return as
`Promise<ReceivedChatMessage>` restores the named reference.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@thomasyuill-livekit

thomasyuill-livekit commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

@1egoman The core fix works. The new error handlers stop the global rethrow.

Claude found a few follow-ups:

  1. Empty failed entries reach the UI. A stream that fails before its first chunk now adds an entry with empty text (textStream.ts:154, chat.ts:216). Chat shows an empty bubble. useSessionMessages makes an empty agentTranscript and fires MessageReceived for it.
  2. Chat messages with attachments lose their text on failure. mergeMap emits nothing until all attachments resolve, so lastMessage is still undefined when the reader errors. The failed message then has message: ''. No test covers this path.
  3. Messages from send(), the legacy decoder, and dataChannel.ts send have no status, but they are whole. I suggest status: 'complete' on them.
  4. The changeset says patch, but TextStreamData and ReceivedChatMessage change from object types to unions. Downstream interface X extends ReceivedChatMessage stops compiling. I suggest minor for core.
  5. The PR description is out of date. It says chat is out of scope and that useTranscriptions uses the status, but the branch changes chat and no React file. Also, useSessionMessages drops the transcript status.

Do you want my to address claude to fix these on this branch?

1egoman and others added 8 commits November 16, 2026 12:40
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A text or chat stream that errors before emitting anything now adds no
entry, matching how a stream that completes with no chunks is handled.
Previously it surfaced an empty failed entry, which rendered as an empty
chat bubble or agent transcript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts resolve

With attachments, mergeMap holds each chunk until an attachment resolves,
so a stream that failed first had no emitted message (or a stale one) and
its text was replaced by an empty string. Track the received text
separately and use it for the failed message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Messages from send(), the legacy data-packet decoder, and the
dataChannel chat send are whole when produced, so give them
status: Complete rather than leaving them on the status-less variant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Streams now re-emit their message on complete/fail to update status,
which the message-list merge treated as an edit, so every received
message showed "edited". Only set editTimestamp when the text changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TextStreamData and ReceivedChatMessage changed from object types to
unions, which breaks downstream `interface X extends ...`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
useSessionMessages built agent/user transcript messages from
TextStreamData but dropped its status, so session UIs couldn't tell a
transcript that was still streaming, complete, or cut off. Carry status
(and error, when failed) onto the transcript messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1egoman

1egoman commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Nice thanks for that list @thomasyuill-livekit! I went through and fixed them, plus one other issue that was discovered along the way:

One more issue that neither of you raised: received chat messages will now show "edited". Each streamed chat message is now emitted again on complete. The messagesObservable merge in chat.ts sets editTimestamp: value.timestamp whenever an id repeats, and ChatEntry shows "edited" whenever editTimestamp is set. Messages with more than one chunk already had this bug. But single-chunk messages, which is most typed chat, will now show "edited" too. The fix: don't set editTimestamp when only status changed, or only set it when message changed.

Let me know what you think of these updates and if there's anything else you think needs to be done to get this approved.

@thomasyuill-livekit

Copy link
Copy Markdown
Contributor

@1egoman Thanks, the fixes cover all five points. Claude reviewed the updates and found three small things. None of them blocks the merge.

  1. The "edited" fix only works for single-chunk messages. Each later chunk changes the text, so a multi-chunk message still gets editTimestamp and ChatEntry shows it as edited. This bug existed before the PR, but the description now says it is fixed. Change the wording or compare against the stream's own chunks.
  2. The same three-way status shape is defined three times: TextStreamData, ReceivedChatMessage, and TranscriptionStatusFields. One shared type would keep them in sync.
  3. In a merged transcription segment, complete and error copy streamInfo from whichever reader finishes, which can be an older one. Also, textStream.ts does not wrap non-Error values in Error like chat.ts does.

Review by Claude.

1egoman and others added 2 commits November 18, 2026 10:58
…-Error failures

In a merged transcription segment, an older reader that completed or
failed after a newer one took over would mark the whole segment
complete/failed and roll its streamInfo back. Ignore complete/error from
a reader that no longer owns the entry. Also wrap non-Error read
failures in an Error, as setupChat does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ving

Each later chunk of a multi-chunk message changes its text, which the
message-list merge treated as an edit. Only count a text change as an
edit once the existing message has finished streaming.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1egoman

1egoman commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@thomasyuill-livekit Sweet, would you be willing to approve this in its current state?

Per your latest comment, I have fixed 1 (8773082) and 3 (8d9300c). I have left 2 unaddressed - I think keeping the cases distinct in each place is more ideal, because the disciminated union branches being seperate facilutates new use case specific fields being added to each more effectively. So I think a a big union of { status: TextStreamStatus.Streaming, .. } | { status: TextStreamStatus.Complete } | { status: TextStreamStatus.Failed, .. } would be premature optimization.

Comment thread packages/core/src/messages/types.ts Resolved
1egoman and others added 2 commits November 23, 2026 11:04
A chat or transcript message's status describes the message's delivery,
not the text stream it happens to be carried on (locally sent and legacy
messages are Complete without any received stream). Use a
transport-neutral ReceivedMessageStatus on ReceivedChatMessage and the
transcript messages, keeping TextStreamStatus for TextStreamData, and map
between them in useSessionMessages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ReceivedChatMessage, the transcript messages, and TextStreamData are
re-exported from components-react, but the enums needed to narrow them
by status were not, so React users had to depend on components-core
directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

This PR bumps

  • @livekit/components-core: minor
  • @livekit/components-react: patch

Also bumped

  • @livekit/agents-ui: depends on @livekit/components-react
  • @livekit/components-js-docs: depends on @livekit/components-react

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1egoman
1egoman merged commit 370cbca into main Nov 22, 2026
6 checks passed
@1egoman
1egoman deleted the fix/textstream-error-status branch November 22, 2026 22:36

This branch was successfully deployed

1 active deployment
Preview — 3c5e8699 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants