core

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 10 Imported by: 0

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) }()

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:

Error Handling

The package defines sentinel errors for common failure modes:

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.ExponentialBackoff{
    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) 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)

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

This section is empty.

Variables

View Source
var (
	ErrUnauthorized = errors.New("unauthorized")
	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.

View Source
var (
	ErrBatchTimeout   = errors.New("batch processing timed out")
	ErrBatchNotFound  = errors.New("batch not found")
	ErrBatchCancelled = errors.New("batch was cancelled")
)

Batch processing errors.

View Source
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.

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 BatchID added in v0.13.0

type BatchID string

BatchID uniquely identifies a 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

func (b *BatchInfo) IsComplete() bool

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:

  1. CreateBatch to submit requests
  2. Poll GetBatchStatus until IsComplete() returns true
  3. 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 attempts to cast a Provider to BatchProvider. Returns the BatchProvider and true if the provider supports batch operations, or nil and false otherwise.

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.

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

func (w *BatchWaiter) Wait(ctx context.Context, id BatchID) (*BatchInfo, error)

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.

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.

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.

Example:

schema := &core.JSONSchemaDefinition{
    Name:   "person",
    Strict: true,
    Schema: json.RawMessage(`{"type":"object","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) 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) 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.

Note: The Timeout() setting is NOT applied to streaming requests because the context must outlive this method call. For streaming with timeouts, create the context externally:

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.

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"`
}

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"`

	// 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.

Behavior:

  1. Read all chunks from Ch, accumulating Delta into output string
  2. Check Err channel for any errors
  3. Wait for Final to get complete response with usage/tool calls
  4. If Final includes Output, use it; otherwise use accumulated deltas
  5. Handle context cancellation gracefully

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) 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) Provider

func (c *Client) Provider() Provider

Provider returns the underlying provider.

type ClientOption

type ClientOption func(*Client)

ClientOption configures a Client.

func WithRetryPolicy

func WithRetryPolicy(r RetryPolicy) ClientOption

WithRetryPolicy sets the retry policy for the client.

func WithTelemetry

func WithTelemetry(h TelemetryHook) ClientOption

WithTelemetry sets the telemetry hook for the client.

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 with the OpenAI Responses API.

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.

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(client *Client, model ModelID, opts ...ConversationOption) *Conversation

NewConversation creates a new conversation session with the given client and model.

func (*Conversation) Clear added in v0.11.0

func (c *Conversation) Clear()

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() []Message

GetHistory returns the full conversation history.

func (*Conversation) MessageCount added in v0.11.0

func (c *Conversation) MessageCount() int

MessageCount returns the number of messages in the conversation.

func (*Conversation) Send added in v0.11.0

func (c *Conversation) Send(userMessage string) (*ChatResponse, error)

Send sends a user message and returns the assistant's response. Automatically manages conversation history. Uses context.Background() internally.

func (*Conversation) SendWithContext added in v0.11.0

func (c *Conversation) SendWithContext(ctx context.Context, userMessage string) (*ChatResponse, error)

SendWithContext sends a user message with context and returns the assistant's response.

func (*Conversation) Stream added in v0.13.0

func (c *Conversation) Stream(userMessage string) (*ChatStream, error)

Stream sends a user message and returns a streaming response. Automatically manages conversation history; the assistant's response is added to history when the stream completes. Uses context.Background() internally.

func (*Conversation) StreamWithContext added in v0.13.0

func (c *Conversation) StreamWithContext(ctx context.Context, userMessage string) (*ChatStream, error)

StreamWithContext sends a user message with context and returns a streaming response. The stream is wrapped to automatically add the assistant's response to history when the stream completes successfully.

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.

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"
	FeatureEmbeddings               Feature = "embeddings"
	FeatureContextualizedEmbeddings Feature = "contextualized_embeddings"
	FeatureReranking                Feature = "reranking"
	FeatureStructuredOutput         Feature = "structured_output"
	FeatureBatch                    Feature = "batch"
)
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.

