wire

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package wire holds what provider adapters share to speak vendor wire protocols: HTTP transport and Server-Sent Events, error and stop-reason classification, stream block indexing, ProviderOptions merging, replay state, and token and data URL helpers. Vendor-specific mappings live in the provider packages, except those shared by several, such as internal/claude.

Index

Constants

View Source
const DefaultUserAgent = "litellm-go/0.1"

DefaultUserAgent identifies the SDK when a config sets no UserAgent.

View Source
const MaxErrorBody = 1 << 20

MaxErrorBody bounds how much of a failed response is read into the error.

Variables

This section is empty.

Functions

func APIKey

func APIKey(ctx context.Context, key string, fn func(context.Context) (string, error), required bool) (string, error)

APIKey resolves the key through fn when set. An empty key is an error only when required.

func AddTokenDetails

func AddTokenDetails(base *int, details ...*int) *int

AddTokenDetails adds optional, additive protocol details to a required base count. Protocol adapters use this only where omitted detail fields mean no increment.

func ApplyOptions

func ApplyOptions(body map[string]any, options map[string]any) error

ApplyOptions puts options, in sorted key order, into body.

func CheckOptions

func CheckOptions(options map[string]any, allowed []string) error

CheckOptions rejects keys outside allowed, in sorted order.

func Do

func Do(client litellm.HTTPClient, req *http.Request, provider, operation string) (*http.Response, error)

Do sends req. A transport failure becomes a network error and a non-2xx response an HTTP error with its body closed, as does a 2xx HTML page. On success the caller owns resp.Body.

func ErrorField

func ErrorField(provider string, raw json.RawMessage) error

ErrorField converts the error member of an OpenAI-style response body or stream chunk, which gateways such as OpenRouter send with HTTP 200, or returns nil when it is absent or null. A numeric code is the upstream HTTP status and is classified as one; otherwise the code, metadata.error_type or type is used.

func FinishReason

func FinishReason(raw string) litellm.FinishReason

FinishReason maps a vendor stop reason. An unrecognized value is FinishReasonOther; the caller keeps the original as FinishReasonRaw.

func HTTPClient

func HTTPClient(c litellm.HTTPClient) litellm.HTTPClient

HTTPClient returns c, or http.DefaultClient when c is nil.

func HTTPError

func HTTPError(provider string, statusCode int, header http.Header, body string) *litellm.Error

HTTPError classifies a non-2xx response from its status, headers and body. header may be nil; a Retry-After value populates RetryAfter.

func MarshalBody

func MarshalBody(req any, options map[string]any) ([]byte, error)

MarshalBody encodes req, a typed request body, with options applied. Only the objects an option merges into are decoded; the rest of the body is copied as encoded.

func NewState

func NewState(provider, model string, data any) *litellm.ProviderState

NewState returns data, the vendor's native replay fields, as the state of a block provider produced for model. json.RawMessage data is kept verbatim.

func ParseDataURL

func ParseDataURL(url string) (mimeType, data string, ok bool)

ParseDataURL splits "data:<mime>;base64,<data>" into its MIME type and base64 payload. Other forms, including non-base64 data URLs, report false.

func ParseRetryAfter

func ParseRetryAfter(value string, now time.Time) time.Duration

ParseRetryAfter returns the delay described by value, either delay-seconds or an HTTP-date relative to now. Absent, invalid or past values yield zero.

func PutOption

func PutOption(body map[string]any, key string, value any) error

PutOption copies an option into body. An option naming a generated object is merged into it and one naming a generated array is appended to it, at every depth; any other collision is an error. Generated values are not modified.

func ReadState

func ReadState[T any](state *litellm.ProviderState, provider string) (T, bool)

ReadState decodes state into a T when provider produced it. Foreign, absent or undecodable state reports false.

func SetHeaders

func SetHeaders(h http.Header, headers map[string]string) error

SetHeaders applies user-configured headers, skipping blank values.

func StreamError

func StreamError(provider, code, message string) *litellm.Error

StreamError classifies an error reported inside a response stream, where no HTTP status is available. code is the vendor error code or type.

func SumTokens

func SumTokens(counts ...*int) *int

SumTokens returns a total only when every component is known.

Types

type BlockTracker

type BlockTracker[K comparable] struct {
	// contains filtered or unexported fields
}

BlockTracker maps provider-native keys to open litellm block indexes. The zero value is ready to use.

func (*BlockTracker[K]) Close

func (t *BlockTracker[K]) Close(events []litellm.Event, key K, final litellm.Block) []litellm.Event

Close appends a BlockEnd for key's open block. final may be nil or carry late metadata; see litellm.BlockEnd.

func (*BlockTracker[K]) CloseAll

func (t *BlockTracker[K]) CloseAll(events []litellm.Event, final func(K) litellm.Block) []litellm.Event

CloseAll appends a BlockEnd for every open block, in index order. final, when not nil, supplies each block's late metadata.

func (*BlockTracker[K]) Index

func (t *BlockTracker[K]) Index(key K) (int, bool)

Index reports the index of key's open block.

func (*BlockTracker[K]) Open

func (t *BlockTracker[K]) Open(events []litellm.Event, key K, block litellm.Block) ([]litellm.Event, int)

Open returns the index of key's open block, first appending a BlockStart for block when key has none.

type SSEEvent

type SSEEvent struct {
	Name string
	Data string
}

SSEEvent is one data line with the name from a preceding "event:" line.

type SSEReader

type SSEReader struct {

	// AcceptBare returns lines outside SSE framing as Data, for endpoints
	// that may stream bare JSON instead.
	AcceptBare bool
	// contains filtered or unexported fields
}

SSEReader reads the Server-Sent Events framing used by LLM streaming APIs. Each "data:" line is one payload: providers send single-line JSON and many gateways omit the blank line between events, so lines are not joined.

func NewSSEReader

func NewSSEReader(r io.Reader, provider string) *SSEReader

NewSSEReader returns a reader over r; provider is used in its errors.

func (*SSEReader) Next

func (r *SSEReader) Next() (SSEEvent, error)

Next returns the next non-empty payload. It returns io.EOF at the end of the body and litellm errors for read failures and oversized lines.

Jump to

Keyboard shortcuts

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