Skip to content
skyl

Streaming

A pull iterator that composes with defer, and cannot leak a goroutine.

Client.Stream delivers a response incrementally. skyl models it as a pull iterator rather than a channel, and that single choice determines most of what this chapter covers.

In this chapter

  • The four-method Stream interface and the loop that goes with it
  • Why stream.Err() after the loop is not optional
  • The four event types, and which providers emit which
  • How to drain a stream into one Response when you do not want events
  • How cancellation works, and why no stream can leak
  • Why a stream that dies halfway is reported rather than hidden

The shape#

goCompiles
stream, err := client.Stream(ctx, req)
if err != nil {
	return err
}
defer stream.Close()

for stream.Next() {
	if ev := stream.Event(); ev.Type == skyl.EventTextDelta {
		fmt.Print(ev.Text)
	}
}

return stream.Err()
stream, err := client.Stream(ctx, req)
if err != nil {
	return err
}
defer stream.Close()

for stream.Next() {
	if ev := stream.Event(); ev.Type == skyl.EventTextDelta {
		fmt.Print(ev.Text)
	}
}

return stream.Err()

Read Your First Stream for what each method does and the two lines people forget.

Read more →

Events#

ConstantValueMeaning
EventTextDeltatext_deltaThe next fragment of assistant text, in StreamEvent.Text. The event most callers want.
EventThinkingDeltathinking_deltaA fragment of the model's reasoning, when the provider discloses it. Only Anthropic ever emits this.
EventToolCalltool_callA completed tool call. skyl buffers partial arguments and emits this once, when the call's JSON is whole — a half-parsed tool call is not actionable.
EventDonedoneThe final event of a successful stream, carrying Usage and StopReason.

Read Stream Events for which adapters emit which, and why tool calls arrive whole.

Read more →

Collecting#

goCompiles
resp, err := skyl.CollectStream(stream, "openai", "gpt-5.6")
resp, err := skyl.CollectStream(stream, "openai", "gpt-5.6")

Read Collecting a Stream for when this is the right tool and what it costs you.

Read more →

Cancellation#

Read Cancellation and Cleanup for why abandoning a stream is safe, and why Close still matters.

Read more →

Tool calls#

Read Streaming Tool Calls — skyl reassembles fragmented arguments so you never see half a call.

Read more →

Truncation#

Read Truncated Streams for the failure that used to look exactly like success.

Read more →

What’s next?

Start with Your First Stream. If you already stream and are chasing a bug where answers are occasionally cut short, go straight to Truncated Streams.

Edit this page on GitHub