Documentation
¶
Overview ¶
Package core provides the Iris SDK client and types.
Package core provides the Iris SDK client and types for interacting with AI providers.
Iris is a Go-native framework for building chat and multimodal workflows. The core package defines the fundamental abstractions that all providers implement.
Client and Provider ¶
The primary entry point is Client, which wraps a Provider and adds telemetry, retry logic, and a fluent builder API:
provider := openai.New(os.Getenv("OPENAI_API_KEY"))
client := core.NewClient(provider,
core.WithTelemetry(myTelemetryHook),
core.WithRetryPolicy(core.DefaultRetryPolicy()),
core.WithWarningHandler(func(msg string) { log.Printf("iris warning: %s", msg) }),
)
ChatBuilder ¶
The ChatBuilder provides a fluent API for constructing chat requests:
resp, err := client.Chat("gpt-4o").
System("You are a helpful assistant.").
User("Hello!").
Temperature(0.7).
GetResponse(ctx)
ChatBuilder is NOT thread-safe. Each goroutine should create its own builder instance. Use ChatBuilder.Clone to create independent copies from a base configuration:
base := client.Chat(model).System("You are helpful.").Temperature(0.7)
go func() { resp1, _ := base.Clone().User("Q1").GetResponse(ctx) }()
go func() { resp2, _ := base.Clone().User("Q2").GetResponse(ctx) }()
Most ChatBuilder configuration methods mutate the receiver and return the same pointer. ChatBuilder.ToolResults, ChatBuilder.ToolResult, and ChatBuilder.ToolError are the exceptions: they clone the builder and leave the original unchanged.
ChatBuilder.Tools, ChatBuilder.ResponseJSONSchema, and ChatBuilder.ResponseJSONSchemaNonStrict defensively copy their caller-owned slice or schema, including raw schema bytes. Callers may therefore reuse or modify those containers after configuring a builder. Tools themselves are interface values and are not cloned; treat the underlying tool objects as immutable while a builder that references them is in use.
Conversations and Memory ¶
Conversation manages multi-turn history. Its constructor and operations require a context, which is passed to both the provider and every Memory operation so cancellations, deadlines, and trace metadata reach remote storage implementations:
conv := core.NewConversation(ctx, client, model) resp, err := conv.Send(ctx, "Hello")
Conversation history preserves complete Message values, including assistant tool calls and tool-result turns. After executing requested tools, use Conversation.AddToolResults before sending the next user message.
Streaming ¶
Iris treats streaming as a first-class primitive. Use ChatBuilder.Stream for streaming responses:
stream, err := client.Chat(model).User("Tell me a story.").Stream(ctx)
if err != nil {
return err
}
for chunk := range stream.Ch {
fmt.Print(chunk.Delta)
}
The ChatStream type provides three channels:
- Ch: Emits text deltas in order
- Err: Emits at most one error
- Final: Emits the complete response with usage and tool calls
Use DrainStream as a convenience to accumulate all chunks into a final response.
Provider Interface ¶
All providers implement the Provider interface:
type Provider interface {
ID() string
Models() []ModelInfo
Supports(feature Feature) bool
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
StreamChat(ctx context.Context, req *ChatRequest) (*ChatStream, error)
}
Providers SHOULD be safe for concurrent use. Use Provider.Supports to check capabilities before making requests:
if provider.Supports(core.FeatureToolCalling) {
// Safe to use tools
}
Features ¶
Providers declare their capabilities through Feature constants:
- FeatureChat: Basic chat completion
- FeatureChatStreaming: Streaming chat completion
- FeatureToolCalling: Function/tool calling support
- FeatureReasoning: Extended reasoning capabilities
- FeatureBuiltInTools: Web search, file search, code interpreter
- FeatureResponseChain: Multi-turn response chaining
- FeatureTokenCounting: Native input-token counting
- FeatureEmbeddings: Text embedding generation
- FeatureContextualizedEmbeddings: Document-aware embeddings
- FeatureReranking: Search result reranking
Error Handling ¶
The package defines sentinel errors for common failure modes:
- ErrUnauthorized: Invalid or missing API key
- ErrRateLimited: Provider rate limit exceeded
- ErrBadRequest: Invalid request parameters
- ErrNotFound: Requested provider resource was not found
- ErrServer: Provider server error (5xx)
- ErrNetwork: Network connectivity issues
- ErrDecode: Response parsing failed
- ErrNilStream: A nil stream was passed to DrainStream
- ErrModelRequired: Model ID not specified
- ErrNoMessages: No messages in request
Use errors.Is to check error types:
if errors.Is(err, core.ErrRateLimited) {
// Handle rate limiting
}
Telemetry ¶
Implement TelemetryHook to observe request lifecycle:
type MyTelemetry struct{}
func (t MyTelemetry) OnRequestStart(e RequestStartEvent) {
log.Printf("Starting %s request to %s", e.Model, e.Provider)
}
func (t MyTelemetry) OnRequestEnd(e RequestEndEvent) {
log.Printf("Completed in %v, tokens: %d", e.End.Sub(e.Start), e.Usage.TotalTokens)
}
Retry Policy ¶
Configure retry behavior with RetryPolicy:
policy := core.NewRetryPolicy(core.RetryConfig{
MaxRetries: 3,
BaseDelay: time.Second,
MaxDelay: 30 * time.Second,
})
client := core.NewClient(provider, core.WithRetryPolicy(policy))
The default policy retries transient errors (rate limits, server errors) with exponential backoff.
Warnings ¶
Non-fatal SDK warnings (for example, mismatched tool result IDs or message parts unsupported by the selected provider) can be routed through WithWarningHandler. The default warning handler is a no-op.
Multimodal Messages ¶
For vision and document analysis, use MessageBuilder:
resp, err := client.Chat(model).
UserMultimodal().
Text("What's in this image?").
ImageURL("https://example.com/image.jpg").
Done().
GetResponse(ctx)
Convenience methods are also available:
resp, err := client.Chat(model).
UserWithImageURL("Describe this:", "https://example.com/image.jpg").
GetResponse(ctx)
Image URLs and data URLs are supported by Anthropic, OpenAI, Azure AI Foundry, and Gemini. File IDs and document inputs are endpoint-specific. Providers declare support through ContentPartSupporter; undeclared parts invoke the client's WarningHandler before they are omitted.
Thread Safety ¶
Client is safe for concurrent use across goroutines. ChatBuilder and MessageBuilder are NOT thread-safe. ChatStream channels may be read by one goroutine at a time. Providers SHOULD be safe for concurrent calls (check provider documentation).
Package core provides the Iris SDK client and types.
Index ¶
- Constants
- Variables
- type APIEndpoint
- type BatchError
- type BatchID
- type BatchInfo
- type BatchProvider
- type BatchRequest
- type BatchResult
- type BatchStatus
- type BatchWaiter
- func (w *BatchWaiter) Wait(ctx context.Context, id BatchID) (*BatchInfo, error)
- func (w *BatchWaiter) WaitAndCollect(ctx context.Context, id BatchID) ([]BatchResult, error)
- func (w *BatchWaiter) WithMaxWait(d time.Duration) *BatchWaiter
- func (w *BatchWaiter) WithPollInterval(d time.Duration) *BatchWaiter
- type BuiltInTool
- type ChatBuilder
- func (b *ChatBuilder) Assistant(s string) *ChatBuilder
- func (b *ChatBuilder) BuiltInTool(toolType string) *ChatBuilder
- func (b *ChatBuilder) Clone() *ChatBuilder
- func (b *ChatBuilder) CodeInterpreter() *ChatBuilder
- func (b *ChatBuilder) ContinueFrom(responseID string) *ChatBuilder
- func (b *ChatBuilder) FileSearch(vectorStoreIDs ...string) *ChatBuilder
- func (b *ChatBuilder) GetResponse(ctx context.Context) (*ChatResponse, error)
- func (b *ChatBuilder) Instructions(s string) *ChatBuilder
- func (b *ChatBuilder) MaxTokens(n int) *ChatBuilder
- func (b *ChatBuilder) ReasoningEffort(level ReasoningEffort) *ChatBuilder
- func (b *ChatBuilder) ResponseJSON() *ChatBuilder
- func (b *ChatBuilder) ResponseJSONSchema(schema *JSONSchemaDefinition) *ChatBuilder
- func (b *ChatBuilder) ResponseJSONSchemaNonStrict(schema *JSONSchemaDefinition) *ChatBuilder
- func (b *ChatBuilder) ResponseText() *ChatBuilder
- func (b *ChatBuilder) SearchOptions(opts *SearchOptions) *ChatBuilder
- func (b *ChatBuilder) Stream(ctx context.Context) (*ChatStream, error)
- func (b *ChatBuilder) System(s string) *ChatBuilder
- func (b *ChatBuilder) Temperature(v float32) *ChatBuilder
- func (b *ChatBuilder) Timeout(d time.Duration) *ChatBuilder
- func (b *ChatBuilder) ToolError(assistantResp *ChatResponse, callID string, err error) *ChatBuilder
- func (b *ChatBuilder) ToolResult(assistantResp *ChatResponse, callID string, content any) *ChatBuilder
- func (b *ChatBuilder) ToolResults(assistantResp *ChatResponse, results []ToolResult) *ChatBuilder
- func (b *ChatBuilder) Tools(ts ...Tool) *ChatBuilder
- func (b *ChatBuilder) Truncation(mode string) *ChatBuilder
- func (b *ChatBuilder) User(s string) *ChatBuilder
- func (b *ChatBuilder) UserMultimodal() *MessageBuilder
- func (b *ChatBuilder) UserWithFileID(text, fileID string) *ChatBuilder
- func (b *ChatBuilder) UserWithFileURL(text, fileURL string) *ChatBuilder
- func (b *ChatBuilder) UserWithImageFileID(text, fileID string) *ChatBuilder
- func (b *ChatBuilder) UserWithImageURL(text, imageURL string) *ChatBuilder
- func (b *ChatBuilder) WebSearch() *ChatBuilder
- type ChatChunk
- type ChatRequest
- type ChatResponse
- type ChatStream
- type Client
- type ClientOption
- func WithRateLimiter(l RateLimiter) ClientOption
- func WithRetryPolicy(r RetryPolicy) ClientOption
- func WithStreamIdleTimeout(d time.Duration) ClientOption
- func WithTelemetry(h TelemetryHook) ClientOption
- func WithTimeout(d time.Duration) ClientOption
- func WithWarningHandler(h WarningHandler) ClientOption
- type ContentPart
- type ContentPartSupporter
- type ContextualTelemetryHook
- type ContextualizedEmbeddingProvider
- type ContextualizedEmbeddingRequest
- type ContextualizedEmbeddingResponse
- type Conversation
- func (c *Conversation) AddToolResults(ctx context.Context, results []ToolResult)
- func (c *Conversation) Clear(ctx context.Context)
- func (c *Conversation) GetHistory(ctx context.Context) []Message
- func (c *Conversation) MessageCount(ctx context.Context) int
- func (c *Conversation) Send(ctx context.Context, userMessage string) (*ChatResponse, error)
- func (c *Conversation) SendWithContext(ctx context.Context, userMessage string) (*ChatResponse, error)deprecated
- func (c *Conversation) Stream(ctx context.Context, userMessage string) (*ChatStream, error)
- func (c *Conversation) StreamWithContext(ctx context.Context, userMessage string) (*ChatStream, error)deprecated
- type ConversationOption
- type EmbeddingInput
- type EmbeddingProvider
- type EmbeddingRequest
- type EmbeddingResponse
- type EmbeddingUsage
- type EmbeddingVector
- type EncodingFormat
- type Feature
- type FileSearchResources
- type ImageAction
- type ImageBackground
- type ImageChunk
- type ImageData
- type ImageDetail
- type ImageEditRequest
- type ImageFormat
- type ImageGenerateRequest
- type ImageGenerator
- type ImageInput
- type ImageInputFidelity
- type ImageQuality
- type ImageResponse
- type ImageSize
- type ImageStream
- type ImageUsage
- type InMemoryStore
- func (m *InMemoryStore) AddMessage(_ context.Context, msg Message)
- func (m *InMemoryStore) AddMessages(_ context.Context, msgs []Message)
- func (m *InMemoryStore) Clear(_ context.Context)
- func (m *InMemoryStore) GetHistory(_ context.Context) []Message
- func (m *InMemoryStore) GetLastN(_ context.Context, n int) []Message
- func (m *InMemoryStore) Len(_ context.Context) int
- func (m *InMemoryStore) SetMessages(_ context.Context, msgs []Message)
- type InputFile
- type InputImage
- type InputText
- type InputType
- type IntervalRateLimiter
- type JSONSchemaDefinition
- type Memory
- type Message
- type MessageBuilder
- func (m *MessageBuilder) Done() *ChatBuilder
- func (m *MessageBuilder) FileBase64(filename, base64Data string) *MessageBuilder
- func (m *MessageBuilder) FileID(fileID string) *MessageBuilder
- func (m *MessageBuilder) FileURL(url string) *MessageBuilder
- func (m *MessageBuilder) ImageFileID(fileID string) *MessageBuilder
- func (m *MessageBuilder) ImageFileIDWithDetail(fileID string, detail ImageDetail) *MessageBuilder
- func (m *MessageBuilder) ImageURL(url string) *MessageBuilder
- func (m *MessageBuilder) ImageURLWithDetail(url string, detail ImageDetail) *MessageBuilder
- func (m *MessageBuilder) Text(s string) *MessageBuilder
- type ModelID
- type ModelInfo
- type NoopTelemetryHook
- type OutputDType
- type Provider
- type ProviderError
- type ProviderUnwrapper
- type RateLimiter
- type RateLimiterFunc
- type ReasoningEffort
- type ReasoningOutput
- type RequestEndEvent
- type RequestStartEvent
- type RerankRequest
- type RerankResponse
- type RerankResult
- type RerankUsage
- type RerankerProvider
- type ResponseFormat
- type RetryConfig
- type RetryPolicy
- type Role
- type SearchMode
- type SearchOptions
- type SearchRecencyFilter
- type Secret
- type TelemetryHook
- type TokenCountResponse
- type TokenCounter
- type TokenUsage
- type Tool
- type ToolCall
- type ToolResources
- type ToolResult
- type ToolResultBuilder
- func (b *ToolResultBuilder) Build() []ToolResult
- func (b *ToolResultBuilder) Error(callID string, err error) *ToolResultBuilder
- func (b *ToolResultBuilder) FromExecution(callID string, result any, err error) *ToolResultBuilder
- func (b *ToolResultBuilder) Success(callID string, content any) *ToolResultBuilder
- type ToolSchema
- type TypedToolResult
- type TypedToolResultBuilder
- type WarningHandler
Constants ¶
const DefaultTimeout = 120 * time.Second
DefaultTimeout is the execution timeout applied to calls routed through Client, including chat, streaming, and embeddings, when the caller's context does not already supply a deadline. Disable it with WithTimeout(0).
Variables ¶
var ( ErrRateLimited = errors.New("rate limited") ErrBadRequest = errors.New("bad request") ErrNotFound = errors.New("not found") ErrServer = errors.New("server error") ErrNetwork = errors.New("network error") ErrDecode = errors.New("decode error") ErrNotSupported = errors.New("operation not supported") )
Sentinel errors for classification.
var ( ErrBatchTimeout = errors.New("batch processing timed out") ErrBatchNotFound = fmt.Errorf("batch %w", ErrNotFound) ErrBatchCancelled = errors.New("batch was cancelled") )
Batch processing errors.
var ( ErrModelRequired = errors.New("model required: pass a model ID to Client.Chat(), e.g., client.Chat(\"gpt-4\")") ErrNoMessages = errors.New("no messages: add at least one message using .System(), .User(), or .Assistant()") )
Validation errors with actionable guidance.
var ErrInvalidSchema = errors.New("invalid json schema for strict structured output")
ErrInvalidSchema indicates a JSON Schema is not compatible with strict structured output mode (e.g., missing "additionalProperties": false or a "required" array that does not cover all declared properties).
var ErrNilStream = errors.New("nil chat stream")
ErrNilStream indicates that DrainStream was called without a stream.
var ErrSearchUnsupported = errors.New("web search options not supported by this provider")
ErrSearchUnsupported indicates a request carried core.SearchOptions targeting a provider that does not support core.FeatureWebSearch. validate() returns this before the request is sent so callers fail fast instead of sending a request whose search directives would be ignored.
var ErrStructuredOutputUnsupported = errors.New("structured output not supported by this provider or model")
ErrStructuredOutputUnsupported indicates a request asked for JSON or JSON-Schema structured output (ResponseFormatJSON or ResponseFormatJSONSchema) targeting a provider that does not support core.FeatureStructuredOutput. validate() returns this before the request is sent so callers fail fast instead of silently receiving unconstrained output.
var ErrTimeout = errors.New("execution timeout")
ErrTimeout indicates an Iris-imposed execution timeout elapsed before the provider call completed. It wraps context.DeadlineExceeded, so errors.Is(err, context.DeadlineExceeded) also holds.
Functions ¶
This section is empty.
Types ¶
type APIEndpoint ¶
type APIEndpoint string
APIEndpoint represents which API endpoint a model uses.
const ( // APIEndpointCompletions is the Chat Completions API (default for older models). APIEndpointCompletions APIEndpoint = "completions" // APIEndpointResponses is the Responses API (for newer models like GPT-5.x). APIEndpointResponses APIEndpoint = "responses" )
type BatchError ¶ added in v0.13.0
type BatchError struct {
// Code is the error code (e.g., "rate_limit_exceeded").
Code string `json:"code"`
// Message is a human-readable error description.
Message string `json:"message"`
}
BatchError contains error details for a failed batch request.
type BatchInfo ¶ added in v0.13.0
type BatchInfo struct {
// ID is the unique identifier for this batch.
ID BatchID `json:"id"`
// Status is the current processing state.
Status BatchStatus `json:"status"`
// Total is the total number of requests in the batch.
Total int `json:"total"`
// Completed is the number of successfully completed requests.
Completed int `json:"completed"`
// Failed is the number of failed requests.
Failed int `json:"failed"`
// CreatedAt is when the batch was created.
CreatedAt int64 `json:"created_at"`
// CompletedAt is when the batch finished (nil if still processing).
CompletedAt *int64 `json:"completed_at,omitempty"`
// ExpiresAt is when the batch will expire if not completed.
ExpiresAt *int64 `json:"expires_at,omitempty"`
// Endpoint is the API endpoint used for this batch.
Endpoint string `json:"endpoint,omitempty"`
// ErrorFileID contains error details if the batch failed.
ErrorFileID string `json:"error_file_id,omitempty"`
// OutputFileID contains results when the batch completes.
OutputFileID string `json:"output_file_id,omitempty"`
}
BatchInfo contains metadata about a batch.
func (*BatchInfo) IsComplete ¶ added in v0.13.0
IsComplete returns true if the batch has finished processing.
type BatchProvider ¶ added in v0.13.0
type BatchProvider interface {
// CreateBatch submits requests for asynchronous batch processing.
// Returns a BatchID that can be used to track progress and retrieve results.
// Each request's CustomID must be unique within the batch.
CreateBatch(ctx context.Context, requests []BatchRequest) (BatchID, error)
// GetBatchStatus returns the current status of a batch.
// Poll this method to check if the batch has completed processing.
GetBatchStatus(ctx context.Context, id BatchID) (*BatchInfo, error)
// GetBatchResults retrieves completed batch results.
// Should only be called after GetBatchStatus indicates completion.
// Returns results for all requests, including failures.
GetBatchResults(ctx context.Context, id BatchID) ([]BatchResult, error)
// CancelBatch cancels a pending or in-progress batch.
// Already completed requests may still return results.
CancelBatch(ctx context.Context, id BatchID) error
// ListBatches returns all batches for the account.
// Use limit to control pagination (0 for default).
ListBatches(ctx context.Context, limit int) ([]BatchInfo, error)
}
BatchProvider is an optional interface for providers supporting batch operations. Batch processing enables 50% cost savings for high-throughput workloads by submitting multiple requests that are processed asynchronously.
Not all providers support batch operations. Check Supports(FeatureBatch) before using batch methods, or use the AsBatchProvider helper.
Typical workflow:
- CreateBatch to submit requests
- Poll GetBatchStatus until IsComplete() returns true
- GetBatchResults to retrieve responses
Example:
bp, ok := core.AsBatchProvider(provider)
if !ok {
return errors.New("provider does not support batch operations")
}
batchID, err := bp.CreateBatch(ctx, requests)
if err != nil {
return err
}
// Poll for completion
for {
info, _ := bp.GetBatchStatus(ctx, batchID)
if info.IsComplete() {
break
}
time.Sleep(30 * time.Second)
}
results, err := bp.GetBatchResults(ctx, batchID)
func AsBatchProvider ¶ added in v0.13.0
func AsBatchProvider(p Provider) (BatchProvider, bool)
AsBatchProvider discovers batch operations on a Provider or its unwrap chain. It returns nil and false when no provider implements batch operations.
Example:
if bp, ok := core.AsBatchProvider(provider); ok {
batchID, err := bp.CreateBatch(ctx, requests)
// ...
}
type BatchRequest ¶ added in v0.13.0
type BatchRequest struct {
// CustomID is a user-provided identifier for correlating results.
// Must be unique within a batch and 64 characters or fewer.
CustomID string `json:"custom_id"`
// Request is the chat request to process.
Request ChatRequest `json:"request"`
}
BatchRequest wraps a ChatRequest with a custom ID for correlation.
type BatchResult ¶ added in v0.13.0
type BatchResult struct {
// CustomID is the user-provided identifier from the request.
CustomID string `json:"custom_id"`
// Response is the chat response (nil if request failed).
Response *ChatResponse `json:"response,omitempty"`
// Error contains error details if the request failed.
Error *BatchError `json:"error,omitempty"`
}
BatchResult contains the response for a single request in a batch.
func (*BatchResult) IsSuccess ¶ added in v0.13.0
func (r *BatchResult) IsSuccess() bool
IsSuccess returns true if the batch request succeeded.
type BatchStatus ¶ added in v0.13.0
type BatchStatus string
BatchStatus represents the state of a batch request.
const ( // BatchStatusPending indicates the batch is queued but not yet processing. BatchStatusPending BatchStatus = "pending" // BatchStatusInProgress indicates the batch is currently being processed. BatchStatusInProgress BatchStatus = "in_progress" // BatchStatusCompleted indicates all requests in the batch have finished. BatchStatusCompleted BatchStatus = "completed" // BatchStatusFailed indicates the batch failed (check individual results). BatchStatusFailed BatchStatus = "failed" // BatchStatusCancelled indicates the batch was cancelled by the user. BatchStatusCancelled BatchStatus = "cancelled" // BatchStatusExpired indicates the batch expired before completion. BatchStatusExpired BatchStatus = "expired" )
type BatchWaiter ¶ added in v0.13.0
type BatchWaiter struct {
// contains filtered or unexported fields
}
BatchWaiter provides utilities for waiting on batch completion.
BatchWaiter polls BatchProvider directly and does not go through core.Client, so core.WithTimeout does not apply; bound Wait via WithMaxWait and/or a context deadline passed by the caller.
func NewBatchWaiter ¶ added in v0.13.0
func NewBatchWaiter(provider BatchProvider) *BatchWaiter
NewBatchWaiter creates a waiter with default settings. Default poll interval is 30 seconds, max wait is 24 hours.
func (*BatchWaiter) Wait ¶ added in v0.13.0
Wait blocks until the batch completes or the context is cancelled. Returns the final batch info, or an error if the wait times out or fails.
Each poll is individually bounded by the remaining maxWait budget: a GetBatchStatus call that hangs (e.g. a stuck network request) cannot exceed the overall deadline, since the per-poll context is derived from that deadline rather than from the caller's context alone.
func (*BatchWaiter) WaitAndCollect ¶ added in v0.13.0
func (w *BatchWaiter) WaitAndCollect(ctx context.Context, id BatchID) ([]BatchResult, error)
WaitAndCollect waits for completion and retrieves all results. This is a convenience method combining Wait and GetBatchResults.
func (*BatchWaiter) WithMaxWait ¶ added in v0.13.0
func (w *BatchWaiter) WithMaxWait(d time.Duration) *BatchWaiter
WithMaxWait sets the maximum time to wait for batch completion.
func (*BatchWaiter) WithPollInterval ¶ added in v0.13.0
func (w *BatchWaiter) WithPollInterval(d time.Duration) *BatchWaiter
WithPollInterval sets the interval between status checks.
type BuiltInTool ¶
type BuiltInTool struct {
Type string `json:"type"` // "web_search", "file_search", "code_interpreter"
}
BuiltInTool represents a built-in tool available in the Responses API.
type ChatBuilder ¶
type ChatBuilder struct {
// contains filtered or unexported fields
}
ChatBuilder provides a fluent API for building chat requests. ChatBuilder is NOT thread-safe and should not be shared across goroutines. Most configuration methods mutate the receiver and return the same builder. Clone returns an independent builder; ToolResults, ToolResult, and ToolError are the only configuration methods that clone automatically.
func (*ChatBuilder) Assistant ¶
func (b *ChatBuilder) Assistant(s string) *ChatBuilder
Assistant appends an assistant message.
func (*ChatBuilder) BuiltInTool ¶
func (b *ChatBuilder) BuiltInTool(toolType string) *ChatBuilder
BuiltInTool adds a built-in tool to the request.
func (*ChatBuilder) Clone ¶ added in v0.10.0
func (b *ChatBuilder) Clone() *ChatBuilder
Clone creates a deep copy of the ChatBuilder. This is useful for reusing a base configuration across multiple requests:
base := client.Chat(model).System("You are a helpful assistant").Temperature(0.7)
resp1, _ := base.Clone().User("Question 1").GetResponse(ctx)
resp2, _ := base.Clone().User("Question 2").GetResponse(ctx)
The original builder remains unchanged after cloning.
func (*ChatBuilder) CodeInterpreter ¶
func (b *ChatBuilder) CodeInterpreter() *ChatBuilder
CodeInterpreter adds the code_interpreter built-in tool.
func (*ChatBuilder) ContinueFrom ¶
func (b *ChatBuilder) ContinueFrom(responseID string) *ChatBuilder
ContinueFrom chains this request to a previous response.
func (*ChatBuilder) FileSearch ¶
func (b *ChatBuilder) FileSearch(vectorStoreIDs ...string) *ChatBuilder
FileSearch adds the file_search built-in tool with optional vector store IDs.
func (*ChatBuilder) GetResponse ¶
func (b *ChatBuilder) GetResponse(ctx context.Context) (*ChatResponse, error)
GetResponse executes the chat request and returns the response. It applies validation, telemetry, and retry logic. If Timeout was set and ctx has no deadline, a timeout context is created internally.
func (*ChatBuilder) Instructions ¶
func (b *ChatBuilder) Instructions(s string) *ChatBuilder
Instructions sets the system instructions (Responses API style). For Chat Completions API, this is equivalent to adding a system message.
func (*ChatBuilder) MaxTokens ¶
func (b *ChatBuilder) MaxTokens(n int) *ChatBuilder
MaxTokens sets the maximum tokens parameter.
func (*ChatBuilder) ReasoningEffort ¶
func (b *ChatBuilder) ReasoningEffort(level ReasoningEffort) *ChatBuilder
ReasoningEffort sets the reasoning effort level for models that support it.
func (*ChatBuilder) ResponseJSON ¶ added in v0.13.0
func (b *ChatBuilder) ResponseJSON() *ChatBuilder
ResponseJSON constrains the model output to valid JSON. This enables JSON mode where the model outputs syntactically valid JSON. Note: The model may still produce invalid JSON in edge cases; always validate.
func (*ChatBuilder) ResponseJSONSchema ¶ added in v0.13.0
func (b *ChatBuilder) ResponseJSONSchema(schema *JSONSchemaDefinition) *ChatBuilder
ResponseJSONSchema constrains the model output to match a specific JSON Schema. This enables structured output mode where the model produces JSON conforming to the schema. The schema parameter defines the structure the output must conform to.
This copies schema, including its raw JSON bytes, and forces Strict = true on the copy without modifying the caller's value. Strict mode requires the schema to set "additionalProperties": false and list every property in "required" at every object node; validate() rejects non-compliant schemas with ErrInvalidSchema before the request is sent. Use ResponseJSONSchemaNonStrict to opt out of strict mode for schemas that cannot meet those constraints.
Example:
schema := &core.JSONSchemaDefinition{
Name: "person",
Schema: json.RawMessage(`{"type":"object","additionalProperties":false,"properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name","age"]}`),
}
resp, err := client.Chat(model).
User("Extract: John is 30 years old").
ResponseJSONSchema(schema).
GetResponse(ctx)
func (*ChatBuilder) ResponseJSONSchemaNonStrict ¶ added in v0.16.0
func (b *ChatBuilder) ResponseJSONSchemaNonStrict(schema *JSONSchemaDefinition) *ChatBuilder
ResponseJSONSchemaNonStrict constrains the model output to match a specific JSON Schema without enforcing strict mode. This copies schema, including its raw JSON bytes, and forces Strict = false on the copy without modifying the caller's value. It skips the strict-schema validation that ResponseJSONSchema applies. Use this when a schema cannot satisfy strict mode's requirements (every object node needs "additionalProperties": false and a "required" array covering all declared properties) and the provider accepts loose schemas.
func (*ChatBuilder) ResponseText ¶ added in v0.13.0
func (b *ChatBuilder) ResponseText() *ChatBuilder
ResponseText explicitly sets the response format to text (the default). This is mainly useful for clearing a previously set JSON format.
func (*ChatBuilder) SearchOptions ¶ added in v1.0.0
func (b *ChatBuilder) SearchOptions(opts *SearchOptions) *ChatBuilder
SearchOptions configures web-search grounding for providers that support FeatureWebSearch (currently Perplexity). Passing a nil pointer clears any previously set options. Requests carrying search options against a provider without the capability fail validation with ErrSearchUnsupported before any HTTP call is made.
Note: this is distinct from WebSearch(), which enables OpenAI's built-in web_search tool on the Responses API. SearchOptions controls how search-native providers ground their results.
resp, err := client.Chat(perplexity.ModelSonar).
User("Latest Go release notes").
SearchOptions(&core.SearchOptions{
SearchDomainFilter: []string{"go.dev"},
Recency: core.SearchRecencyMonth,
}).
GetResponse(ctx)
for _, url := range resp.Citations {
fmt.Println(url)
}
func (*ChatBuilder) Stream ¶
func (b *ChatBuilder) Stream(ctx context.Context) (*ChatStream, error)
Stream executes the chat request and returns a streaming response. It applies validation and telemetry.
The effective execution timeout (see effectiveTimeout for precedence) is applied to the context used to establish the stream. The derived timeout context is NOT cancelled when Stream returns; it stays alive until the stream completes (successfully or with an error) or the deadline fires, so the timeout does not truncate an in-flight stream prematurely.
A caller-supplied ctx deadline is never remapped to ErrTimeout — only a deadline applied internally by Iris is. To apply a timeout externally instead:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
stream, err := client.Chat(model).User("...").Stream(ctx)
func (*ChatBuilder) System ¶
func (b *ChatBuilder) System(s string) *ChatBuilder
System appends a system message.
func (*ChatBuilder) Temperature ¶
func (b *ChatBuilder) Temperature(v float32) *ChatBuilder
Temperature sets the temperature parameter.
func (*ChatBuilder) Timeout ¶ added in v0.10.0
func (b *ChatBuilder) Timeout(d time.Duration) *ChatBuilder
Timeout sets an optional timeout for the request. When set, GetResponse and Stream will create a context with this timeout if a context.Background() or context without deadline is passed. This provides a convenient alternative to manually creating contexts:
// Instead of:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := client.Chat(model).User("Hello").GetResponse(ctx)
// You can write:
resp, err := client.Chat(model).User("Hello").Timeout(30*time.Second).GetResponse(context.Background())
func (*ChatBuilder) ToolError ¶ added in v0.11.0
func (b *ChatBuilder) ToolError(assistantResp *ChatResponse, callID string, err error) *ChatBuilder
ToolError is a convenience method for adding a single tool error result. Returns a new builder (immutable).
func (*ChatBuilder) ToolResult ¶ added in v0.11.0
func (b *ChatBuilder) ToolResult(assistantResp *ChatResponse, callID string, content any) *ChatBuilder
ToolResult is a convenience method for adding a single successful tool result. Returns a new builder (immutable).
func (*ChatBuilder) ToolResults ¶ added in v0.11.0
func (b *ChatBuilder) ToolResults(assistantResp *ChatResponse, results []ToolResult) *ChatBuilder
ToolResults returns a new ChatBuilder with tool execution results appended. This automatically formats results according to the provider's expected format. The assistant message containing the original tool calls is automatically included.
IMPORTANT: This method returns a NEW builder (immutable). The original builder is unchanged.
If fewer results are provided than tool calls, a warning is emitted via the client warning handler. If result IDs don't match any tool calls, a warning is emitted via the client warning handler.
func (*ChatBuilder) Tools ¶
func (b *ChatBuilder) Tools(ts ...Tool) *ChatBuilder
Tools sets the tools available for the request. It copies the caller's slice, so later changes to the slice do not affect the request. The Tool values are retained as interfaces and must not be mutated while the builder is in use.
func (*ChatBuilder) Truncation ¶
func (b *ChatBuilder) Truncation(mode string) *ChatBuilder
Truncation sets the truncation mode for the request.
func (*ChatBuilder) User ¶
func (b *ChatBuilder) User(s string) *ChatBuilder
User appends a user message.
func (*ChatBuilder) UserMultimodal ¶
func (b *ChatBuilder) UserMultimodal() *MessageBuilder
UserMultimodal starts building a multimodal user message.
func (*ChatBuilder) UserWithFileID ¶
func (b *ChatBuilder) UserWithFileID(text, fileID string) *ChatBuilder
UserWithFileID adds a user message with text and a file ID. This is a convenience method for document analysis with uploaded files.
func (*ChatBuilder) UserWithFileURL ¶
func (b *ChatBuilder) UserWithFileURL(text, fileURL string) *ChatBuilder
UserWithFileURL adds a user message with text and a file URL. This is a convenience method for document analysis use cases.
func (*ChatBuilder) UserWithImageFileID ¶
func (b *ChatBuilder) UserWithImageFileID(text, fileID string) *ChatBuilder
UserWithImageFileID adds a user message with text and an image file ID. This is a convenience method for vision use cases with uploaded files.
func (*ChatBuilder) UserWithImageURL ¶
func (b *ChatBuilder) UserWithImageURL(text, imageURL string) *ChatBuilder
UserWithImageURL adds a user message with text and an image URL. This is a convenience method for common vision use cases.
func (*ChatBuilder) WebSearch ¶
func (b *ChatBuilder) WebSearch() *ChatBuilder
WebSearch adds the web_search built-in tool.
type ChatChunk ¶
type ChatChunk struct {
Delta string `json:"delta"`
}
ChatChunk represents an incremental streaming response. Delta contains incremental assistant text.
type ChatRequest ¶
type ChatRequest struct {
Model ModelID `json:"model"`
Messages []Message `json:"messages"`
Temperature *float32 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Tools []Tool `json:"-"` // Tools are handled separately by providers
// Structured output fields
ResponseFormat ResponseFormat `json:"response_format,omitempty"`
JSONSchema *JSONSchemaDefinition `json:"json_schema,omitempty"`
// Responses API fields (ignored for Chat Completions API)
Instructions string `json:"instructions,omitempty"`
ReasoningEffort ReasoningEffort `json:"reasoning_effort,omitempty"`
BuiltInTools []BuiltInTool `json:"builtin_tools,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
Truncation string `json:"truncation,omitempty"`
ToolResources *ToolResources `json:"tool_resources,omitempty"`
// Web-search grounding options for providers that support
// FeatureWebSearch (currently Perplexity). Requests carrying search
// options against a provider without the capability are rejected by the
// builder with ErrSearchUnsupported before any HTTP call is made.
SearchOptions *SearchOptions `json:"search_options,omitempty"`
}
ChatRequest represents a request to a chat model.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Model ModelID `json:"model"`
Output string `json:"output"`
Usage TokenUsage `json:"usage"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// Citations contains the source URLs consulted by search-grounded
// providers (currently Perplexity). Empty for providers without search.
Citations []string `json:"citations,omitempty"`
// Responses API fields
Reasoning *ReasoningOutput `json:"reasoning,omitempty"`
Status string `json:"status,omitempty"`
}
ChatResponse represents a response from a chat model. For providers returning multiple choices, v0.1 uses only the first choice.
func DrainStream ¶
func DrainStream(ctx context.Context, s *ChatStream) (*ChatResponse, error)
DrainStream accumulates all deltas and returns the final ChatResponse. Blocks until stream completes or context cancels. Returns ErrNilStream when s is nil.
Behavior:
- Read all chunks from Ch, accumulating Delta into output string
- Keep reading after Ch closes until BOTH Err and Final have resolved (closed or delivered a value) — mirrors wrapStreamWithTelemetry's non-lossy discipline so a real stream error delivered after Ch closes is never missed.
- If Final closes without a value, that's not itself an error; Err is still checked independently.
- If Final includes Output, use it; otherwise use accumulated deltas.
- On context cancellation, do one final non-blocking drain of Err (and Final) so a concrete provider error wins over a generic context error, then return ctx.Err().
func (*ChatResponse) FirstToolCall ¶ added in v0.10.0
func (r *ChatResponse) FirstToolCall() *ToolCall
FirstToolCall returns the first tool call, or nil if there are none. This is convenient for single-tool scenarios:
if tc := resp.FirstToolCall(); tc != nil {
// handle tool call
}
func (*ChatResponse) HasCitations ¶ added in v1.0.0
func (r *ChatResponse) HasCitations() bool
HasCitations reports whether the response carries search citations.
func (*ChatResponse) HasReasoning ¶ added in v0.10.0
func (r *ChatResponse) HasReasoning() bool
HasReasoning reports whether the response contains reasoning output.
func (*ChatResponse) HasToolCalls ¶ added in v0.10.0
func (r *ChatResponse) HasToolCalls() bool
HasToolCalls reports whether the response contains any tool calls.
type ChatStream ¶
type ChatStream struct {
// Ch emits text deltas in order. Closed when stream ends.
Ch <-chan ChatChunk
// Err emits at most one error. MUST be closed when stream ends.
// If error occurs after setup, send on Err then close all channels.
Err <-chan error
// Final sent exactly once (or zero if setup fails) after stream completion.
// Includes usage and tool calls if available.
// Providers may send partial ChatResponse with Output empty.
Final <-chan *ChatResponse
}
ChatStream represents a streaming response from a provider.
Channel Rules:
- Providers MUST close Ch, Err, and Final when finished
- On context cancellation, providers MUST terminate promptly and close channels
- Err channel emits at most one error
- Final channel emits exactly once on success (or zero times on setup failure)
- If providers cannot compute Usage for streaming, they MAY leave it zeroed
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main entry point for interacting with LLM providers. Client is safe for concurrent use.
func NewClient ¶
func NewClient(p Provider, opts ...ClientOption) *Client
NewClient creates a new Client with the given provider and options.
func (*Client) Chat ¶
func (c *Client) Chat(model ModelID) *ChatBuilder
Chat returns a ChatBuilder for constructing and executing a chat request.
func (*Client) Embed ¶ added in v1.0.0
func (c *Client) Embed(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
Embed generates embeddings through the client's provider. It applies the client timeout, telemetry, and retry policy to the optional embedding capability just as GetResponse does for chat requests.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithRateLimiter ¶ added in v1.0.0
func WithRateLimiter(l RateLimiter) ClientOption
WithRateLimiter configures a limiter used before every provider attempt, including retries. Pass nil to leave rate limiting disabled.
func WithRetryPolicy ¶
func WithRetryPolicy(r RetryPolicy) ClientOption
WithRetryPolicy sets the retry policy for the client.
func WithStreamIdleTimeout ¶ added in v0.17.0
func WithStreamIdleTimeout(d time.Duration) ClientOption
WithStreamIdleTimeout sets a stall/idle timeout for streaming calls: if a stream produces no chunk for at least d, it is terminated and the resulting error (delivered on the stream's Err channel, and returned by DrainStream) satisfies errors.Is(err, ErrTimeout). This is independent of WithTimeout, which bounds the overall call; WithStreamIdleTimeout instead bounds the gap between successive chunks, so a provider that goes silent mid-stream is caught well before a long overall timeout would elapse.
Pass 0 (the default) to disable idle detection.
func WithTelemetry ¶
func WithTelemetry(h TelemetryHook) ClientOption
WithTelemetry sets the telemetry hook for the client.
func WithTimeout ¶ added in v0.15.0
func WithTimeout(d time.Duration) ClientOption
WithTimeout sets the default execution timeout applied to calls routed through Client when the caller's context has no deadline of its own. Chat builders can override it per call with Timeout(). Pass 0 to disable the default and allow unbounded calls.
func WithWarningHandler ¶ added in v0.12.0
func WithWarningHandler(h WarningHandler) ClientOption
WithWarningHandler sets a handler for non-fatal SDK warnings. Pass nil to keep the default no-op handler.
type ContentPart ¶
type ContentPart interface {
// ContentType returns the type identifier for this content part.
ContentType() string
}
ContentPart represents a part of multimodal content in a message. Types implementing this interface can be used in multimodal messages.
type ContentPartSupporter ¶ added in v1.0.0
type ContentPartSupporter interface {
SupportsContentPart(model ModelID, role Role, part ContentPart) bool
}
ContentPartSupporter is an optional provider capability interface for declaring which message parts a provider can transmit for a model. The client warns through WarningHandler before dispatching any part for which support is not declared.
func AsContentPartSupporter ¶ added in v1.0.0
func AsContentPartSupporter(p Provider) (ContentPartSupporter, bool)
AsContentPartSupporter discovers per-part support on a Provider or its unwrap chain. It returns nil and false when no provider declares that support.
type ContextualTelemetryHook ¶ added in v0.14.0
type ContextualTelemetryHook interface {
TelemetryHook
// OnRequestStartWithContext is called when a request to a provider begins.
// Returns a new context that should be used for the request.
// The returned context may contain span information for propagation.
OnRequestStartWithContext(ctx context.Context, e RequestStartEvent) context.Context
// OnRequestEndWithContext is called when a request to a provider completes.
OnRequestEndWithContext(ctx context.Context, e RequestEndEvent)
}
ContextualTelemetryHook extends TelemetryHook with context support. Implementations that need access to context.Context (e.g., OpenTelemetry for span creation and propagation) should implement this interface.
The Client checks for this interface at runtime and uses it when available, falling back to the base TelemetryHook methods otherwise. This ensures backward compatibility with existing TelemetryHook implementations.
Context Propagation ¶
OnRequestStartWithContext returns a new context that may contain span information or other telemetry-related data. The returned context is passed through the request lifecycle and provided to OnRequestEndWithContext.
Security ¶
Like TelemetryHook, implementations must NOT capture sensitive data (API keys, prompts, responses) in spans or other telemetry output.
type ContextualizedEmbeddingProvider ¶
type ContextualizedEmbeddingProvider interface {
// CreateContextualizedEmbeddings generates context-aware embeddings for document chunks.
// Each inner slice in Inputs represents chunks from a single document.
CreateContextualizedEmbeddings(ctx context.Context, req *ContextualizedEmbeddingRequest) (*ContextualizedEmbeddingResponse, error)
}
ContextualizedEmbeddingProvider is an optional interface for providers that support context-aware chunk embeddings. These embeddings encode not just the local content of each chunk, but also context from surrounding chunks in the same document.
func AsContextualizedEmbeddingProvider ¶ added in v1.0.0
func AsContextualizedEmbeddingProvider(p Provider) (ContextualizedEmbeddingProvider, bool)
AsContextualizedEmbeddingProvider discovers contextualized embeddings on a Provider or its unwrap chain. It returns nil and false when no provider implements contextualized embeddings.
type ContextualizedEmbeddingRequest ¶
type ContextualizedEmbeddingRequest struct {
// Model specifies the embedding model to use.
Model ModelID `json:"model"`
// Inputs is a list of documents, where each document is a list of chunks.
// Chunks within the same document are encoded with awareness of each other.
Inputs [][]string `json:"inputs"`
// InputType specifies whether inputs are queries or documents.
InputType InputType `json:"input_type,omitempty"`
// OutputDimension specifies the number of dimensions for output embeddings.
OutputDimension *int `json:"output_dimension,omitempty"`
// OutputDType specifies the data type for embeddings (float, int8, etc.).
OutputDType OutputDType `json:"output_dtype,omitempty"`
// EncodingFormat specifies the encoding format (float array or base64).
EncodingFormat EncodingFormat `json:"encoding_format,omitempty"`
}
ContextualizedEmbeddingRequest represents a request to generate contextualized embeddings.
type ContextualizedEmbeddingResponse ¶
type ContextualizedEmbeddingResponse struct {
// Embeddings contains the vectors grouped by document, then by chunk.
// embeddings[i][j] is the embedding for chunk j of document i.
Embeddings [][]EmbeddingVector `json:"embeddings"`
// Model is the model that generated the embeddings.
Model ModelID `json:"model"`
// Usage contains token consumption information.
Usage EmbeddingUsage `json:"usage"`
}
ContextualizedEmbeddingResponse contains the generated contextualized embeddings.
type Conversation ¶ added in v0.11.0
type Conversation struct {
// contains filtered or unexported fields
}
Conversation provides a high-level API for managing multi-turn conversations with automatic history management.
func NewConversation ¶ added in v0.11.0
func NewConversation(ctx context.Context, client *Client, model ModelID, opts ...ConversationOption) *Conversation
NewConversation creates a new conversation session with the given client and model. ctx is passed to the configured Memory when initializing a system message.
func (*Conversation) AddToolResults ¶ added in v1.0.0
func (c *Conversation) AddToolResults(ctx context.Context, results []ToolResult)
AddToolResults appends tool execution results to the conversation. Call it after Send or Stream returns assistant tool calls and before sending the next user message. The results slice is copied before it is stored.
func (*Conversation) Clear ¶ added in v0.11.0
func (c *Conversation) Clear(ctx context.Context)
Clear resets the conversation history. If a system message was provided, it will be re-added.
func (*Conversation) GetHistory ¶ added in v0.11.0
func (c *Conversation) GetHistory(ctx context.Context) []Message
GetHistory returns the full conversation history.
func (*Conversation) MessageCount ¶ added in v0.11.0
func (c *Conversation) MessageCount(ctx context.Context) int
MessageCount returns the number of messages in the conversation.
func (*Conversation) Send ¶ added in v0.11.0
func (c *Conversation) Send(ctx context.Context, userMessage string) (*ChatResponse, error)
Send sends a user message with ctx and returns the assistant's response. It automatically manages conversation history, preserving multimodal and tool-call turns when replaying earlier messages.
func (*Conversation) SendWithContext
deprecated
added in
v0.11.0
func (c *Conversation) SendWithContext(ctx context.Context, userMessage string) (*ChatResponse, error)
SendWithContext delegates to Send.
Deprecated: use Send.
func (*Conversation) Stream ¶ added in v0.13.0
func (c *Conversation) Stream(ctx context.Context, userMessage string) (*ChatStream, error)
Stream sends a user message with ctx and returns a streaming response. The stream is wrapped to add the assistant's complete text and tool calls to conversation history when it finishes successfully.
func (*Conversation) StreamWithContext
deprecated
added in
v0.13.0
func (c *Conversation) StreamWithContext(ctx context.Context, userMessage string) (*ChatStream, error)
StreamWithContext delegates to Stream.
Deprecated: use Stream.
type ConversationOption ¶ added in v0.11.0
type ConversationOption func(*Conversation)
ConversationOption configures a Conversation.
func WithMemoryStore ¶ added in v0.11.0
func WithMemoryStore(memory Memory) ConversationOption
WithMemoryStore sets a custom memory store for the conversation.
func WithSystemMessage ¶ added in v0.11.0
func WithSystemMessage(system string) ConversationOption
WithSystemMessage sets a system message for the conversation.
type EmbeddingInput ¶
type EmbeddingInput struct {
Text string `json:"text"`
ID string `json:"id,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
EmbeddingInput represents a single text to embed with optional metadata.
type EmbeddingProvider ¶
type EmbeddingProvider interface {
// CreateEmbeddings generates embeddings for the given input texts.
CreateEmbeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
}
EmbeddingProvider is an optional interface for providers that support embeddings.
func AsEmbeddingProvider ¶ added in v1.0.0
func AsEmbeddingProvider(p Provider) (EmbeddingProvider, bool)
AsEmbeddingProvider discovers embeddings on a Provider or its unwrap chain. It returns nil and false when no provider implements embeddings.
type EmbeddingRequest ¶
type EmbeddingRequest struct {
Model ModelID `json:"model"`
Input []EmbeddingInput `json:"input"`
EncodingFormat EncodingFormat `json:"encoding_format,omitempty"`
Dimensions *int `json:"dimensions,omitempty"`
User string `json:"user,omitempty"`
InputType InputType `json:"input_type,omitempty"`
OutputDType OutputDType `json:"output_dtype,omitempty"`
Truncation *bool `json:"truncation,omitempty"`
}
EmbeddingRequest represents a request to generate embeddings.
type EmbeddingResponse ¶
type EmbeddingResponse struct {
Vectors []EmbeddingVector `json:"vectors"`
Model ModelID `json:"model"`
Usage EmbeddingUsage `json:"usage"`
}
EmbeddingResponse contains the generated embeddings.
type EmbeddingUsage ¶
type EmbeddingUsage struct {
PromptTokens int `json:"prompt_tokens"`
TotalTokens int `json:"total_tokens"`
}
EmbeddingUsage tracks token consumption for embeddings.
type EmbeddingVector ¶
type EmbeddingVector struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Vector []float32 `json:"vector,omitempty"`
VectorB64 string `json:"vector_b64,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
EmbeddingVector represents a single embedding result.
type EncodingFormat ¶
type EncodingFormat string
EncodingFormat specifies the embedding output format.
const ( // EncodingFormatFloat returns embeddings as float arrays. EncodingFormatFloat EncodingFormat = "float" // EncodingFormatBase64 returns embeddings as base64-encoded strings. EncodingFormatBase64 EncodingFormat = "base64" )
type Feature ¶
type Feature string
Feature represents a capability that a provider may support.
const ( FeatureChat Feature = "chat" FeatureChatStreaming Feature = "chat_streaming" FeatureToolCalling Feature = "tool_calling" FeatureReasoning Feature = "reasoning" FeatureBuiltInTools Feature = "builtin_tools" FeatureResponseChain Feature = "response_chain" FeatureTokenCounting Feature = "token_counting" FeatureEmbeddings Feature = "embeddings" FeatureContextualizedEmbeddings Feature = "contextualized_embeddings" FeatureReranking Feature = "reranking" FeatureStructuredOutput Feature = "structured_output" FeatureBatch Feature = "batch" FeatureWebSearch Feature = "web_search" )
const FeatureImageGeneration Feature = "image_generation"
FeatureImageGeneration indicates support for image generation.
type FileSearchResources ¶
type FileSearchResources struct {
VectorStoreIDs []string `json:"vector_store_ids"`
}
FileSearchResources contains vector store IDs for file search.
type ImageAction ¶
type ImageAction string
ImageAction represents the action to take (for Responses API).
const ( ImageActionAuto ImageAction = "auto" ImageActionGenerate ImageAction = "generate" ImageActionEdit ImageAction = "edit" )
type ImageBackground ¶
type ImageBackground string
ImageBackground represents the background style.
const ( ImageBackgroundOpaque ImageBackground = "opaque" ImageBackgroundTransparent ImageBackground = "transparent" ImageBackgroundAuto ImageBackground = "auto" )
type ImageChunk ¶
type ImageChunk struct {
PartialImageIndex int `json:"partial_image_index"`
B64JSON string `json:"b64_json"`
}
ImageChunk represents an incremental streaming response for images.
type ImageData ¶
type ImageData struct {
B64JSON string `json:"b64_json,omitempty"`
URL string `json:"url,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
}
ImageData represents a single generated image.
type ImageDetail ¶
type ImageDetail string
ImageDetail specifies the level of detail for image processing.
const ( // ImageDetailAuto lets the model decide the appropriate detail level. ImageDetailAuto ImageDetail = "auto" // ImageDetailLow uses fewer tokens for faster processing. ImageDetailLow ImageDetail = "low" // ImageDetailHigh uses more tokens for detailed analysis. ImageDetailHigh ImageDetail = "high" )
type ImageEditRequest ¶
type ImageEditRequest struct {
Model ModelID `json:"model"`
Prompt string `json:"prompt"`
// Image inputs - at least one required
Images []ImageInput `json:"-"` // Handled separately for multipart
// Optional mask for inpainting
Mask *ImageInput `json:"-"`
// Optional parameters
N int `json:"n,omitempty"`
Size ImageSize `json:"size,omitempty"`
Quality ImageQuality `json:"quality,omitempty"`
Format ImageFormat `json:"output_format,omitempty"`
Compression *int `json:"output_compression,omitempty"`
Background ImageBackground `json:"background,omitempty"`
InputFidelity ImageInputFidelity `json:"input_fidelity,omitempty"`
User string `json:"user,omitempty"`
}
ImageEditRequest represents a request to edit images.
type ImageFormat ¶
type ImageFormat string
ImageFormat represents the output file format.
const ( ImageFormatPNG ImageFormat = "png" ImageFormatJPEG ImageFormat = "jpeg" ImageFormatWebP ImageFormat = "webp" )
func (ImageFormat) IsValid ¶
func (f ImageFormat) IsValid() bool
IsValid reports whether the image format is a recognized value.
type ImageGenerateRequest ¶
type ImageGenerateRequest struct {
Model ModelID `json:"model"`
Prompt string `json:"prompt"`
// Optional parameters
N int `json:"n,omitempty"` // Number of images to generate (default 1)
Size ImageSize `json:"size,omitempty"` // Image dimensions
Quality ImageQuality `json:"quality,omitempty"` // Rendering quality
Format ImageFormat `json:"output_format,omitempty"` // Output format
Compression *int `json:"output_compression,omitempty"` // 0-100 for jpeg/webp
Background ImageBackground `json:"background,omitempty"` // Transparency setting
Moderation string `json:"moderation,omitempty"` // "auto" or "low"
User string `json:"user,omitempty"` // User identifier
ResponseFormat string `json:"response_format,omitempty"` // "b64_json" or "url"
PartialImages int `json:"partial_images,omitempty"` // For streaming (0-3)
}
ImageGenerateRequest represents a request to generate images.
type ImageGenerator ¶
type ImageGenerator interface {
// GenerateImage generates images from a text prompt.
GenerateImage(ctx context.Context, req *ImageGenerateRequest) (*ImageResponse, error)
// EditImage edits existing images using a prompt and optional mask.
EditImage(ctx context.Context, req *ImageEditRequest) (*ImageResponse, error)
// StreamImage generates images with streaming partial results.
// Not all providers support streaming.
StreamImage(ctx context.Context, req *ImageGenerateRequest) (*ImageStream, error)
}
ImageGenerator is an optional interface for providers that support image generation.
func AsImageGenerator ¶ added in v1.0.0
func AsImageGenerator(p Provider) (ImageGenerator, bool)
AsImageGenerator discovers image operations on a Provider or its unwrap chain. It returns nil and false when no provider implements them.
type ImageInput ¶
type ImageInput struct {
// One of these must be set
Data []byte // Raw image bytes
Base64 string // Base64-encoded image
URL string // URL to fetch image from (Responses API only)
FileID string // File ID from Files API (Responses API only)
Filename string // Optional filename hint
}
ImageInput represents an input image for editing.
func (ImageInput) GetBytes ¶
func (i ImageInput) GetBytes() ([]byte, error)
GetBytes returns the image data as bytes.
type ImageInputFidelity ¶
type ImageInputFidelity string
ImageInputFidelity represents the input image preservation level.
const ( ImageInputFidelityLow ImageInputFidelity = "low" ImageInputFidelityHigh ImageInputFidelity = "high" )
type ImageQuality ¶
type ImageQuality string
ImageQuality represents the rendering quality level.
const ( ImageQualityLow ImageQuality = "low" ImageQualityMedium ImageQuality = "medium" ImageQualityHigh ImageQuality = "high" ImageQualityAuto ImageQuality = "auto" )
func (ImageQuality) IsValid ¶
func (q ImageQuality) IsValid() bool
IsValid reports whether the image quality is a recognized value.
type ImageResponse ¶
type ImageResponse struct {
Created int64 `json:"created"`
Data []ImageData `json:"data"`
Usage *ImageUsage `json:"usage,omitempty"`
}
ImageResponse represents a response containing generated images.
type ImageStream ¶
type ImageStream struct {
Ch <-chan ImageChunk // Partial images
Err <-chan error // At most one error
Final <-chan *ImageResponse // Complete response
}
ImageStream represents a streaming image generation response.
type ImageUsage ¶
type ImageUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens"`
}
ImageUsage tracks token usage for image generation.
type InMemoryStore ¶ added in v0.11.0
type InMemoryStore struct {
// contains filtered or unexported fields
}
InMemoryStore is a thread-safe in-memory implementation of the Memory interface. Operations complete synchronously, so their contexts are unused. It is suitable for single-session conversations that don't require persistence.
func NewInMemoryStore ¶ added in v0.11.0
func NewInMemoryStore() *InMemoryStore
NewInMemoryStore creates a new in-memory conversation store.
func (*InMemoryStore) AddMessage ¶ added in v0.11.0
func (m *InMemoryStore) AddMessage(_ context.Context, msg Message)
AddMessage appends a message to the conversation history.
func (*InMemoryStore) AddMessages ¶ added in v0.11.0
func (m *InMemoryStore) AddMessages(_ context.Context, msgs []Message)
AddMessages appends multiple messages to the conversation history.
func (*InMemoryStore) Clear ¶ added in v0.11.0
func (m *InMemoryStore) Clear(_ context.Context)
Clear removes all messages from the conversation.
func (*InMemoryStore) GetHistory ¶ added in v0.11.0
func (m *InMemoryStore) GetHistory(_ context.Context) []Message
GetHistory returns all messages in the conversation. Returns a copy of the messages slice.
func (*InMemoryStore) GetLastN ¶ added in v0.11.0
func (m *InMemoryStore) GetLastN(_ context.Context, n int) []Message
GetLastN returns the last N messages in the conversation. If N is greater than the number of messages, returns all messages.
func (*InMemoryStore) Len ¶ added in v0.11.0
func (m *InMemoryStore) Len(_ context.Context) int
Len returns the number of messages in the conversation.
func (*InMemoryStore) SetMessages ¶ added in v0.11.0
func (m *InMemoryStore) SetMessages(_ context.Context, msgs []Message)
SetMessages replaces the entire conversation history.
type InputFile ¶
type InputFile struct {
// FileID is a file ID from the Files API.
FileID string
// FileURL is an HTTPS URL to the file.
FileURL string
// FileData contains base64-encoded file bytes.
FileData string
// Filename is the recommended filename when using FileData.
Filename string
}
InputFile represents file content in a multimodal message.
func (InputFile) ContentType ¶
ContentType returns the type identifier for InputFile.
type InputImage ¶
type InputImage struct {
// ImageURL is an HTTPS URL or data URL (data:image/jpeg;base64,...).
ImageURL string
// FileID is a file ID from the Files API.
FileID string
// Detail specifies the level of detail for image processing.
Detail ImageDetail
}
InputImage represents image content in a multimodal message.
func (InputImage) ContentType ¶
func (i InputImage) ContentType() string
ContentType returns the type identifier for InputImage.
type InputText ¶
type InputText struct {
// Text is the text content.
Text string
}
InputText represents text content in a multimodal message.
func (InputText) ContentType ¶
ContentType returns the type identifier for InputText.
type InputType ¶
type InputType string
InputType specifies the type of input for retrieval optimization.
const ( // InputTypeNone uses default embedding without retrieval optimization. InputTypeNone InputType = "" // InputTypeQuery optimizes embeddings for search queries. InputTypeQuery InputType = "query" // InputTypeDocument optimizes embeddings for documents being searched. InputTypeDocument InputType = "document" )
type IntervalRateLimiter ¶ added in v1.0.0
type IntervalRateLimiter struct {
// contains filtered or unexported fields
}
IntervalRateLimiter spaces request starts by a fixed interval. It is safe for concurrent use and does not queue requests after their contexts expire.
func NewIntervalRateLimiter ¶ added in v1.0.0
func NewIntervalRateLimiter(interval time.Duration) *IntervalRateLimiter
NewIntervalRateLimiter creates a limiter that permits one request every interval. A non-positive interval disables waiting.
type JSONSchemaDefinition ¶ added in v0.13.0
type JSONSchemaDefinition struct {
// Name is a required identifier for the schema (used by some providers).
Name string `json:"name"`
// Description explains what the schema represents (optional).
Description string `json:"description,omitempty"`
// Schema is the JSON Schema definition.
Schema json.RawMessage `json:"schema"`
// Strict enables strict schema validation (recommended).
// When true, the model will always output valid JSON matching the schema.
// The tag omits omitempty so an explicit false is still marshaled.
Strict bool `json:"strict"`
}
JSONSchemaDefinition represents a JSON Schema for structured output. When provided, the model's output will conform to this schema.
type Memory ¶ added in v0.11.0
type Memory interface {
// AddMessage appends a message to the conversation history.
AddMessage(ctx context.Context, msg Message)
// AddMessages appends multiple messages to the conversation history.
AddMessages(ctx context.Context, msgs []Message)
// GetHistory returns all messages in the conversation.
GetHistory(ctx context.Context) []Message
// GetLastN returns the last N messages in the conversation.
GetLastN(ctx context.Context, n int) []Message
// Clear removes all messages from the conversation.
Clear(ctx context.Context)
// Len returns the number of messages in the conversation.
Len(ctx context.Context) int
// SetMessages replaces the entire conversation history.
SetMessages(ctx context.Context, msgs []Message)
}
Memory is the interface for managing conversation history. Implementations provide different storage backends (in-memory, Redis, PostgreSQL, etc.). Every operation receives the caller's context so remote stores can honor cancellation and deadlines and propagate trace metadata.
type Message ¶
type Message struct {
Role Role `json:"role"`
Content string `json:"content,omitempty"`
Parts []ContentPart `json:"-"` // Provider-mapped multimodal content parts
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For assistant messages requesting tools
ToolResults []ToolResult `json:"tool_results,omitempty"` // For tool result messages (RoleTool)
}
Message represents a single message in a conversation. For simple text messages, use Content. For multimodal messages, use Parts. If Parts is non-empty, Content is ignored.
type MessageBuilder ¶
type MessageBuilder struct {
// contains filtered or unexported fields
}
MessageBuilder provides a fluent API for building multimodal messages.
func (*MessageBuilder) Done ¶
func (m *MessageBuilder) Done() *ChatBuilder
Done completes the message and returns to the ChatBuilder.
func (*MessageBuilder) FileBase64 ¶
func (m *MessageBuilder) FileBase64(filename, base64Data string) *MessageBuilder
FileBase64 adds a file with base64-encoded content.
func (*MessageBuilder) FileID ¶
func (m *MessageBuilder) FileID(fileID string) *MessageBuilder
FileID adds a file by file ID from the Files API.
func (*MessageBuilder) FileURL ¶
func (m *MessageBuilder) FileURL(url string) *MessageBuilder
FileURL adds a file by URL.
func (*MessageBuilder) ImageFileID ¶
func (m *MessageBuilder) ImageFileID(fileID string) *MessageBuilder
ImageFileID adds an image by file ID from the Files API.
func (*MessageBuilder) ImageFileIDWithDetail ¶
func (m *MessageBuilder) ImageFileIDWithDetail(fileID string, detail ImageDetail) *MessageBuilder
ImageFileIDWithDetail adds an image by file ID with a specific detail level.
func (*MessageBuilder) ImageURL ¶
func (m *MessageBuilder) ImageURL(url string) *MessageBuilder
ImageURL adds an image by URL (HTTPS or data URL).
func (*MessageBuilder) ImageURLWithDetail ¶
func (m *MessageBuilder) ImageURLWithDetail(url string, detail ImageDetail) *MessageBuilder
ImageURLWithDetail adds an image by URL with a specific detail level.
func (*MessageBuilder) Text ¶
func (m *MessageBuilder) Text(s string) *MessageBuilder
Text adds a text content part to the message.
type ModelID ¶
type ModelID string
ModelID is a string identifier for a model. Using string avoids coupling to provider-specific enums.
type ModelInfo ¶
type ModelInfo struct {
ID ModelID `json:"id"`
DisplayName string `json:"display_name"`
Capabilities []Feature `json:"capabilities"`
APIEndpoint APIEndpoint `json:"api_endpoint,omitempty"` // defaults to completions
}
ModelInfo describes a model available from a provider.
func (ModelInfo) GetAPIEndpoint ¶
func (m ModelInfo) GetAPIEndpoint() APIEndpoint
GetAPIEndpoint returns the API endpoint for the model, defaulting to completions.
func (ModelInfo) HasCapability ¶
HasCapability reports whether the model supports the given feature.
type NoopTelemetryHook ¶
type NoopTelemetryHook struct{}
NoopTelemetryHook is a no-op implementation of TelemetryHook. Use this as a default when no telemetry is configured.
func (NoopTelemetryHook) OnRequestEnd ¶
func (NoopTelemetryHook) OnRequestEnd(RequestEndEvent)
OnRequestEnd does nothing.
func (NoopTelemetryHook) OnRequestStart ¶
func (NoopTelemetryHook) OnRequestStart(RequestStartEvent)
OnRequestStart does nothing.
type OutputDType ¶
type OutputDType string
OutputDType specifies the data type for embedding vectors.
const ( // OutputDTypeFloat returns 32-bit floating point numbers (default). OutputDTypeFloat OutputDType = "float" // OutputDTypeInt8 returns 8-bit signed integers (-128 to 127). OutputDTypeInt8 OutputDType = "int8" // OutputDTypeUint8 returns 8-bit unsigned integers (0 to 255). OutputDTypeUint8 OutputDType = "uint8" // OutputDTypeBinary returns bit-packed signed integers. OutputDTypeBinary OutputDType = "binary" // OutputDTypeUbinary returns bit-packed unsigned integers. OutputDTypeUbinary OutputDType = "ubinary" )
type Provider ¶
type Provider interface {
// ID returns the provider identifier (e.g., "openai", "anthropic").
ID() string
// Models returns the list of models available from this provider.
Models() []ModelInfo
// Supports reports whether the provider supports the given feature.
Supports(feature Feature) bool
// Chat sends a non-streaming chat request.
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
// StreamChat sends a streaming chat request.
StreamChat(ctx context.Context, req *ChatRequest) (*ChatStream, error)
}
Provider is the interface that LLM providers must implement. Providers SHOULD be safe for concurrent calls. If a provider cannot be concurrent-safe, it MUST document this.
type ProviderError ¶
type ProviderError struct {
Provider string
Status int
RequestID string
Code string
Message string
Body string // raw response body (truncated); preserved when Message lacks detail
RetryAfter time.Duration // server-advised minimum delay before retrying
Err error
}
ProviderError represents an error returned by a provider with full context.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
Error implements the error interface.
func (*ProviderError) Unwrap ¶
func (e *ProviderError) Unwrap() error
Unwrap returns the underlying error for error chaining.
type ProviderUnwrapper ¶ added in v1.0.0
type ProviderUnwrapper interface {
Unwrap() Provider
}
ProviderUnwrapper is implemented by provider decorators that expose the provider they wrap. Capability helpers follow this chain so optional interfaces remain discoverable through decorators.
type RateLimiter ¶ added in v1.0.0
RateLimiter controls when the client may start an outgoing provider attempt. Wait must return context cancellation or deadline errors promptly when ctx is done. The client calls Wait for the initial request and every retry attempt.
type RateLimiterFunc ¶ added in v1.0.0
RateLimiterFunc adapts a function into a RateLimiter.
type ReasoningEffort ¶
type ReasoningEffort string
ReasoningEffort represents the level of reasoning effort for models that support it.
const ( ReasoningEffortNone ReasoningEffort = "none" ReasoningEffortLow ReasoningEffort = "low" ReasoningEffortMedium ReasoningEffort = "medium" ReasoningEffortHigh ReasoningEffort = "high" ReasoningEffortXHigh ReasoningEffort = "xhigh" )
type ReasoningOutput ¶
ReasoningOutput contains reasoning information from the model.
type RequestEndEvent ¶
type RequestEndEvent struct {
Provider string // Provider identifier
Model ModelID // Model that was called
Start time.Time // When the request started
End time.Time // When the request completed
Usage TokenUsage // Token consumption
Err error // Error if request failed, nil on success
}
RequestEndEvent contains metadata about a completed request.
Security ¶
This struct intentionally excludes:
- API keys (never logged)
- Response content (may be sensitive)
- Raw HTTP response data
The Err field contains error types, not raw error messages from providers which might inadvertently include sensitive data.
func (RequestEndEvent) Duration ¶
func (e RequestEndEvent) Duration() time.Duration
Duration returns the elapsed time for the request.
type RequestStartEvent ¶
type RequestStartEvent struct {
Provider string // Provider identifier (e.g., "openai", "anthropic")
Model ModelID // Model being called
Start time.Time // When the request started
}
RequestStartEvent contains metadata about a starting request.
Security ¶
This struct intentionally excludes:
- API keys (never logged)
- Prompt/message content (privacy sensitive)
- Request headers (may contain auth tokens)
Only operational metadata suitable for logging is included.
type RerankRequest ¶
type RerankRequest struct {
// Model specifies the reranker model to use.
Model ModelID `json:"model"`
// Query is the search query to rank documents against.
Query string `json:"query"`
// Documents is the list of documents to rerank.
Documents []string `json:"documents"`
// TopK limits the number of results to return. If nil, returns all.
TopK *int `json:"top_k,omitempty"`
// ReturnDocuments includes document text in the response if true.
ReturnDocuments bool `json:"return_documents,omitempty"`
// Truncation controls whether to truncate inputs exceeding context length.
// Defaults to true.
Truncation *bool `json:"truncation,omitempty"`
}
RerankRequest represents a request to rerank documents.
type RerankResponse ¶
type RerankResponse struct {
// Results contains the reranked documents sorted by descending relevance.
Results []RerankResult `json:"results"`
// Model is the model that performed the reranking.
Model ModelID `json:"model"`
// Usage contains token consumption information.
Usage RerankUsage `json:"usage"`
}
RerankResponse contains the reranking results.
type RerankResult ¶
type RerankResult struct {
// Index is the original position in the input documents slice.
Index int `json:"index"`
// RelevanceScore is the relevance score (higher is more relevant).
RelevanceScore float64 `json:"relevance_score"`
// Document contains the document text if ReturnDocuments was true.
Document string `json:"document,omitempty"`
}
RerankResult represents a single document's reranking result.
type RerankUsage ¶
type RerankUsage struct {
// TotalTokens is the total number of tokens used.
TotalTokens int `json:"total_tokens"`
}
RerankUsage tracks token consumption for a rerank request.
type RerankerProvider ¶
type RerankerProvider interface {
// Rerank scores and sorts documents by relevance to the query.
// Results are returned in descending order of relevance.
Rerank(ctx context.Context, req *RerankRequest) (*RerankResponse, error)
}
RerankerProvider is an optional interface for providers that support semantic reranking of documents based on query relevance.
func AsReranker ¶ added in v1.0.0
func AsReranker(p Provider) (RerankerProvider, bool)
AsReranker discovers reranking on a Provider or its unwrap chain. It returns nil and false when no provider implements reranking.
type ResponseFormat ¶ added in v0.13.0
type ResponseFormat string
ResponseFormat specifies the output format constraint for chat responses.
const ( // ResponseFormatText is the default format with no constraints. ResponseFormatText ResponseFormat = "text" // ResponseFormatJSON forces the model to output valid JSON. ResponseFormatJSON ResponseFormat = "json_object" // ResponseFormatJSONSchema forces output matching a specific JSON Schema. ResponseFormatJSONSchema ResponseFormat = "json_schema" )
type RetryConfig ¶
type RetryConfig struct {
MaxRetries int // Maximum number of retry attempts (default: 3)
BaseDelay time.Duration // Initial delay before first retry (default: 1s)
MaxDelay time.Duration // Maximum delay cap (default: 30s)
Jitter float64 // Jitter factor 0.0-1.0 (default: 0.2)
}
RetryConfig configures retry behavior.
type RetryPolicy ¶
type RetryPolicy interface {
// NextDelay returns the delay before the next retry attempt and whether to retry.
// If ok is false, no more retries should be attempted.
// attempt starts at 0 for the first retry after the initial failure.
// The default policy retries only errors explicitly classified as transient;
// custom policies may opt into retrying additional error types.
NextDelay(attempt int, err error) (delay time.Duration, ok bool)
}
RetryPolicy determines retry behavior for failed requests.
func DefaultRetryPolicy ¶
func DefaultRetryPolicy() RetryPolicy
DefaultRetryPolicy returns a retry policy with sensible defaults. Uses exponential backoff with jitter, max 3 retries, 30s max delay. It retries network, rate-limit, and server errors (including HTTP 429 and 5xx provider errors). Unknown errors are not retried by default.
func NewRetryPolicy ¶
func NewRetryPolicy(cfg RetryConfig) RetryPolicy
NewRetryPolicy creates a retry policy with the given configuration.
type SearchMode ¶ added in v1.0.0
type SearchMode string
SearchMode selects the corpus searched by search-grounded providers.
const ( // SearchModeWeb searches the general web (default). SearchModeWeb SearchMode = "web" // SearchModeAcademic searches academic sources (Perplexity). SearchModeAcademic SearchMode = "academic" // SearchModeSEC searches SEC filings (Perplexity). SearchModeSEC SearchMode = "sec" )
Supported search modes.
type SearchOptions ¶ added in v1.0.0
type SearchOptions struct {
// SearchDomainFilter restricts results to the listed domains. A domain
// prefixed with "-" is excluded instead of included (e.g. "-example.com").
SearchDomainFilter []string `json:"search_domain_filter,omitempty"`
// Recency limits results by freshness (hour, day, week, month, year).
Recency SearchRecencyFilter `json:"search_recency_filter,omitempty"`
// Mode selects the search corpus: web (default), academic, or sec.
Mode SearchMode `json:"search_mode,omitempty"`
}
SearchOptions configures web-search grounding for providers that support FeatureWebSearch (currently Perplexity). Set them via ChatBuilder.SearchOptions; requests carrying search options against a provider without the capability are rejected with ErrSearchUnsupported before any HTTP call is made.
type SearchRecencyFilter ¶ added in v1.0.0
type SearchRecencyFilter string
SearchRecencyFilter limits web-search results by freshness for search-grounded providers (currently Perplexity).
const ( SearchRecencyHour SearchRecencyFilter = "hour" SearchRecencyDay SearchRecencyFilter = "day" SearchRecencyWeek SearchRecencyFilter = "week" SearchRecencyMonth SearchRecencyFilter = "month" SearchRecencyYear SearchRecencyFilter = "year" )
Supported search recency filters.
type Secret ¶ added in v0.10.0
type Secret struct {
// contains filtered or unexported fields
}
Secret wraps a sensitive string value with protection against accidental logging. The underlying value is never exposed through String(), GoString(), or JSON marshaling.
Use Expose() to access the actual value when needed (e.g., for HTTP headers).
Example:
secret := NewSecret("sk-abc123")
fmt.Println(secret) // prints: [REDACTED]
fmt.Printf("%#v", secret) // prints: core.Secret{[REDACTED]}
secret.Expose() // returns: "sk-abc123"
func NewSecret ¶ added in v0.10.0
NewSecret creates a new Secret from a string value. Surrounding whitespace (including trailing newlines commonly introduced by secret managers such as Azure Key Vault) is trimmed so downstream comparisons and HTTP headers never carry accidental corruption.
func (Secret) Expose ¶ added in v0.10.0
Expose returns the actual secret value. Use this only when the value is genuinely needed (e.g., for authentication headers).
Security note: Be careful not to log or serialize the returned value.
func (Secret) GoString ¶ added in v0.10.0
GoString returns a redacted placeholder for %#v formatting. Implements fmt.GoStringer.
func (Secret) IsEmpty ¶ added in v0.10.0
IsEmpty returns true if the secret value is empty or whitespace-only.
func (Secret) MarshalJSON ¶ added in v0.10.0
MarshalJSON returns a redacted JSON string. This prevents accidental JSON serialization of the secret value.
func (Secret) MarshalText ¶ added in v0.10.0
MarshalText returns a redacted text representation. This prevents accidental text serialization (e.g., in YAML). Implements encoding.TextMarshaler.
type TelemetryHook ¶
type TelemetryHook interface {
// OnRequestStart is called when a request to a provider begins.
OnRequestStart(e RequestStartEvent)
// OnRequestEnd is called when a request to a provider completes.
OnRequestEnd(e RequestEndEvent)
}
TelemetryHook receives notifications about request lifecycle events. Implementations can use this for logging, metrics, tracing, etc.
Security Considerations ¶
Event types are designed to NEVER include sensitive data:
- API keys are NEVER included (stored separately as core.Secret)
- Prompt content (user messages) is NEVER included
- Response content (model outputs) is NEVER included
- Only operational metadata is exposed (provider, model, timing, token counts)
This design ensures that telemetry data can be safely:
- Logged to disk without risk of credential exposure
- Sent to external monitoring systems
- Aggregated for analytics
- Stored long-term for debugging
If extending this interface, maintain these security properties. New event types must undergo security review before merge. Never add fields that could contain: API keys, user prompts, model responses, or any other potentially sensitive content.
type TokenCountResponse ¶ added in v1.0.0
type TokenCountResponse struct {
InputTokens int `json:"input_tokens"`
}
TokenCountResponse contains the number of input tokens a provider would process for a chat request.
type TokenCounter ¶ added in v1.0.0
type TokenCounter interface {
CountTokens(ctx context.Context, req *ChatRequest) (*TokenCountResponse, error)
}
TokenCounter is an optional interface for providers with a native token counting endpoint. The request should be the same ChatRequest that will be sent for generation so system messages, multimodal parts, and tools can be included in the provider's count.
func AsTokenCounter ¶ added in v1.0.0
func AsTokenCounter(p Provider) (TokenCounter, bool)
AsTokenCounter discovers token counting on a Provider or its unwrap chain. It returns nil and false when no provider implements native token counting.
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
TokenUsage tracks token consumption for a request.
type Tool ¶
type Tool interface {
Name() string
Description() string
Schema() ToolSchema
}
Tool is the canonical contract for tool definitions passed to ChatBuilder.Tools. Name and Description are used by every provider; Schema supplies the JSON Schema describing the tool's parameters so the model can generate valid arguments. A tool that takes no parameters may return an empty ToolSchema.
The tools package extends this interface with Call for executable tools:
type Tool interface {
core.Tool
Call(ctx context.Context, args json.RawMessage) (any, error)
}
Any type implementing tools.Tool (or core.Tool plus Schema) can be passed to ChatBuilder.Tools.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
ToolCall represents a tool invocation requested by the model. Arguments MUST be valid JSON bytes and MUST preserve raw JSON (no reformatting).
type ToolResources ¶
type ToolResources struct {
FileSearch *FileSearchResources `json:"file_search,omitempty"`
}
ToolResources contains configuration for built-in tools.
type ToolResult ¶ added in v0.11.0
type ToolResult struct {
CallID string `json:"call_id"` // Must match ToolCall.ID from the response
Content any `json:"content"` // Result data (will be JSON marshaled)
IsError bool `json:"is_error"` // True if this represents an error
}
ToolResult represents the outcome of executing a tool. Use this for untyped tool results where the Content can be any JSON-serializable value.
type ToolResultBuilder ¶ added in v0.11.0
type ToolResultBuilder struct {
// contains filtered or unexported fields
}
ToolResultBuilder provides a fluent API for constructing tool results.
func NewToolResults ¶ added in v0.11.0
func NewToolResults() *ToolResultBuilder
NewToolResults creates a new builder for tool results.
func (*ToolResultBuilder) Build ¶ added in v0.11.0
func (b *ToolResultBuilder) Build() []ToolResult
Build returns the accumulated results.
func (*ToolResultBuilder) Error ¶ added in v0.11.0
func (b *ToolResultBuilder) Error(callID string, err error) *ToolResultBuilder
Error adds a failed tool result.
func (*ToolResultBuilder) FromExecution ¶ added in v0.11.0
func (b *ToolResultBuilder) FromExecution(callID string, result any, err error) *ToolResultBuilder
FromExecution adds a result from a tool execution, handling both success and error cases.
func (*ToolResultBuilder) Success ¶ added in v0.11.0
func (b *ToolResultBuilder) Success(callID string, content any) *ToolResultBuilder
Success adds a successful tool result.
type ToolSchema ¶ added in v1.0.0
type ToolSchema struct {
// JSONSchema is a valid JSON Schema object describing the tool's
// parameters. Example:
// {"type": "object", "properties": {"location": {"type": "string"}}}
JSONSchema json.RawMessage `json:"json_schema"`
}
ToolSchema describes a tool's parameters as a JSON Schema. tools.ToolSchema is an alias of this type, so implementations may return either interchangeably.
type TypedToolResult ¶ added in v0.11.0
type TypedToolResult[T any] struct { CallID string `json:"call_id"` Content T `json:"content"` IsError bool `json:"is_error"` }
TypedToolResult is a type-safe tool result with compile-time type checking. Use this when you want type safety for tool results.
func (TypedToolResult[T]) ToUntyped ¶ added in v0.11.0
func (r TypedToolResult[T]) ToUntyped() ToolResult
ToUntyped converts a typed result to the untyped ToolResult for use with ChatBuilder.
type TypedToolResultBuilder ¶ added in v0.11.0
type TypedToolResultBuilder[T any] struct { // contains filtered or unexported fields }
TypedToolResultBuilder provides a type-safe fluent API for constructing tool results.
func NewTypedToolResults ¶ added in v0.11.0
func NewTypedToolResults[T any]() *TypedToolResultBuilder[T]
NewTypedToolResults creates a new type-safe builder for tool results.
func (*TypedToolResultBuilder[T]) Build ¶ added in v0.11.0
func (b *TypedToolResultBuilder[T]) Build() []ToolResult
Build returns the accumulated results as untyped ToolResults for use with ChatBuilder.
func (*TypedToolResultBuilder[T]) Success ¶ added in v0.11.0
func (b *TypedToolResultBuilder[T]) Success(callID string, content T) *TypedToolResultBuilder[T]
Success adds a successful typed tool result.
type WarningHandler ¶ added in v0.12.0
type WarningHandler func(message string)
WarningHandler receives non-fatal warnings emitted by the SDK. Implementations should be safe for concurrent use.