func (ImageData) GetBytes

func (d ImageData) GetBytes() ([]byte, error)

GetBytes decodes and returns the image data.

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.

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 ImageSize

type ImageSize string

ImageSize represents supported image dimensions.

const (
	ImageSize1024x1024 ImageSize = "1024x1024"
	ImageSize1536x1024 ImageSize = "1536x1024"
	ImageSize1024x1536 ImageSize = "1024x1536"
	ImageSizeAuto      ImageSize = "auto"
)

func (ImageSize) IsValid

func (s ImageSize) IsValid() bool

IsValid reports whether the image size is a recognized value.

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. 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(msg Message)

AddMessage appends a message to the conversation history.

func (*InMemoryStore) AddMessages added in v0.11.0

func (m *InMemoryStore) AddMessages(msgs []Message)

AddMessages appends multiple messages to the conversation history.

func (*InMemoryStore) Clear added in v0.11.0

func (m *InMemoryStore) Clear()

Clear removes all messages from the conversation.

func (*InMemoryStore) GetHistory added in v0.11.0

func (m *InMemoryStore) GetHistory() []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(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() int

Len returns the number of messages in the conversation.

func (*InMemoryStore) SetMessages added in v0.11.0

func (m *InMemoryStore) SetMessages(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

func (f InputFile) ContentType() string

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

func (t InputText) ContentType() string

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 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.
	Strict bool `json:"strict,omitempty"`
}

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(msg Message)

	// AddMessages appends multiple messages to the conversation history.
	AddMessages(msgs []Message)

	// GetHistory returns all messages in the conversation.
	GetHistory() []Message

	// GetLastN returns the last N messages in the conversation.
	GetLastN(n int) []Message

	// Clear removes all messages from the conversation.
	Clear()

	// Len returns the number of messages in the conversation.
	Len() int

	// SetMessages replaces the entire conversation history.
	SetMessages(msgs []Message)
}

Memory is the interface for managing conversation history. Implementations provide different storage backends (in-memory, Redis, PostgreSQL, etc.).

type Message

type Message struct {
	Role        Role          `json:"role"`
	Content     string        `json:"content,omitempty"`
	Parts       []ContentPart `json:"-"`                      // Multimodal content parts (Responses API only)
	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

func (m ModelInfo) HasCapability(f Feature) bool

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
	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 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

type ReasoningOutput struct {
	ID      string   `json:"id"`
	Summary []string `json:"summary,omitempty"`
}

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.

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.
	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.

func NewRetryPolicy

func NewRetryPolicy(cfg RetryConfig) RetryPolicy

NewRetryPolicy creates a retry policy with the given configuration.

type Role

type Role string

Role represents a message participant role.

const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool" // For tool result messages
)

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

func NewSecret(value string) Secret

NewSecret creates a new Secret from a string value.

func (Secret) Expose added in v0.10.0

func (s Secret) Expose() string

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

func (s Secret) GoString() string

GoString returns a redacted placeholder for %#v formatting. Implements fmt.GoStringer.

func (Secret) IsEmpty added in v0.10.0

func (s Secret) IsEmpty() bool

IsEmpty returns true if the secret value is empty.

func (Secret) MarshalJSON added in v0.10.0

func (s Secret) MarshalJSON() ([]byte, error)

MarshalJSON returns a redacted JSON string. This prevents accidental JSON serialization of the secret value.

func (Secret) MarshalText added in v0.10.0

func (s Secret) MarshalText() ([]byte, error)

MarshalText returns a redacted text representation. This prevents accidental text serialization (e.g., in YAML). Implements encoding.TextMarshaler.

func (Secret) String added in v0.10.0

func (s Secret) String() string

String returns a redacted placeholder. This prevents accidental logging of the secret value. Implements fmt.Stringer.

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 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
}

Tool is a placeholder interface for tool definitions. Full implementation is in the tools package (Task 07).

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 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.

Jump to

Keyboard shortcuts

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