wire

package
v1.9.5 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package wire holds what provider adapters share to speak vendor wire protocols: HTTP transport, Server-Sent Events and the stream event queue, error and stop-reason classification, stream block indexing, ProviderOptions merging, replay state, tool schemas and arguments, 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 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 the client a provider sends with: c, or http.DefaultClient when c is nil, failing a call whose context carries an idle timeout once it waits that long for data.

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. The status decides the type unless a vendor code or message identifies a failure it does not; the code names the failure in the error. RetryAfter is the Retry-After header's wait or, without one, the retryDelay a Google API error body suggests, as Gemini sends.

func IsObject added in v1.9.4

func IsObject(text string) bool

IsObject reports whether text is a JSON object.

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 ToolInput added in v1.9.4

func ToolInput(call litellm.ToolUseBlock) (json.RawMessage, error)

ToolInput returns the arguments of call as the JSON object wire formats such as Anthropic's, Bedrock's and Gemini's need, the empty object when it has none. Arguments that are not an object fail, naming the call.

func ToolParameters added in v1.9.4

func ToolParameters(t litellm.Tool) json.RawMessage

ToolParameters returns the parameters schema of t, one taking no arguments when it has none, as wire formats require a schema.

func ToolReferenceText added in v1.9.3

func ToolReferenceText(ref litellm.ToolReferenceBlock) string

ToolReferenceText is a tool reference as text, for wire formats without one: the referenced tool is among the request's tools.

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.

type Stream added in v1.9.4

type Stream struct {
	// contains filtered or unexported fields
}

Stream is the litellm.Stream of a vendor response body. Next delivers the queued events and, while none are queued, calls read, which decodes the next frame of the body and appends its events to those it is given. read returns io.EOF at the end of the body, which before a DoneEvent the Client reports as a truncated reply, a retryable network error. Once read queues a DoneEvent or fails, or the stream is closed, Next returns io.EOF after the queued events.

func NewStream added in v1.9.4

func NewStream(body io.Closer, read func([]litellm.Event) ([]litellm.Event, error), events ...litellm.Event) *Stream

NewStream returns a Stream over body that delivers events first.

func (*Stream) Close added in v1.9.4

func (s *Stream) Close() error

Close closes the body.

func (*Stream) Next added in v1.9.4

func (s *Stream) Next() (litellm.Event, error)

Next returns the next event.

Jump to

Keyboard shortcuts

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