stream

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: BSD-2-Clause Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package stream reads the streamed responses of the Google LLM services: the server-sent events of one request, bounded by how long the model may go quiet for, with a request that never began answering re-issued once.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithRetry

func WithRetry(req *http.Request, cfg Config, onData func(ctx context.Context, data string) error) error

WithRetry issues req and hands the data of each event of its streamed response to onData, bounding how long the model may go quiet for.

A request the API accepts and then never answers is the case this exists for: nothing closes such a stream, so without a bound the turn stays open and the caller waits on a bot that will never speak. With RetryOnTimeout set, a first event that does not arrive in time re-issues the request once instead, which is safe only up to that point: an event that has been handed on is already downstream, so re-issuing after one would say everything twice.

A stream the bound cuts short ends with an error wrapping llm.ErrCompletionTimeout. onData is given the attempt's context, which the bound cancels.

Called from a task, WithRetry lets go of the session's loop only while it waits for the network: for the response to the request, and for each read of its body. Begin, Retrying and onData run on the loop, so onData may push frames.

Types

type Config

type Config struct {
	// Client sends the requests.
	Client *http.Client
	// IdleTimeout bounds the gap between two events, and the wait for the first
	// one when the request is not retried. Zero waits indefinitely.
	IdleTimeout time.Duration
	// RetryOnTimeout re-issues the request once when its first event does not
	// arrive within RetryTimeout.
	RetryOnTimeout bool
	// RetryTimeout is how long the first event is waited for before the request
	// is re-issued, when RetryOnTimeout is set.
	RetryTimeout time.Duration
	// Begin is called as each attempt is sent, which is where a service starts
	// measuring the time to its first byte. Nil does nothing.
	Begin func()
	// Retrying is called before a stalled request is re-issued, for the service
	// to log it. Nil does nothing.
	Retrying func(ctx context.Context)
	// StatusError is the error a response with a status other than 200 ends the
	// stream with. It must not close the body. It is called off the session's
	// loop, since it reads the body, so it must do nothing else.
	StatusError func(resp *http.Response) error
}

Config says how a streamed request is bounded and retried, and what the service reading it does around each attempt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL