Skip to documentation
Documentation navigation

Documentation navigation

Documentation / guides

Text deltas

Render TextChunk values incrementally and fold them into a TextBlock.

developer

TextChunk carries one provider-emitted text fragment. The fragment may be empty and still counts as a received chunk; the core accumulator preserves that distinction through Empty.

API surface

type TextChunk struct{ Text string }

var accumulated streamaccumulator.Text
accumulated.Add(&content.TextChunk{Text: "Hello, "})
accumulated.Add(&content.TextChunk{Text: "world!"})
block := accumulated.Block()

block is nil until at least one chunk is added. Afterwards it is a *content.TextBlock whose Text is the concatenation in arrival order.

for {
	chunk, err := reader.Next()
	if errors.Is(err, io.EOF) {
		break
	}
	if err != nil {
		return err
	}
	if text, ok := chunk.(*content.TextChunk); ok {
		fmt.Print(text.Text) // live display
		accumulated.Add(text) // final block
	}
}

The accumulator does not send events, validate output, or decide whether the turn failed. Those policies stay in the caller or loop.

Proof

Related: TextBlock, Accumulate a response.

← back to documentation