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
- func APIKey(ctx context.Context, key string, fn func(context.Context) (string, error), ...) (string, error)
- func AddTokenDetails(base *int, details ...*int) *int
- func ApplyOptions(body map[string]any, options map[string]any) error
- func CheckOptions(options map[string]any, allowed []string) error
- func Do(client litellm.HTTPClient, req *http.Request, provider, operation string) (*http.Response, error)
- func ErrorField(provider string, raw json.RawMessage) error
- func FinishReason(raw string) litellm.FinishReason
- func HTTPClient(c litellm.HTTPClient) litellm.HTTPClient
- func HTTPError(provider string, statusCode int, header http.Header, body string) *litellm.Error
- func MarshalBody(req any, options map[string]any) ([]byte, error)
- func NewState(provider, model string, data any) *litellm.ProviderState
- func ParseDataURL(url string) (mimeType, data string, ok bool)
- func ParseRetryAfter(value string, now time.Time) time.Duration
- func PutOption(body map[string]any, key string, value any) error
- func ReadState[T any](state *litellm.ProviderState, provider string) (T, bool)
- func SetHeaders(h http.Header, headers map[string]string) error
- func StreamError(provider, code, message string) *litellm.Error
- func SumTokens(counts ...*int) *int
- type BlockTracker
- func (t *BlockTracker[K]) Close(events []litellm.Event, key K, final litellm.Block) []litellm.Event
- func (t *BlockTracker[K]) CloseAll(events []litellm.Event, final func(K) litellm.Block) []litellm.Event
- func (t *BlockTracker[K]) Index(key K) (int, bool)
- func (t *BlockTracker[K]) Open(events []litellm.Event, key K, block litellm.Block) ([]litellm.Event, int)
- type SSEEvent
- type SSEReader
Constants ¶
const DefaultUserAgent = "litellm-go/0.1"
DefaultUserAgent identifies the SDK when a config sets no UserAgent.
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 ¶
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 ¶
ApplyOptions puts options, in sorted key order, into body.
func CheckOptions ¶
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 ¶
HTTPError classifies a non-2xx response from its status, headers and body. header may be nil; a Retry-After value populates RetryAfter.
func MarshalBody ¶
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 ¶
ParseDataURL splits "data:<mime>;base64,<data>" into its MIME type and base64 payload. Other forms, including non-base64 data URLs, report false.
func ParseRetryAfter ¶
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 ¶
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 ¶
SetHeaders applies user-configured headers, skipping blank values.
func StreamError ¶
StreamError classifies an error reported inside a response stream, where no HTTP status is available. code is the vendor error code or type.
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 ¶
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.
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 ¶
NewSSEReader returns a reader over r; provider is used in its errors.