Skip to content

TranscriptText(stream:) in a LazyVStack never shows text that arrives after the row starts empty #12

Done

Description

@servercimen

Problem

A TranscriptText(stream:) that is empty when a LazyVStack lays it out never shows the text that arrives later. Its TranscriptTextView is never added to the window, so the row stays empty.

A likely cause, not proven: the stream sends its text straight to the UIView, without a SwiftUI update. If the lazy stack has not created that view, nothing tells SwiftUI that the row grew.

Repro 1: an empty row

struct EmptyStreamRepro: View {
    @State private var stream = TranscriptStream()

    var body: some View {
        ScrollView {
            LazyVStack(alignment: .leading) {
                TranscriptText(stream: stream)
            }
            .padding()
        }
        .task {
            try? await Task.sleep(for: .seconds(1))
            stream.append("Hello **world**. This text arrives one second after the view.")
            stream.finish()
        }
    }
}

Result: no text shows. No TranscriptTextView is in the window, and the scroll content is 32 pt tall, which is only the padding.

These variants show the text:

  • VStack instead of LazyVStack.
  • .frame(minHeight: 1) on the TranscriptText.
  • TranscriptText(document) with an empty @State document that gets its document a second later.

Repro 2: an answer row added at the end of a long list

struct InsertedAnswerRepro: View {
    @State private var answering = false
    @State private var stream = TranscriptStream()

    var body: some View {
        ScrollView {
            LazyVStack(alignment: .leading, spacing: 16) {
                ForEach(0..<50, id: \.self) { _ in
                    Color.gray.opacity(0.2).frame(height: 2432)
                }
                if answering {
                    TranscriptText(stream: stream)
                        .frame(minHeight: 20, alignment: .top)
                        .onAppear { print("answer row appeared") }
                }
            }
            .padding(16)
        }
        .defaultScrollAnchor(.bottom)
        .task {
            try? await Task.sleep(for: .seconds(2))
            answering = true
            for _ in 0..<40 {
                stream.append("More of the answer arrives, with **bold** text and a [link](https://example.com). ")
                try? await Task.sleep(for: .milliseconds(100))
            }
            stream.finish()
        }
    }
}

Result: the list is scrolled to the bottom, but the answer row never appears. onAppear never runs, and the screen stays blank. Here .frame(minHeight: 20) does not help.

The result is the same with rows 1,400 pt tall. It is also the same with 50 TranscriptText rows from the benchmark's rich corpus (Corpus.history(messages: 50, rich: true)) in place of the gray rows. With the plain corpus (Corpus.history(messages: 50)), the answer row appears.

These variants show the answer:

  • Text instead of TranscriptText(stream:), with the text in @State.
  • TranscriptText(document), with the document in @State and parsed again for each chunk.

Environment

iPhone 17 Pro, iOS 27.0.1, Xcode 27.2, TranscriptKit 73bc58b, optimised build. Each case ran 1 to 3 times, with the same result every time.

Possible fixes

  • Give an empty TranscriptText(stream:) the height of one line, as Text("") has. This fixes repro 1 only.
  • Tell SwiftUI when the streamed text's height changes, also when the view is not in the window. For example, TranscriptText could observe the stream's height and update the row's size through SwiftUI.

Activity

  1. added
    bug
    on Nov 20, 2026
  2. servercimen commented on Nov 20, 2026

    @servercimen
    OwnerAuthor

    Status after #15, checked on an iPhone 12 mini with main at 1afd010:

    Case Result
    Repro 1: an empty row, with text a second later shown in 2 of 2 runs
    Repro 2: an answer after 50 rows of 2,432 pt shown in 2 of 2 runs
    Repro 2: an answer after 50 TranscriptText rows from the rich corpus shown in 4 of 5 runs
    Repro 2: an answer after 50 rows of 1,400 pt not shown in 2 of 2 runs
    Repro 2 with rows of 1,400 pt, and the answer row after the LazyVStack shown in 2 of 2 runs

    The open case is the one that #15 describes. The lazy stack estimates the height of the new row and keeps the scroll offset at that estimate. The real row is then above the visible area, and SwiftUI does not update it. The failed run with rich rows had a different content height, so it is probably the same cause.

    Workaround: put the streaming row after the LazyVStack, not inside it.

  3. servercimen commented on Nov 24, 2026

    @servercimen
    OwnerAuthor

    A correction to my comment above. A trace on an iPhone 12 mini with iOS 27.0 shows a different cause. The lazy stack makes the answer row but does not place it. While the row is not placed, the view changes its own @State to ask SwiftUI to measure it again (#15). In the 1,400-point case, SwiftUI did not update or measure the row after it made it, while the text grew to 1,197 points. TranscriptText(document) with the document in the parent's @State showed the answer, because each parent update made SwiftUI update and measure the row.

    #30 makes the view that builds TranscriptText(stream:) update while the row is not placed. With #30, the 1,400-point case and the rich case each show the answer in 7 of 7 runs.

    One case stays open. When a row view of its own builds TranscriptText(stream:) in its body, the row can stay empty. SwiftUI Text that reads an observable model also stays empty in that layout. For that case, build the view in the ForEach closure, or put the streaming row after the LazyVStack.

  4. servercimen commented on Nov 24, 2026

    @servercimen
    OwnerAuthor

    Fixed by #30. The README says where to build TranscriptText(stream:) in a LazyVStack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions