Documentation
¶
Overview ¶
Package litellm provides a predictable, explicit client for multiple LLM providers.
Design Principles ¶
- Explicit configuration: no environment auto-discovery, no auto-routing
- Single-provider client: each Client binds exactly one Provider
- Predictable behavior: fail fast instead of guessing
Quick Start ¶
Create a client explicitly:
client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
APIKey: os.Getenv("OPENAI_API_KEY"),
})
if err != nil {
log.Fatal(err)
}
resp, err := client.Chat(context.Background(), &litellm.Request{
Model: "gpt-4o-mini",
Messages: []litellm.Message{
{Role: "user", Content: "Explain AI in one sentence."},
},
})
Streaming ¶
stream, err := client.Stream(ctx, &litellm.Request{
Model: "gpt-4o-mini",
Messages: []litellm.Message{
{Role: "user", Content: "Tell me a joke."},
},
})
if err != nil {
log.Fatal(err)
}
defer stream.Close()
for {
chunk, err := stream.Next()
if err != nil || chunk.Done {
break
}
fmt.Print(chunk.Content)
}
Or collect the full response:
resp, err := litellm.CollectStream(stream)
if err != nil {
log.Fatal(err)
}
fmt.Print(resp.Content)
Or stream while aggregating:
resp, err := litellm.CollectStreamWithHandler(stream, func(chunk *litellm.StreamChunk) {
if chunk.Type == litellm.ChunkTypeContent && chunk.Content != "" {
fmt.Print(chunk.Content)
}
})
if err != nil {
log.Fatal(err)
}
fmt.Print(resp.Content)
OpenAI Responses API ¶
Use a dedicated request type for Responses API:
resp, err := client.Responses(ctx, &litellm.OpenAIResponsesRequest{
Model: "o3-mini",
Messages: []litellm.Message{
{Role: "user", Content: "Solve 15*8 step by step."},
},
ReasoningEffort: "medium",
ReasoningSummary: "auto",
MaxOutputTokens: litellm.IntPtr(800),
})
_ = resp
Custom Providers ¶
Implement the Provider interface and register it:
type MyProvider struct{}
func (p *MyProvider) Name() string { return "myprovider" }
func (p *MyProvider) Validate() error { return nil }
func (p *MyProvider) Chat(ctx context.Context, req *litellm.Request) (*litellm.Response, error) { ... }
func (p *MyProvider) Stream(ctx context.Context, req *litellm.Request) (litellm.StreamReader, error) { ... }
litellm.RegisterProviderWithDescriptor(litellm.ProviderDescriptor{
Name: "myprovider",
DefaultURL: "https://api.example.com/v1",
Factory: func(cfg litellm.ProviderConfig) litellm.Provider {
return &MyProvider{}
},
})
Custom provider contract:
- Chat should validate request basics and return canonical fields when possible: Response.Provider, Response.Model, Response.FinishReason, and Response.Usage.
- Stream should emit ChunkTypeContent for text deltas, ReasoningContent for reasoning deltas, ToolCallDelta for tool streaming, and a final chunk with Done=true.
- ToolCallDelta.Index must remain stable across all deltas of the same tool call.
- Prefer returning LiteLLMError (or errors wrapped by WrapError) so upper layers can classify auth, rate-limit, timeout, model, and validation failures consistently.
Thread Safety ¶
Client is safe for concurrent use. StreamReader is not goroutine-safe and must be consumed by a single goroutine.
Index ¶
- Constants
- Variables
- func BoolPtr(v bool) *bool
- func Float64Ptr(v float64) *float64
- func GetRetryAfter(err error) int
- func IntPtr(v int) *int
- func IsAuthError(err error) bool
- func IsContextOverflowError(err error) bool
- func IsModelError(err error) bool
- func IsNetworkError(err error) bool
- func IsOverloadedError(err error) bool
- func IsPricingLoaded() bool
- func IsProviderRegistered(name string) bool
- func IsRateLimitError(err error) bool
- func IsRetryableError(err error) bool
- func IsStreamIdleError(err error) bool
- func IsValidationError(err error) bool
- func ListRegisteredProviders() []string
- func LoadPricing(ctx context.Context) error
- func LoadPricingFromReader(r io.Reader) error
- func NormalizeFinishReason(raw string) string
- func ParsePartialJSON(data string) any
- func RegisterProvider(name string, factory ProviderFactory) error
- func RegisterProviderWithDescriptor(descriptor ProviderDescriptor) error
- func SetModelPricing(model string, pricing ModelPricing)
- func StringPtr(v string) *string
- func WrapError(err error, provider string) error
- type CacheControl
- type CallMeta
- type Client
- func (c *Client) Chat(ctx context.Context, req *Request) (*Response, error)
- func (c *Client) ListModels(ctx context.Context) ([]ModelInfo, error)
- func (c *Client) ProviderName() string
- func (c *Client) Responses(ctx context.Context, req *OpenAIResponsesRequest) (*Response, error)
- func (c *Client) ResponsesStream(ctx context.Context, req *OpenAIResponsesRequest) (StreamReader, error)
- func (c *Client) Stream(ctx context.Context, req *Request) (StreamReader, error)
- type ClientOption
- type CostResult
- type DefaultConfig
- type ErrorType
- type FunctionCall
- type FunctionDef
- type Hook
- type HookFuncs
- type JSONSchema
- type LiteLLMError
- func NewAuthError(provider, message string) *LiteLLMError
- func NewError(errorType ErrorType, message string) *LiteLLMError
- func NewErrorWithCause(errorType ErrorType, message string, cause error) *LiteLLMError
- func NewHTTPError(provider string, statusCode int, message string) *LiteLLMError
- func NewModelError(provider, model, message string) *LiteLLMError
- func NewNetworkError(provider, message string, cause error) *LiteLLMError
- func NewProviderError(provider string, errorType ErrorType, message string) *LiteLLMError
- func NewRateLimitError(provider, message string, retryAfter int) *LiteLLMError
- func NewTimeoutError(provider, message string) *LiteLLMError
- func NewValidationError(provider, message string) *LiteLLMError
- type Message
- type MessageContent
- type MessageImageURL
- type ModelCapabilities
- type ModelInfo
- type ModelLister
- type ModelPricing
- type OpenAIResponsesRequest
- type Provider
- type ProviderConfig
- type ProviderDescriptor
- type ProviderFactory
- type Request
- type RequestOption
- func WithCacheRetention(retention string) RequestOption
- func WithExtra(key string, value any) RequestOption
- func WithExtras(extras map[string]any) RequestOption
- func WithJSONMode() RequestOption
- func WithJSONSchema(name, description string, schema any, strict bool) RequestOption
- func WithMaxTokens(n int) RequestOption
- func WithOnPayload(fn func(provider string, payload []byte)) RequestOption
- func WithOnWarning(fn func(provider string, message string)) RequestOption
- func WithResponseFormat(format *ResponseFormat) RequestOption
- func WithStop(sequences ...string) RequestOption
- func WithSystemPrompt(content string) RequestOption
- func WithTemperature(t float64) RequestOption
- func WithThinking(opts ...any) RequestOption
- func WithToolChoice(choice any) RequestOption
- func WithTools(tools ...Tool) RequestOption
- func WithTopP(p float64) RequestOption
- func WithoutThinking() RequestOption
- type ResilienceConfig
- type ResilientHTTPClient
- type Response
- type ResponseFormat
- type StreamCallbacks
- type StreamChunk
- type StreamReader
- type ThinkingConfig
- type Tool
- type ToolCall
- type ToolCallAccumulator
- type ToolCallDelta
- type Usage
Constants ¶
const ( CacheTypeEphemeral = "ephemeral" CacheTypePersistent = "persistent" )
CacheControl type constants.
const ( ChunkTypeContent = "content" ChunkTypeToolCallDelta = "tool_call_delta" ChunkTypeReasoning = "reasoning" )
Stream chunk type constants.
const ( ChunkTypeContentStart = "content_start" ChunkTypeContentEnd = "content_end" ChunkTypeReasoningStart = "reasoning_start" ChunkTypeReasoningEnd = "reasoning_end" ChunkTypeToolCallStart = "tool_call_start" ChunkTypeToolCallEnd = "tool_call_end" )
Lifecycle event types — bracket start/end of each content block during streaming.
const ( ResponseFormatText = "text" ResponseFormatJSONObject = "json_object" ResponseFormatJSONSchema = "json_schema" )
ResponseFormat type constants.
const ( ThinkingEnabled = "enabled" ThinkingDisabled = "disabled" )
Thinking type constants.
const ( FinishReasonStop = providers.FinishReasonStop FinishReasonLength = providers.FinishReasonLength FinishReasonToolCall = providers.FinishReasonToolCall FinishReasonError = providers.FinishReasonError FinishReasonSafety = providers.FinishReasonSafety )
FinishReason constants — canonical values returned by all providers.
const PricingURL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"
Variables ¶
var ErrStreamIdle = errors.New("stream idle timeout: no chunks received")
ErrStreamIdle is the sentinel cause for stream idle-timeout errors. Use errors.Is(err, ErrStreamIdle) or IsStreamIdleError(err) to detect.
Functions ¶
func BoolPtr ¶ added in v1.2.1
BoolPtr returns a pointer to a bool value Example: enabled := litellm.BoolPtr(true)
func Float64Ptr ¶
Float64Ptr returns a pointer to a float64 value Example: req.Temperature = litellm.Float64Ptr(0.7)
func GetRetryAfter ¶ added in v1.5.0
func IntPtr ¶
IntPtr returns a pointer to an int value Example: req.MaxTokens = litellm.IntPtr(2048)
func IsAuthError ¶ added in v1.5.0
func IsContextOverflowError ¶ added in v1.6.0
func IsModelError ¶ added in v1.5.0
func IsNetworkError ¶ added in v1.5.0
func IsOverloadedError ¶ added in v1.6.3
func IsPricingLoaded ¶ added in v1.5.5
func IsPricingLoaded() bool
IsPricingLoaded returns whether registry data has been loaded.
func IsProviderRegistered ¶ added in v1.5.0
IsProviderRegistered checks if a provider is registered (built-in or custom)
func IsRateLimitError ¶ added in v1.5.0
func IsRetryableError ¶ added in v1.5.0
func IsStreamIdleError ¶ added in v1.6.8
IsStreamIdleError reports whether err originated from a stream idle-timeout abort.
func IsValidationError ¶ added in v1.5.0
func ListRegisteredProviders ¶ added in v1.5.0
func ListRegisteredProviders() []string
ListRegisteredProviders returns all registered provider names
func LoadPricing ¶ added in v1.5.5
LoadPricing fetches model registry data from BerriAI/litellm GitHub.
func LoadPricingFromReader ¶ added in v1.5.5
LoadPricingFromReader loads model registry data from any io.Reader.
func NormalizeFinishReason ¶ added in v1.5.7
NormalizeFinishReason maps provider-specific stop reasons to canonical constants.
func ParsePartialJSON ¶ added in v1.6.0
ParsePartialJSON attempts to parse potentially incomplete JSON from streaming. It tries standard parsing first, then attempts to complete truncated JSON by closing open strings, arrays, and objects. Returns nil if the input is empty; returns an empty map if parsing fails entirely.
func RegisterProvider ¶
func RegisterProvider(name string, factory ProviderFactory) error
RegisterProvider registers a custom provider factory Returns an error if the name is empty or factory is nil
func RegisterProviderWithDescriptor ¶ added in v1.6.4
func RegisterProviderWithDescriptor(descriptor ProviderDescriptor) error
RegisterProviderWithDescriptor registers a custom provider together with lightweight static metadata such as its default BaseURL.
func SetModelPricing ¶ added in v1.5.5
func SetModelPricing(model string, pricing ModelPricing)
SetModelPricing sets custom pricing for a model.
Types ¶
type CacheControl ¶ added in v1.5.0
type CacheControl = providers.CacheControl
Core types are sourced from providers; litellm re-exports them.
func NewEphemeralCache ¶ added in v1.5.0
func NewEphemeralCache() *CacheControl
NewEphemeralCache creates an ephemeral cache control (TTL is provider-defined, typically ~5 minutes).
func NewPersistentCache ¶ added in v1.5.0
func NewPersistentCache(ttlSeconds int) *CacheControl
NewPersistentCache creates a persistent cache control with a custom TTL (seconds).
type CallMeta ¶ added in v1.6.7
type CallMeta struct {
CallID string
Provider string
Operation string
Model string
Streaming bool
StartedAt time.Time
Duration time.Duration
}
CallMeta describes a single client call.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a minimal, predictable client bound to a single Provider.
func New ¶
func New(provider Provider, opts ...ClientOption) (*Client, error)
New creates a client with an explicit Provider (no implicit discovery).
func NewWithProvider ¶ added in v1.5.4
func NewWithProvider(name string, config ProviderConfig, opts ...ClientOption) (*Client, error)
NewWithProvider creates a client from provider name and config.
func (*Client) ListModels ¶ added in v1.5.5
ListModels returns the list of available models for the bound provider (if supported).
func (*Client) ProviderName ¶ added in v1.5.7
ProviderName returns the name of the bound provider.
func (*Client) Responses ¶ added in v1.5.4
Responses executes an OpenAI Responses API request on an OpenAI provider.
func (*Client) ResponsesStream ¶ added in v1.5.4
func (c *Client) ResponsesStream(ctx context.Context, req *OpenAIResponsesRequest) (StreamReader, error)
ResponsesStream executes a streaming OpenAI Responses API request.
type ClientOption ¶
ClientOption configures the Client.
func WithDebug ¶ added in v1.5.5
func WithDebug(enabled bool) ClientOption
WithDebug enables debug logging to stderr.
func WithDebugOutput ¶ added in v1.5.5
func WithDebugOutput(w io.Writer) ClientOption
WithDebugOutput enables debug logging to a custom writer. If w is nil, debug logging is disabled.
func WithDefaults ¶
func WithDefaults(maxTokens int, temperature float64, topP float64) ClientOption
WithDefaults sets request-level defaults (applies only when fields are unset).
func WithHook ¶ added in v1.6.7
func WithHook(h Hook) ClientOption
WithHook appends a single execution hook to the client.
func WithHooks ¶ added in v1.6.7
func WithHooks(hooks ...Hook) ClientOption
WithHooks appends multiple execution hooks to the client.
type CostResult ¶ added in v1.5.5
type CostResult struct {
InputCost float64 `json:"input_cost"`
OutputCost float64 `json:"output_cost"`
CacheReadCost float64 `json:"cache_read_cost,omitempty"`
CacheWriteCost float64 `json:"cache_write_cost,omitempty"`
TotalCost float64 `json:"total_cost"`
Currency string `json:"currency"`
}
CostResult contains the calculated cost for a request.
func CalculateCost ¶ added in v1.5.5
func CalculateCost(model string, usage Usage) (*CostResult, error)
CalculateCost calculates the cost based on token usage. Registry data is loaded automatically on first call.
func CalculateCostForResponse ¶ added in v1.5.5
func CalculateCostForResponse(resp *Response) (*CostResult, error)
CalculateCostForResponse calculates the cost for a response.
type DefaultConfig ¶
type DefaultConfig struct {
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
}
DefaultConfig holds request-level defaults.
type ErrorType ¶ added in v1.5.0
Error types and constructors are sourced from providers; this file is a thin re-export.
const ( ErrorTypeAuth ErrorType = providers.ErrorTypeAuth ErrorTypeRateLimit ErrorType = providers.ErrorTypeRateLimit ErrorTypeNetwork ErrorType = providers.ErrorTypeNetwork ErrorTypeValidation ErrorType = providers.ErrorTypeValidation ErrorTypeProvider ErrorType = providers.ErrorTypeProvider ErrorTypeTimeout ErrorType = providers.ErrorTypeTimeout ErrorTypeQuota ErrorType = providers.ErrorTypeQuota ErrorTypeModel ErrorType = providers.ErrorTypeModel ErrorTypeInternal ErrorType = providers.ErrorTypeInternal ErrorTypeContextOverflow ErrorType = providers.ErrorTypeContextOverflow ErrorTypeOverloaded ErrorType = providers.ErrorTypeOverloaded )
type FunctionCall ¶
type FunctionCall = providers.FunctionCall
Core types are sourced from providers; litellm re-exports them.
type FunctionDef ¶ added in v1.5.0
type FunctionDef = providers.FunctionDef
Core types are sourced from providers; litellm re-exports them.
type Hook ¶ added in v1.6.7
type Hook interface {
BeforeRequest(ctx context.Context, meta CallMeta)
AfterResponse(ctx context.Context, meta CallMeta, resp *Response, err error)
OnStreamChunk(ctx context.Context, meta CallMeta, chunk *StreamChunk)
}
Hook observes request execution without modifying control flow. For streaming calls, AfterResponse runs when the stream is established; resp is nil in that case.
type HookFuncs ¶ added in v1.6.7
type HookFuncs struct {
BeforeRequestFunc func(ctx context.Context, meta CallMeta)
AfterResponseFunc func(ctx context.Context, meta CallMeta, resp *Response, err error)
OnStreamChunkFunc func(ctx context.Context, meta CallMeta, chunk *StreamChunk)
}
HookFuncs adapts plain functions into a Hook.
func (HookFuncs) AfterResponse ¶ added in v1.6.7
func (HookFuncs) BeforeRequest ¶ added in v1.6.7
func (HookFuncs) OnStreamChunk ¶ added in v1.6.7
func (h HookFuncs) OnStreamChunk(ctx context.Context, meta CallMeta, chunk *StreamChunk)
type JSONSchema ¶ added in v1.2.1
type JSONSchema = providers.JSONSchema
Core types are sourced from providers; litellm re-exports them.
type LiteLLMError ¶ added in v1.5.0
type LiteLLMError = providers.LiteLLMError
func NewAuthError ¶ added in v1.5.0
func NewAuthError(provider, message string) *LiteLLMError
func NewError ¶ added in v1.5.0
func NewError(errorType ErrorType, message string) *LiteLLMError
func NewErrorWithCause ¶ added in v1.5.0
func NewErrorWithCause(errorType ErrorType, message string, cause error) *LiteLLMError
func NewHTTPError ¶ added in v1.5.0
func NewHTTPError(provider string, statusCode int, message string) *LiteLLMError
func NewModelError ¶ added in v1.5.0
func NewModelError(provider, model, message string) *LiteLLMError
func NewNetworkError ¶ added in v1.5.0
func NewNetworkError(provider, message string, cause error) *LiteLLMError
func NewProviderError ¶ added in v1.5.0
func NewProviderError(provider string, errorType ErrorType, message string) *LiteLLMError
func NewRateLimitError ¶ added in v1.5.0
func NewRateLimitError(provider, message string, retryAfter int) *LiteLLMError
func NewTimeoutError ¶ added in v1.5.0
func NewTimeoutError(provider, message string) *LiteLLMError
func NewValidationError ¶ added in v1.5.0
func NewValidationError(provider, message string) *LiteLLMError
type Message ¶
Core types are sourced from providers; litellm re-exports them.
func AssistantMessage ¶ added in v1.5.2
AssistantMessage creates an assistant message Example: litellm.AssistantMessage("Hello! How can I help you?")
func MultiContentMessage ¶ added in v1.5.4
func MultiContentMessage(role string, contents ...MessageContent) Message
MultiContentMessage creates a message with multiple content items (text, images, etc).
Example:
msg := litellm.MultiContentMessage("user",
litellm.TextContent("What's in this image?"),
litellm.ImageContent("https://example.com/image.png"),
)
func SystemMessage ¶ added in v1.5.2
SystemMessage creates a system message Example: litellm.SystemMessage("You are a helpful assistant.")
func ToolMessage ¶ added in v1.5.2
ToolMessage creates a tool response message Example: litellm.ToolMessage("call_abc123", `{"result": "success"}`)
func UserMessage ¶ added in v1.5.2
UserMessage creates a user message Example: litellm.UserMessage("Hello, AI!")
type MessageContent ¶ added in v1.5.3
type MessageContent = providers.MessageContent
Core types are sourced from providers; litellm re-exports them.
func ImageContent ¶ added in v1.5.4
func ImageContent(url string) MessageContent
ImageContent creates an image content item from a URL.
func ImageContentWithDetail ¶ added in v1.5.4
func ImageContentWithDetail(url, detail string) MessageContent
ImageContentWithDetail creates an image content item with detail level. Detail can be "auto", "low", or "high".
func TextContent ¶ added in v1.5.4
func TextContent(text string) MessageContent
TextContent creates a text content item for multi-content messages.
func ToolRefContent ¶ added in v1.6.2
func ToolRefContent(toolName string) MessageContent
ToolRefContent creates a tool_reference content item for tool search results.
type MessageImageURL ¶ added in v1.5.3
type MessageImageURL = providers.MessageImageURL
Core types are sourced from providers; litellm re-exports them.
type ModelCapabilities ¶ added in v1.5.7
type ModelCapabilities struct {
Provider string `json:"litellm_provider"`
MaxInputTokens int `json:"max_input_tokens"`
MaxOutputTokens int `json:"max_output_tokens"`
SupportsTools bool `json:"supports_function_calling"`
SupportsVision bool `json:"supports_vision"`
SupportsReasoning bool `json:"supports_reasoning"`
}
ModelCapabilities contains capability and limit metadata for a model.
func GetModelCapabilities ¶ added in v1.5.7
func GetModelCapabilities(model string) (*ModelCapabilities, bool)
GetModelCapabilities returns capability metadata for a model.
type ModelLister ¶ added in v1.5.5
type ModelLister = providers.ModelLister
Core types are sourced from providers; litellm re-exports them.
type ModelPricing ¶ added in v1.5.5
type ModelPricing struct {
InputCostPerToken float64 `json:"input_cost_per_token"`
OutputCostPerToken float64 `json:"output_cost_per_token"`
CacheReadCostPerToken float64 `json:"cache_read_input_token_cost,omitempty"`
CacheWriteCostPerToken float64 `json:"cache_creation_input_token_cost,omitempty"`
}
ModelPricing contains pricing information for a model.
func GetModelPricing ¶ added in v1.5.5
func GetModelPricing(model string) (*ModelPricing, bool)
GetModelPricing returns the pricing for a model.
type OpenAIResponsesRequest ¶ added in v1.5.4
type OpenAIResponsesRequest = providers.OpenAIResponsesRequest
Core types are sourced from providers; litellm re-exports them.
type ProviderConfig ¶
type ProviderConfig = providers.ProviderConfig
Core types are sourced from providers; litellm re-exports them.
type ProviderDescriptor ¶ added in v1.6.4
type ProviderDescriptor struct {
Name string
DefaultURL string
Factory ProviderFactory
}
ProviderDescriptor describes a custom provider registration. Keep this intentionally small: only static metadata needed by the client runtime should live here.
type ProviderFactory ¶
type ProviderFactory func(config ProviderConfig) Provider
ProviderFactory is used to register custom providers.
type Request ¶
Core types are sourced from providers; litellm re-exports them.
func NewRequest ¶ added in v1.5.4
func NewRequest(model, prompt string, opts ...RequestOption) *Request
NewRequest creates a new Request with the given model and user prompt. Additional options can be passed to configure the request.
Example:
req := litellm.NewRequest("gpt-4", "Hello",
litellm.WithSystemPrompt("You are helpful"),
litellm.WithMaxTokens(1024),
litellm.WithTemperature(0.7),
)
func NewRequestWithMessages ¶ added in v1.5.4
func NewRequestWithMessages(model string, messages []Message, opts ...RequestOption) *Request
NewRequestWithMessages creates a new Request with the given model and messages. Use this when you need full control over the message history.
Example:
req := litellm.NewRequestWithMessages("gpt-4",
[]litellm.Message{
litellm.SystemMessage("You are helpful"),
litellm.UserMessage("Hello"),
},
litellm.WithMaxTokens(1024),
)
type RequestOption ¶ added in v1.5.4
type RequestOption func(*Request)
RequestOption configures a Request using the functional options pattern.
func WithCacheRetention ¶ added in v1.6.0
func WithCacheRetention(retention string) RequestOption
WithCacheRetention configures automatic prompt caching placement. Supported values:
- "none": disable auto-caching
- "short": ephemeral cache (~5 min, Anthropic default)
- "long": extended cache retention where supported
When enabled, cache breakpoints are automatically placed on system messages and the last user message. User-provided CacheControl on individual messages takes precedence and is not overwritten.
Currently supported by: Anthropic.
func WithExtra ¶ added in v1.5.4
func WithExtra(key string, value any) RequestOption
WithExtra sets a provider-specific parameter.
func WithExtras ¶ added in v1.5.4
func WithExtras(extras map[string]any) RequestOption
WithExtras sets multiple provider-specific parameters.
func WithJSONMode ¶ added in v1.5.4
func WithJSONMode() RequestOption
WithJSONMode enables JSON object output mode. The model will return valid JSON without enforcing a specific schema.
func WithJSONSchema ¶ added in v1.5.4
func WithJSONSchema(name, description string, schema any, strict bool) RequestOption
WithJSONSchema enables structured JSON output with schema validation.
func WithMaxTokens ¶ added in v1.5.4
func WithMaxTokens(n int) RequestOption
WithMaxTokens sets the maximum number of tokens to generate.
func WithOnPayload ¶ added in v1.6.0
func WithOnPayload(fn func(provider string, payload []byte)) RequestOption
WithOnPayload sets a debug hook that receives the serialized JSON body before each HTTP request is sent to the provider.
func WithOnWarning ¶ added in v1.6.8
func WithOnWarning(fn func(provider string, message string)) RequestOption
WithOnWarning sets a hook for portability warnings, such as an unsupported option being omitted for a provider instead of failing the request.
func WithResponseFormat ¶ added in v1.5.4
func WithResponseFormat(format *ResponseFormat) RequestOption
WithResponseFormat sets a custom response format.
func WithStop ¶ added in v1.5.4
func WithStop(sequences ...string) RequestOption
WithStop sets the stop sequences.
func WithSystemPrompt ¶ added in v1.5.4
func WithSystemPrompt(content string) RequestOption
WithSystemPrompt prepends a system message to the request. If a system message already exists, it will be replaced.
func WithTemperature ¶ added in v1.5.4
func WithTemperature(t float64) RequestOption
WithTemperature sets the sampling temperature (0.0 to 2.0). Higher values make output more random, lower values more deterministic.
func WithThinking ¶ added in v1.5.4
func WithThinking(opts ...any) RequestOption
WithThinking enables thinking/reasoning mode.
WithThinking() // enable with provider defaults
WithThinking("high") // set reasoning effort level (low/medium/high)
WithThinking(8192) // set token budget
WithThinking("high", 8192) // both level and budget
func WithToolChoice ¶ added in v1.5.4
func WithToolChoice(choice any) RequestOption
WithToolChoice sets the tool choice behavior. Accepts: "auto", "none", "required", or a specific tool name.
func WithTools ¶ added in v1.5.4
func WithTools(tools ...Tool) RequestOption
WithTools adds tool definitions to the request.
func WithTopP ¶ added in v1.5.4
func WithTopP(p float64) RequestOption
WithTopP sets the nucleus sampling parameter (0.0 to 1.0).
func WithoutThinking ¶ added in v1.5.4
func WithoutThinking() RequestOption
WithoutThinking explicitly disables thinking/reasoning mode.
type ResilienceConfig ¶ added in v1.2.2
type ResilienceConfig = providers.ResilienceConfig
ResilienceConfig and defaults are sourced from providers; re-exported here to keep the public API small.
func DefaultResilienceConfig ¶ added in v1.2.2
func DefaultResilienceConfig() ResilienceConfig
type ResilientHTTPClient ¶ added in v1.2.2
type ResilientHTTPClient struct {
// contains filtered or unexported fields
}
ResilientHTTPClient wraps http.Client with retry logic
func NewResilientHTTPClient ¶ added in v1.2.2
func NewResilientHTTPClient(config ResilienceConfig) *ResilientHTTPClient
NewResilientHTTPClient creates a new resilient HTTP client
type Response ¶
Core types are sourced from providers; litellm re-exports them.
func CollectStream ¶ added in v1.5.4
func CollectStream(stream StreamReader) (*Response, error)
CollectStream consumes a StreamReader and returns a unified Response. Callers are responsible for closing the stream.
func CollectStreamWithCallbacks ¶ added in v1.5.4
func CollectStreamWithCallbacks(stream StreamReader, callbacks StreamCallbacks) (*Response, error)
CollectStreamWithCallbacks consumes a StreamReader, calls callbacks for each chunk, and returns a unified Response. Callers are responsible for closing the stream.
func CollectStreamWithHandler ¶ added in v1.5.4
func CollectStreamWithHandler(stream StreamReader, onChunk func(*StreamChunk)) (*Response, error)
CollectStreamWithHandler consumes a StreamReader, calls onChunk for each chunk, and returns a unified Response. Callers are responsible for closing the stream.
type ResponseFormat ¶ added in v1.2.1
type ResponseFormat = providers.ResponseFormat
Core types are sourced from providers; litellm re-exports them.
func NewResponseFormatJSONObject ¶ added in v1.2.1
func NewResponseFormatJSONObject() *ResponseFormat
NewResponseFormatJSONObject creates a JSON object response format This ensures the model returns valid JSON without enforcing a specific schema
func NewResponseFormatJSONSchema ¶ added in v1.2.1
func NewResponseFormatJSONSchema(name, description string, schema any, strict bool) *ResponseFormat
NewResponseFormatJSONSchema creates a JSON schema response format with strict validation enabled/disabled
Parameters:
- name: Schema name (required)
- description: Schema description (optional, can be empty)
- schema: JSON Schema definition as a map[string]interface{}
- strict: Enable strict schema validation (OpenAI only)
Example:
schema := map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{"type": "string"},
"age": map[string]interface{}{"type": "integer"},
},
"required": []string{"name", "age"},
}
format := litellm.NewResponseFormatJSONSchema("person", "A person object", schema, true)
func NewResponseFormatText ¶ added in v1.2.1
func NewResponseFormatText() *ResponseFormat
NewResponseFormatText creates a text response format
type StreamCallbacks ¶ added in v1.5.4
type StreamCallbacks struct {
OnChunk func(*StreamChunk)
OnContent func(string)
OnReasoning func(content string)
OnToolCall func(*ToolCallDelta)
// Lifecycle callbacks — bracket start/end of each content block.
// Transitions are detected automatically from chunk types.
OnContentStart func()
OnContentEnd func(content string) // content = full accumulated block
OnReasoningStart func()
OnReasoningEnd func(content string) // content = full accumulated reasoning
OnToolCallStart func(delta *ToolCallDelta) // carries ID and FunctionName
OnToolCallEnd func(call ToolCall) // carries complete ToolCall
}
StreamCallbacks provides optional per-chunk handlers during stream collection.
type StreamChunk ¶
type StreamChunk = providers.StreamChunk
Core types are sourced from providers; litellm re-exports them.
type StreamReader ¶
type StreamReader = providers.StreamReader
Core types are sourced from providers; litellm re-exports them.
type ThinkingConfig ¶ added in v1.5.4
type ThinkingConfig = providers.ThinkingConfig
Core types are sourced from providers; litellm re-exports them.
func NewThinkingDisabled ¶ added in v1.5.4
func NewThinkingDisabled() *ThinkingConfig
NewThinkingDisabled disables thinking output.
func NewThinkingEnabled ¶ added in v1.5.4
func NewThinkingEnabled(budgetTokens int) *ThinkingConfig
NewThinkingEnabled enables thinking with an optional budget.
func NewThinkingWithLevel ¶ added in v1.5.7
func NewThinkingWithLevel(level string) *ThinkingConfig
NewThinkingWithLevel enables thinking with a reasoning level. Level is provider-specific: "low", "medium", "high" etc.
type ToolCallAccumulator ¶ added in v1.5.7
type ToolCallAccumulator struct {
// contains filtered or unexported fields
}
ToolCallAccumulator reconstructs complete ToolCall objects from streaming deltas. Safe for single-goroutine use only.
func NewToolCallAccumulator ¶ added in v1.5.7
func NewToolCallAccumulator() *ToolCallAccumulator
NewToolCallAccumulator creates an empty accumulator.
func (*ToolCallAccumulator) Apply ¶ added in v1.5.7
func (a *ToolCallAccumulator) Apply(delta *ToolCallDelta)
Apply processes a single ToolCallDelta, creating or updating the corresponding ToolCall.
func (*ToolCallAccumulator) Build ¶ added in v1.5.7
func (a *ToolCallAccumulator) Build() []ToolCall
Build returns the completed ToolCall list in first-seen order.
func (*ToolCallAccumulator) Get ¶ added in v1.6.0
func (a *ToolCallAccumulator) Get(index int) *ToolCall
Get returns the accumulated ToolCall for the given index, or nil if not found.
func (*ToolCallAccumulator) PartialArguments ¶ added in v1.6.0
func (a *ToolCallAccumulator) PartialArguments(index int) any
PartialArguments returns a best-effort parse of the accumulated (possibly incomplete) function arguments for the tool call at the given index. Useful for streaming UIs that want to display arguments as they arrive. Returns nil if the index has no accumulated data.
func (*ToolCallAccumulator) Started ¶ added in v1.5.7
func (a *ToolCallAccumulator) Started(index int) bool
Started reports whether a delta with the given index has been received.
type ToolCallDelta ¶
type ToolCallDelta = providers.ToolCallDelta
Core types are sourced from providers; litellm re-exports them.