core

package
v0.83.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 23 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ToolResultModelTextMeta     = "agent.model_text"
	ToolResultDisplayTextMeta   = "agent.display_text"
	ToolResultIsErrorMeta       = "agent.is_error"
	ToolResultSyntheticMeta     = "agent.synthetic"
	ToolResultRedactedMeta      = "agent.redacted"
	ToolResultTruncatedMeta     = "agent.truncated"
	ToolResultDetailsAnnotation = "agent.details"
)
View Source
const Version = "0.0.0-dev"

Version is a placeholder for the dspy-go version.

Variables

View Source
var ErrToolBlocked = errors.New("tool blocked")
View Source
var (

	// GlobalConfig holds package-level defaults for backward compatibility.
	// Prefer the package helper functions over direct field mutation.
	GlobalConfig = &Config{

		ConcurrencyLevel: 1,
	}
)
View Source
var ProviderModels = map[string][]ModelID{
	"anthropic": {
		ModelAnthropicSonnet, ModelAnthropicHaiku, ModelAnthropicOpus,
		ModelAnthropicClaude4Opus, ModelAnthropicClaude4Sonnet, ModelAnthropicClaude45Sonnet,
		ModelAnthropicClaude45Opus, ModelAnthropicClaude46Sonnet, ModelAnthropicClaude46Opus,
	},
	"google": {
		ModelGoogleGeminiFlash, ModelGoogleGeminiPro, ModelGoogleGeminiFlashLite,
		ModelGoogleGemini3ProPreview, ModelGoogleGemini3FlashPreview,
		ModelGoogleGemini20Flash, ModelGoogleGemini20FlashLite,
	},
	"openai": {
		ModelOpenAIGPT4, ModelOpenAIGPT4Turbo, ModelOpenAIGPT35Turbo, ModelOpenAIGPT4o, ModelOpenAIGPT4oMini,
		ModelOpenAIGPT41, ModelOpenAIGPT41Mini, ModelOpenAIGPT41Nano,
		ModelOpenAIO1, ModelOpenAIO1Pro, ModelOpenAIO1Mini,
		ModelOpenAIO3, ModelOpenAIO3Mini,
		ModelOpenAIGPT5, ModelOpenAIGPT5Mini, ModelOpenAIGPT5Nano,
		ModelOpenAIGPT52, ModelOpenAIGPT52Instant, ModelOpenAIGPT52Thinking,
		ModelOpenAIGPT52ThinkHigh, ModelOpenAIGPT52Pro, ModelOpenAIGPT52Codex,
	},
	"ollama": {
		ModelOllamaLlama3_8B, ModelOllamaLlama3_70B, ModelOllamaLlama3_1_8B, ModelOllamaLlama3_1_70B,
		ModelOllamaLlama3_2_3B, ModelOllamaCodeLlama13B, ModelOllamaCodeLlama34B, ModelOllamaMistral7B,
		ModelOllamaGemma2B, ModelOllamaGemma7B, ModelOllamaQwen2_5_7B, ModelOllamaQwen2_5_14B,
		ModelOllamaNomicEmbed, ModelOllamaMxbaiEmbed, ModelOllamaAllMiniLM,
	},
	"llamacpp":          {},
	"litellm":           {ModelLiteLLMGPT4, ModelLiteLLMClaude3, ModelLiteLLMLlama2, ModelLiteLLMGemini},
	"localai":           {ModelLocalAILlama2, ModelLocalAICodeLlama, ModelLocalAIAlpaca, ModelLocalAIVicuna},
	"fastchat":          {ModelFastChatVicuna, ModelFastChatAlpaca, ModelFastChatCodeLlama, ModelFastChatLlama2},
	"openai_compatible": {ModelCompatibleGPT4, ModelCompatibleGPT35, ModelCompatibleClaude, ModelCompatibleLlama2},
}

Functions

func ConfigureDefaultLLM added in v0.16.1

func ConfigureDefaultLLM(apiKey string, modelID ModelID) error

ConfigureDefaultLLM sets up the default LLM to be used across the package.

func ConfigureDefaultLLMFromRegistry added in v0.37.0

func ConfigureDefaultLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) error

ConfigureDefaultLLMFromRegistry sets up the default LLM using the registry.

func ConfigureFromRegistryConfig added in v0.37.0

func ConfigureFromRegistryConfig(ctx context.Context, config RegistryConfig) error

ConfigureFromRegistryConfig initializes the global configuration with registry settings.

func ConfigureTeacherLLM added in v0.16.1

func ConfigureTeacherLLM(apiKey string, modelID ModelID) error

ConfigureTeacherLLM sets up the teacher LLM.

func ConfigureTeacherLLMFromRegistry added in v0.37.0

func ConfigureTeacherLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) error

ConfigureTeacherLLMFromRegistry sets up the teacher LLM using the registry.

func EndSpan added in v0.9.0

func EndSpan(ctx context.Context)

EndSpan completes the current span.

func GetConcurrencyLevel added in v0.83.0

func GetConcurrencyLevel() int

GetConcurrencyLevel returns the configured package concurrency level.

func GetSupportedModels added in v0.37.0

func GetSupportedModels() map[string][]string

GetSupportedModels returns all models supported by all providers in the registry.

func InferProviderFromModelID added in v0.83.1

func InferProviderFromModelID(modelID ModelID) string

InferProviderFromModelID infers the provider name from a model identifier for backward-compatible model resolution.

func InitializeDefaultFactory added in v0.37.0

func InitializeDefaultFactory()

InitializeDefaultFactory initializes the default factory with the registry system.

func InitializeRegistry added in v0.37.0

func InitializeRegistry(ctx context.Context, config RegistryConfig) error

InitializeRegistry initializes the global registry with the given configuration.

func IsModelSupportedInRegistry added in v0.37.0

func IsModelSupportedInRegistry(modelID ModelID) bool

IsModelSupportedInRegistry checks if a model is supported by the registry.

func IsMultimodalContent added in v0.38.0

func IsMultimodalContent(signature Signature, inputs map[string]any) bool

IsMultimodalContent checks if the inputs contain any non-text content.

func JoinFieldNames added in v0.75.2

func JoinFieldNames(fields []Field) string

JoinFieldNames joins field names with commas.

func LoadProgram added in v0.27.0

func LoadProgram(p *Program, filepath string) error

LoadProgram loads program state (demos and tuned parameters) from a JSON file into an existing Program instance. It assumes the Program `p` has already been constructed with the correct architecture (modules and signatures) and necessary LLMs configured.

func LoadProgramWithOptions added in v0.83.0

func LoadProgramWithOptions(p *Program, filepath string, opts LoadProgramOptions) error

LoadProgramWithOptions loads program state and reports any non-fatal warnings through the provided options instead of writing to the process logger.

func ProcessTyped added in v0.52.0

func ProcessTyped[TInput, TOutput any](ctx context.Context, module Module, inputs TInput, opts ...Option) (TOutput, error)

ProcessTyped provides type-safe module processing using generics.

func ProcessTypedWithValidation added in v0.52.0

func ProcessTypedWithValidation[TInput, TOutput any](ctx context.Context, module Module, inputs TInput, opts ...Option) (TOutput, error)

ProcessTypedWithValidation provides type-safe processing with signature validation.

func RecordLLMCall added in v0.83.0

func RecordLLMCall(ctx context.Context, llm LLM)

RecordLLMCall stores the current LLM model identifier in execution state so logs and traces can attribute downstream events to the active model.

func RecordModelCall added in v0.83.0

func RecordModelCall(ctx context.Context, model ModelIdentifier)

RecordModelCall stores the current model identifier in execution state so logs and traces can attribute downstream events to the active model.

func RefreshRegistryProvider added in v0.37.0

func RefreshRegistryProvider(ctx context.Context, providerName string, config ProviderConfig) error

RefreshRegistryProvider reloads a provider configuration.

func RegisterProviderFactory added in v0.37.0

func RegisterProviderFactory(name string, factory ProviderFactory) error

RegisterProviderFactory is a convenience function to register a provider factory.

func SaveProgram added in v0.27.0

func SaveProgram(p *Program, filepath string) error

SaveProgram serializes the current state of the Program's modules to a JSON file.

func SetConcurrencyOptions added in v0.16.1

func SetConcurrencyOptions(level int)

func SetDefaultFactory added in v0.83.0

func SetDefaultFactory(factory LLMFactory)

SetDefaultFactory replaces the package default LLM factory.

func SetDefaultLLM

func SetDefaultLLM(llm LLM)

SetDefaultLLM sets the default LLM.

func SetRegistry added in v0.83.0

func SetRegistry(registry LLMRegistry)

SetRegistry replaces the package-level registry.

func SetRegistryConstructor added in v0.83.1

func SetRegistryConstructor(constructor RegistryConstructor)

SetRegistryConstructor installs or replaces the constructor used by NewLLMRegistry and the GetRegistry lazy-init path.

func SetTeacherLLM added in v0.83.0

func SetTeacherLLM(llm LLM)

SetTeacherLLM sets the teacher LLM.

func ShallowCopyMap deprecated added in v0.80.0

func ShallowCopyMap[V any](input map[string]V) map[string]V

ShallowCopyMap returns a shallow copy of the provided map.

Deprecated: use maps.Clone.

func ToolResultMetadataString added in v0.82.0

func ToolResultMetadataString(metadata map[string]any, key string) string

ToolResultMetadataString returns a string metadata value when present.

func ValidateEndpointConfig added in v0.14.0

func ValidateEndpointConfig(cfg *EndpointConfig) error

func WithExecutionState added in v0.9.0

func WithExecutionState(ctx context.Context) context.Context

WithExecutionState creates a new context with dspy-go execution state.

func WithFreshExecutionState added in v0.65.0

func WithFreshExecutionState(ctx context.Context) context.Context

WithFreshExecutionState creates a new context with a fresh ExecutionState, even if the parent context already has one. This is useful for parallel workers that need isolated execution state to avoid mutex contention while still inheriting other context values from the parent while preserving the trace ID.

func WithMaxTrials

func WithMaxTrials(n int) func(*CompileOptions)

WithMaxTrials sets the maximum number of trials for optimization.

func WithRuntime added in v0.83.1

func WithRuntime(ctx context.Context, runtime *Runtime) context.Context

WithRuntime attaches a runtime to the context for downstream resolution.

func WithTeacher

func WithTeacher(teacher *Program) func(*CompileOptions)

WithTeacher sets a teacher program for optimization.

Types

type AgentHandler added in v0.45.0

type AgentHandler func(ctx context.Context, input map[string]any) (map[string]any, error)

AgentHandler represents the actual agent execute function.

type AgentInfo added in v0.45.0

type AgentInfo struct {
	// AgentID is the unique identifier for the agent instance
	AgentID string

	// AgentType is the category/type of the agent (e.g., "ReactiveAgent", "PlannerAgent")
	AgentType string

	// Capabilities lists the tools/capabilities available to this agent
	Capabilities []Tool

	// Version is the agent version for compatibility tracking
	Version string

	// Metadata contains additional agent-specific information
	Metadata map[string]any
}

AgentInfo contains metadata about an agent for interceptor use.

func NewAgentInfo added in v0.45.0

func NewAgentInfo(agentID, agentType string, capabilities []Tool) *AgentInfo

NewAgentInfo creates an AgentInfo with the provided details. This is a convenience function to ensure consistent AgentInfo creation.

func (*AgentInfo) WithMetadata added in v0.45.0

func (ai *AgentInfo) WithMetadata(key string, value any) *AgentInfo

WithMetadata adds metadata to AgentInfo.

func (*AgentInfo) WithVersion added in v0.45.0

func (ai *AgentInfo) WithVersion(version string) *AgentInfo

WithVersion sets the version for AgentInfo.

type AgentInterceptor added in v0.45.0

type AgentInterceptor func(ctx context.Context, input map[string]any, info *AgentInfo, handler AgentHandler) (map[string]any, error)

AgentInterceptor wraps Agent.Execute() calls with additional functionality. Similar to ModuleInterceptor but for agent operations.

func ChainAgentInterceptors added in v0.45.0

func ChainAgentInterceptors(interceptors ...AgentInterceptor) AgentInterceptor

ChainAgentInterceptors combines multiple agent interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type BaseDecorator deprecated added in v0.14.0

type BaseDecorator struct {
	LLM
}

Deprecated: prefer explicit RecordLLMCall calls at LLM boundaries instead of wrapping LLM implementations with decorators. BaseDecorator provides common functionality for compatibility with older code.

func (*BaseDecorator) GenerateWithTools added in v0.80.0

func (d *BaseDecorator) GenerateWithTools(ctx context.Context, messages []ChatMessage, tools []map[string]any, opts ...GenerateOption) (map[string]any, error)

func (*BaseDecorator) Unwrap added in v0.14.0

func (d *BaseDecorator) Unwrap() LLM

type BaseLLM

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

BaseLLM provides a base implementation of the LLM interface.

func NewBaseLLM added in v0.14.0

func NewBaseLLM(providerName string, modelID ModelID, capabilities []Capability, endpoint *EndpointConfig, opts ...BaseLLMOption) *BaseLLM

func (*BaseLLM) Capabilities added in v0.14.0

func (b *BaseLLM) Capabilities() []Capability

Capabilities implements LLM interface.

func (*BaseLLM) GenerateWithContent added in v0.38.0

func (b *BaseLLM) GenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*LLMResponse, error)

Concrete LLM implementations should override this if they support multimodal content.

func (*BaseLLM) GetEndpointConfig added in v0.14.0

func (b *BaseLLM) GetEndpointConfig() *EndpointConfig

GetEndpointConfig returns the current endpoint configuration.

func (*BaseLLM) GetHTTPClient added in v0.14.0

func (b *BaseLLM) GetHTTPClient() *http.Client

GetHTTPClient returns the HTTP client.

func (*BaseLLM) IsLocal added in v0.65.0

func (b *BaseLLM) IsLocal() bool

IsLocal returns true if this LLM runs locally (CPU-bound inference).

func (*BaseLLM) ModelID added in v0.14.0

func (b *BaseLLM) ModelID() string

ModelID implements LLM interface.

func (*BaseLLM) ProviderName added in v0.14.0

func (b *BaseLLM) ProviderName() string

ProviderName implements LLM interface.

func (*BaseLLM) StreamGenerateWithContent added in v0.38.0

func (b *BaseLLM) StreamGenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*StreamResponse, error)

Concrete LLM implementations should override this if they support multimodal streaming.

func (*BaseLLM) WorkloadType added in v0.65.0

func (b *BaseLLM) WorkloadType() WorkloadType

WorkloadType returns the workload classification based on provider. Remote APIs are I/O-bound (network latency), local models are CPU-bound.

type BaseLLMOption added in v0.65.0

type BaseLLMOption func(*BaseLLM)

BaseLLMOption configures BaseLLM behavior.

func WithTransportConfig added in v0.65.0

func WithTransportConfig(config TransportConfig) BaseLLMOption

WithTransportConfig sets custom HTTP transport configuration. Use this for high-throughput scenarios that need more concurrent connections.

type BaseModule

type BaseModule struct {
	Signature   Signature
	LLM         LLM
	DisplayName string
	ModuleType  string
	// contains filtered or unexported fields
}

BaseModule provides a basic implementation of the Module interface.

func NewModule

func NewModule(signature Signature) *BaseModule

NewModule creates a new base module with the given signature.

func (*BaseModule) ClearInterceptors added in v0.47.0

func (bm *BaseModule) ClearInterceptors()

ClearInterceptors removes all interceptors from this module.

func (*BaseModule) Clone

func (bm *BaseModule) Clone() Module

Clone creates a deep copy of the BaseModule.

func (*BaseModule) FormatOutputs

func (bm *BaseModule) FormatOutputs(outputs map[string]any) map[string]any

FormatOutputs ensures that the output map contains all fields specified in the output signature.

func (*BaseModule) GetDisplayName added in v0.36.0

func (bm *BaseModule) GetDisplayName() string

GetDisplayName returns the human-readable name for this module instance.

func (*BaseModule) GetInterceptors added in v0.47.0

func (bm *BaseModule) GetInterceptors() []ModuleInterceptor

GetInterceptors returns the current interceptors for this module.

func (*BaseModule) GetModuleType added in v0.36.0

func (bm *BaseModule) GetModuleType() string

GetModuleType returns the category/type of this module.

func (*BaseModule) GetSignature

func (bm *BaseModule) GetSignature() Signature

GetSignature returns the module's signature.

func (*BaseModule) Process

func (bm *BaseModule) Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)

Process is a placeholder implementation and should be overridden by specific modules.

func (*BaseModule) ProcessWithInterceptors added in v0.47.0

func (bm *BaseModule) ProcessWithInterceptors(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, opts ...Option) (map[string]any, error)

ProcessWithInterceptors executes the module's logic with interceptor support. If no interceptors are provided, it falls back to using the module's default interceptors. Note: This method should be called on the concrete module type, not BaseModule directly.

func (*BaseModule) ProcessWithInterceptorsImpl added in v0.49.0

func (bm *BaseModule) ProcessWithInterceptorsImpl(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, processFunc func(context.Context, map[string]any, ...Option) (map[string]any, error), opts ...Option) (map[string]any, error)

ProcessWithInterceptorsImpl is a helper method that implements the interceptor logic. It accepts a process function as a parameter to allow concrete types to pass their own Process method.

func (*BaseModule) SetInterceptors added in v0.47.0

func (bm *BaseModule) SetInterceptors(interceptors []ModuleInterceptor)

SetInterceptors sets the default interceptors for this module instance.

func (*BaseModule) SetLLM

func (bm *BaseModule) SetLLM(llm LLM)

SetLLM sets the language model for the module.

func (*BaseModule) SetSignature added in v0.1.0

func (bm *BaseModule) SetSignature(signature Signature)

func (*BaseModule) ValidateInputs

func (bm *BaseModule) ValidateInputs(inputs map[string]any) error

ValidateInputs checks if the provided inputs match the module's input signature.

type BaseOptimizer

type BaseOptimizer struct {
	Name string
}

BaseOptimizer provides a basic implementation of the Optimizer interface.

func (*BaseOptimizer) Compile

func (bo *BaseOptimizer) Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)

Compile is a placeholder implementation and should be overridden by specific optimizer implementations.

type BatchEmbedder added in v0.83.0

type BatchEmbedder interface {
	CreateEmbeddings(ctx context.Context, inputs []string, options ...EmbeddingOption) (*BatchEmbeddingResult, error)
}

BatchEmbedder produces embeddings for multiple inputs.

type BatchEmbeddingResult added in v0.16.1

type BatchEmbeddingResult struct {
	// Embeddings for each input
	Embeddings []EmbeddingResult
	// Any error that occurred during processing
	Error error
	// Input index that caused the error (if applicable)
	ErrorIndex int
}

BatchEmbeddingResult represents results for multiple inputs.

type BootstrapFewShot

type BootstrapFewShot struct {
	BaseOptimizer
	MaxExamples int
}

BootstrapFewShot implements a basic few-shot learning optimizer.

func NewBootstrapFewShot

func NewBootstrapFewShot(maxExamples int) *BootstrapFewShot

NewBootstrapFewShot creates a new BootstrapFewShot optimizer.

func (*BootstrapFewShot) Compile

func (bfs *BootstrapFewShot) Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)

Compile implements the optimization logic for BootstrapFewShot.

type Capability added in v0.14.0

type Capability string
const (
	// Core capabilities.
	CapabilityCompletion Capability = "completion"
	CapabilityChat       Capability = "chat"
	CapabilityEmbedding  Capability = "embedding"

	// Advanced capabilities.
	CapabilityJSON        Capability = "json"
	CapabilityStreaming   Capability = "streaming"
	CapabilityToolCalling Capability = "tool-calling"
	// New multimodal capabilities.
	CapabilityMultimodal Capability = "multimodal"
	CapabilityVision     Capability = "vision"
	CapabilityAudio      Capability = "audio"
)

type CapabilityProvider added in v0.83.0

type CapabilityProvider interface {
	Capabilities() []Capability
}

CapabilityProvider reports supported model capabilities.

type ChatMessage added in v0.76.0

type ChatMessage struct {
	Role       string          `json:"role"`                  // "system"|"user"|"assistant"|"tool"
	Content    []ContentBlock  `json:"content"`               // reuse existing multimodal blocks from llm.go
	ToolCalls  []ToolCall      `json:"tool_calls,omitempty"`  // for assistant messages requesting tool calls
	ToolResult *ChatToolResult `json:"tool_result,omitempty"` // for tool role messages
}

ChatMessage represents a single message in a multi-turn conversation. Used by ToolCallingChatLLM for true native tool-use loops.

type ChatToolResult added in v0.76.0

type ChatToolResult struct {
	ToolCallID string         `json:"tool_call_id"`
	Name       string         `json:"name"`
	Content    []ContentBlock `json:"content"`
	IsError    bool           `json:"is_error,omitempty"`
}

ChatToolResult carries the result of a single tool execution in a chat message.

type CloneableTool added in v0.80.0

type CloneableTool interface {
	CloneTool() Tool
}

CloneableTool is an optional interface for tools that can provide an isolated copy for concurrent agent or optimizer execution.

type CompileOptions

type CompileOptions struct {
	MaxTrials int
	Teacher   *Program
}

CompileOptions represents options for the compilation process.

type Composable

type Composable interface {
	Module
	Compose(next Module) Module
	GetSubModules() []Module
	SetSubModules([]Module)
}

Composable is an interface for modules that can be composed with other modules.

type Config added in v0.16.1

type Config struct {
	DefaultLLM       LLM
	TeacherLLM       LLM
	ConcurrencyLevel int

	// Registry configuration
	Registry *RegistryConfig `json:"registry,omitempty" yaml:"registry,omitempty"`
}

type ContentBlock added in v0.38.0

type ContentBlock struct {
	Type     FieldType `json:"type"`
	Text     string    `json:"text,omitempty"`
	Data     []byte    `json:"-"` // Raw binary data for images/audio
	MimeType string    `json:"mime_type,omitempty"`
	// Optional metadata for extensibility
	Metadata map[string]any `json:"metadata,omitempty"`
}

It's provider-agnostic - each LLM provider handles its own format conversion.

func ConvertInputsToContentBlocks added in v0.38.0

func ConvertInputsToContentBlocks(signature Signature, inputs map[string]any) []ContentBlock

This enables backward compatibility while supporting multimodal content. It creates a structured prompt that includes field instructions and prefix information.

func NewAudioBlock added in v0.38.0

func NewAudioBlock(data []byte, mimeType string) ContentBlock

NewAudioBlock creates an audio content block.

func NewAudioFromBase64 added in v0.38.0

func NewAudioFromBase64(data string, mimeType string) (*ContentBlock, error)

NewAudioFromBase64 creates an audio ContentBlock from base64 encoded data. It decodes the base64 data and creates a properly formatted ContentBlock.

func NewAudioFromFile added in v0.38.0

func NewAudioFromFile(path string) (*ContentBlock, error)

NewAudioFromFile creates an audio ContentBlock from a file path. It reads the file, detects the MIME type, and creates a properly formatted ContentBlock.

func NewAudioFromURL added in v0.38.0

func NewAudioFromURL(url string) (*ContentBlock, error)

NewAudioFromURL creates an audio ContentBlock from a URL. It downloads the audio, detects the MIME type, and creates a properly formatted ContentBlock.

func NewImageBlock added in v0.38.0

func NewImageBlock(data []byte, mimeType string) ContentBlock

NewImageBlock creates an image content block.

func NewImageFromBase64 added in v0.38.0

func NewImageFromBase64(data string, mimeType string) (*ContentBlock, error)

NewImageFromBase64 creates an image ContentBlock from base64 encoded data. It decodes the base64 data and creates a properly formatted ContentBlock.

func NewImageFromFile added in v0.38.0

func NewImageFromFile(path string) (*ContentBlock, error)

NewImageFromFile creates an image ContentBlock from a file path. It reads the file, detects the MIME type, and creates a properly formatted ContentBlock.

func NewImageFromURL added in v0.38.0

func NewImageFromURL(url string) (*ContentBlock, error)

NewImageFromURL creates an image ContentBlock from a URL. It downloads the image, detects the MIME type, and creates a properly formatted ContentBlock.

func NewTextBlock added in v0.38.0

func NewTextBlock(text string) ContentBlock

NewTextBlock creates a text content block.

func (ContentBlock) String added in v0.38.0

func (cb ContentBlock) String() string

String returns a string representation of the content block.

type ContentGenerator added in v0.83.0

type ContentGenerator interface {
	GenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*LLMResponse, error)
}

ContentGenerator produces completions from multimodal content blocks.

type Dataset

type Dataset interface {
	// Next returns the next example in the dataset
	Next() (Example, bool)
	// Reset resets the dataset iterator
	Reset()
}

Dataset represents a collection of examples for training/evaluation.

type DemoConsumer added in v0.27.0

type DemoConsumer interface {
	SetDemos([]Example)
}

DemoConsumer is an interface that modules can optionally implement to allow their demos to be loaded.

type DemoProvider added in v0.27.0

type DemoProvider interface {
	GetDemos() []Example
}

DemoProvider is an interface that modules can optionally implement to allow their demos to be retrieved.

type Embedder added in v0.83.0

type Embedder interface {
	CreateEmbedding(ctx context.Context, input string, options ...EmbeddingOption) (*EmbeddingResult, error)
}

Embedder produces a single embedding.

type EmbeddingOption added in v0.16.1

type EmbeddingOption func(*EmbeddingOptions)

EmbeddingOption allows for optional parameters.

func WithBatchSize added in v0.16.1

func WithBatchSize(size int) EmbeddingOption

func WithModel added in v0.16.1

func WithModel(model string) EmbeddingOption

func WithParams added in v0.16.1

func WithParams(params map[string]any) EmbeddingOption

type EmbeddingOptions added in v0.16.1

type EmbeddingOptions struct {
	// Model-specific options for embedding
	Model string
	// Optional batch size for bulk embeddings
	BatchSize int
	// Additional model-specific parameters
	Params map[string]any
}

func NewEmbeddingOptions added in v0.16.1

func NewEmbeddingOptions() *EmbeddingOptions

Default options for embeddings.

type EmbeddingResult added in v0.16.1

type EmbeddingResult struct {
	// The generated embedding vector
	Vector []float32
	// Token count and other metadata
	TokenCount int
	// Any model-specific metadata
	Metadata map[string]any
}

EmbeddingResult represents the result of embedding generation.

type EndpointConfig added in v0.14.0

type EndpointConfig struct {
	BaseURL    string            // Base API URL
	Path       string            // Specific endpoint path
	Headers    map[string]string // Common headers
	TimeoutSec int               // Request timeout in seconds
}

type Example

type Example struct {
	Inputs  map[string]any
	Outputs map[string]any
}

Example represents a single training/evaluation example.

func DatasetToSlice added in v0.75.2

func DatasetToSlice(dataset Dataset) []Example

DatasetToSlice converts a Dataset to a slice of Examples. This is a helper function to avoid repeated iteration patterns.

type ExecutionContextKey added in v0.9.0

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

ExecutionContextKey is the type for context keys specific to dspy-go.

type ExecutionState added in v0.9.0

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

ExecutionState holds the mutable state for an execution context.

func GetExecutionState added in v0.9.0

func GetExecutionState(ctx context.Context) *ExecutionState

GetExecutionState retrieves the execution state from a context.

func (*ExecutionState) GetCurrentSpan added in v0.9.0

func (s *ExecutionState) GetCurrentSpan() *Span

func (*ExecutionState) GetModelID added in v0.9.0

func (s *ExecutionState) GetModelID() string

State access methods.

func (*ExecutionState) GetTokenUsage added in v0.9.0

func (s *ExecutionState) GetTokenUsage() *TokenUsage

func (*ExecutionState) GetTraceID added in v0.9.0

func (s *ExecutionState) GetTraceID() string

func (*ExecutionState) WithModelID added in v0.9.0

func (s *ExecutionState) WithModelID(modelID string)

State modification methods.

func (*ExecutionState) WithTokenUsage added in v0.9.0

func (s *ExecutionState) WithTokenUsage(usage *TokenUsage)

type Field

type Field struct {
	Name        string
	Description string
	Prefix      string
	Type        FieldType // Default is FieldTypeText for backward compatibility
}

Field represents a single field in a signature.

func InputFieldsToFields added in v0.75.2

func InputFieldsToFields(inputs []InputField) []Field

InputFieldsToFields converts InputField slice to Field slice.

func NewAudioField added in v0.38.0

func NewAudioField(name string, opts ...FieldOption) Field

NewAudioField creates a new audio field.

func NewField added in v0.4.0

func NewField(name string, opts ...FieldOption) Field

NewField creates a new Field with smart defaults.

func NewImageField added in v0.38.0

func NewImageField(name string, opts ...FieldOption) Field

NewImageField creates a new image field.

func NewTextField added in v0.38.0

func NewTextField(name string, opts ...FieldOption) Field

NewTextField creates a new text field (explicit version of NewField).

func OutputFieldsToFields added in v0.75.2

func OutputFieldsToFields(outputs []OutputField) []Field

OutputFieldsToFields converts OutputField slice to Field slice.

type FieldMetadata added in v0.52.0

type FieldMetadata struct {
	Name        string       // Field name from struct tag or field name
	GoFieldName string       // Original Go struct field name for direct lookup
	Required    bool         // Whether field is required
	Description string       // Field description
	Prefix      string       // Output prefix for LLM generation
	Type        FieldType    // Field type (text, image, audio)
	GoType      reflect.Type // The actual Go type
}

FieldMetadata represents parsed struct tag information.

type FieldOption added in v0.4.0

type FieldOption func(*Field)

FieldOption allows customization of Field creation.

func WithCustomPrefix added in v0.4.0

func WithCustomPrefix(prefix string) FieldOption

WithCustomPrefix overrides the default prefix.

func WithDescription added in v0.4.0

func WithDescription(desc string) FieldOption

WithDescription sets a custom description.

func WithFieldType added in v0.38.0

func WithFieldType(fieldType FieldType) FieldOption

WithFieldType sets the field type.

func WithNoPrefix added in v0.4.0

func WithNoPrefix() FieldOption

WithNoPrefix removes the prefix entirely.

type FieldType added in v0.38.0

type FieldType string

FieldType represents the type of data a field can contain.

const (
	FieldTypeText  FieldType = "text"
	FieldTypeImage FieldType = "image"
	FieldTypeAudio FieldType = "audio"
)

type ForwardFactory added in v0.80.0

type ForwardFactory func(modules map[string]Module) func(context.Context, map[string]any) (map[string]any, error)

ForwardFactory rebuilds a program forward function against a specific module map. Programs created with a factory can safely rebind their forward logic during Clone().

type FunctionCaller added in v0.83.0

type FunctionCaller interface {
	GenerateWithFunctions(ctx context.Context, prompt string, functions []map[string]any, options ...GenerateOption) (map[string]any, error)
}

FunctionCaller produces structured function-call responses from a string prompt.

type GenerateOption

type GenerateOption func(*GenerateOptions)

GenerateOption represents an option for text generation.

func WithFrequencyPenalty

func WithFrequencyPenalty(p float64) GenerateOption

WithFrequencyPenalty sets the frequency penalty.

func WithMaxTokens

func WithMaxTokens(n int) GenerateOption

WithMaxTokens sets the maximum number of tokens to generate.

func WithPresencePenalty

func WithPresencePenalty(p float64) GenerateOption

WithPresencePenalty sets the presence penalty.

func WithStopSequences

func WithStopSequences(sequences ...string) GenerateOption

WithStopSequences sets the stop sequences.

func WithTemperature

func WithTemperature(t float64) GenerateOption

WithTemperature sets the sampling temperature.

func WithTopP

func WithTopP(p float64) GenerateOption

WithTopP sets the nucleus sampling probability.

type GenerateOptions

type GenerateOptions struct {
	MaxTokens        int
	Temperature      float64
	TopP             float64
	PresencePenalty  float64
	FrequencyPenalty float64
	Stop             []string
}

GenerateOptions holds configuration for text generation.

func NewGenerateOptions

func NewGenerateOptions() *GenerateOptions

NewGenerateOptions creates a new GenerateOptions with default values.

type InputField

type InputField struct {
	Field
}

InputField represents an input field.

type InterceptableModule added in v0.47.0

type InterceptableModule interface {
	Module

	// ProcessWithInterceptors executes the module's logic with interceptor support
	ProcessWithInterceptors(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, opts ...Option) (map[string]any, error)

	// SetInterceptors sets the default interceptors for this module instance
	SetInterceptors(interceptors []ModuleInterceptor)

	// GetInterceptors returns the current interceptors for this module
	GetInterceptors() []ModuleInterceptor

	// ClearInterceptors removes all interceptors from this module
	ClearInterceptors()
}

InterceptableModule extends Module with interceptor support. This interface provides backward-compatible enhancement for modules that support interceptors.

type InterceptorChain added in v0.45.0

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

InterceptorChain manages a collection of interceptors for a specific component type.

func NewInterceptorChain added in v0.45.0

func NewInterceptorChain() *InterceptorChain

NewInterceptorChain creates a new empty interceptor chain.

func (*InterceptorChain) AddAgentInterceptor added in v0.45.0

func (ic *InterceptorChain) AddAgentInterceptor(interceptor AgentInterceptor) *InterceptorChain

AddAgentInterceptor adds an agent interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) AddModuleInterceptor added in v0.45.0

func (ic *InterceptorChain) AddModuleInterceptor(interceptor ModuleInterceptor) *InterceptorChain

AddModuleInterceptor adds a module interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) AddToolInterceptor added in v0.45.0

func (ic *InterceptorChain) AddToolInterceptor(interceptor ToolInterceptor) *InterceptorChain

AddToolInterceptor adds a tool interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) Clear added in v0.45.0

func (ic *InterceptorChain) Clear() *InterceptorChain

Clear removes all interceptors from the chain.

func (*InterceptorChain) Compose added in v0.45.0

func (ic *InterceptorChain) Compose(other *InterceptorChain) *InterceptorChain

Compose combines multiple interceptor chains into a single chain. This is useful for merging different sources of interceptors (e.g., global + module-specific).

func (*InterceptorChain) Count added in v0.45.0

func (ic *InterceptorChain) Count() int

Count returns the total number of interceptors in the chain.

func (*InterceptorChain) CountByType added in v0.45.0

func (ic *InterceptorChain) CountByType() (modules, agents, tools int)

CountByType returns the count of interceptors by type.

func (*InterceptorChain) GetAgentInterceptors added in v0.45.0

func (ic *InterceptorChain) GetAgentInterceptors() []AgentInterceptor

GetAgentInterceptors returns a copy of the agent interceptor slice.

func (*InterceptorChain) GetModuleInterceptors added in v0.45.0

func (ic *InterceptorChain) GetModuleInterceptors() []ModuleInterceptor

GetModuleInterceptors returns a copy of the module interceptor slice.

func (*InterceptorChain) GetToolInterceptors added in v0.45.0

func (ic *InterceptorChain) GetToolInterceptors() []ToolInterceptor

GetToolInterceptors returns a copy of the tool interceptor slice.

func (*InterceptorChain) IsEmpty added in v0.45.0

func (ic *InterceptorChain) IsEmpty() bool

IsEmpty returns true if the chain contains no interceptors.

type JSONGenerator added in v0.83.0

type JSONGenerator interface {
	GenerateWithJSON(ctx context.Context, prompt string, options ...GenerateOption) (map[string]any, error)
}

JSONGenerator produces structured JSON output from a string prompt.

type LLM

LLM is the full compatibility interface for model implementations. New code should prefer the smallest capability interface it needs.

func Chain deprecated added in v0.14.0

func Chain(base LLM, decorators ...func(LLM) LLM) LLM

Deprecated: prefer explicit RecordLLMCall calls at LLM boundaries instead of composing LLM decorators. Helper function to compose multiple decorators.

func CreateLLMFromRegistry added in v0.37.0

func CreateLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) (LLM, error)

CreateLLMFromRegistry creates an LLM instance using the global registry.

func CreateLLMWithTimeout added in v0.37.0

func CreateLLMWithTimeout(apiKey string, modelID ModelID, timeout time.Duration) (LLM, error)

CreateLLMWithTimeout creates an LLM with a specified timeout.

func GetDefaultLLM added in v0.16.1

func GetDefaultLLM() LLM

GetDefaultLLM returns the default LLM.

func GetTeacherLLM added in v0.16.1

func GetTeacherLLM() LLM

GetTeacherLLM returns the teacher LLM.

func LoadLLMFromConfig added in v0.37.0

func LoadLLMFromConfig(ctx context.Context, providerConfig ProviderConfig, modelID ModelID) (LLM, error)

LoadLLMFromConfig creates an LLM instance from a complete provider configuration.

func ResolveDefaultLLM added in v0.83.1

func ResolveDefaultLLM(ctx context.Context, info *ModuleInfo) LLM

ResolveDefaultLLM resolves the model to use for module execution in the following order: 1. module-local LLM from ModuleInfo 2. explicit runtime on the context 3. package-level default LLM for backward compatibility.

func ResolveTeacherLLM added in v0.83.1

func ResolveTeacherLLM(ctx context.Context) LLM

ResolveTeacherLLM resolves the teacher model from an explicit runtime first, then falls back to the package-level compatibility default.

type LLMFactory

type LLMFactory interface {
	// CreateLLM creates a new LLM instance. It uses the global configuration
	// from core.GlobalConfig for client settings.
	CreateLLM(apiKey string, modelID ModelID) (LLM, error)
}

LLMFactory defines a simple interface for creating LLM instances. This maintains compatibility with existing code while allowing for configuration.

var DefaultFactory LLMFactory

DefaultFactory is the global factory instance used by the configuration system. Prefer GetDefaultFactory and SetDefaultFactory over direct mutation.

func GetDefaultFactory added in v0.37.0

func GetDefaultFactory() LLMFactory

GetDefaultFactory returns the default factory, initializing it if necessary.

type LLMMetadata added in v0.83.0

type LLMMetadata interface {
	ProviderNamer
	ModelIdentifier
	CapabilityProvider
}

LLMMetadata provides provider/model metadata independent of generation methods.

type LLMRegistry

type LLMRegistry interface {
	// RegisterProvider registers a new provider factory
	RegisterProvider(name string, factory ProviderFactory) error

	// UnregisterProvider removes a provider
	UnregisterProvider(name string) error

	// CreateLLM creates an LLM instance using the registry
	CreateLLM(ctx context.Context, apiKey string, modelID ModelID) (LLM, error)

	// CreateLLMWithConfig creates an LLM instance with specific configuration
	CreateLLMWithConfig(ctx context.Context, config ProviderConfig, modelID ModelID) (LLM, error)

	// LoadFromConfig loads provider configurations from a config structure
	LoadFromConfig(ctx context.Context, configs map[string]ProviderConfig) error

	// ListProviders returns all registered provider names
	ListProviders() []string

	// GetProviderConfig returns the configuration for a provider
	GetProviderConfig(name string) (ProviderConfig, bool)

	// IsModelSupported checks if a model is supported by any provider
	IsModelSupported(modelID ModelID) bool

	// GetModelProvider returns the provider name for a model
	GetModelProvider(modelID ModelID) (string, bool)

	// SetDefaultProvider sets the default provider for models
	SetDefaultProvider(name string) error

	// RefreshProvider reloads a provider configuration
	RefreshProvider(ctx context.Context, name string, config ProviderConfig) error
}

LLMRegistry manages dynamic registration and creation of LLM providers.

var (

	// GlobalRegistry is the package-level registry for backward compatibility.
	// Prefer GetRegistry and SetRegistry over direct mutation.
	GlobalRegistry LLMRegistry
)

GlobalRegistry is the global registry instance.

func GetRegistry added in v0.37.0

func GetRegistry() LLMRegistry

GetRegistry returns the global registry instance.

func NewLLMRegistry

func NewLLMRegistry() LLMRegistry

NewLLMRegistry creates a lazily constructed registry wrapper backed by the currently installed registry constructor.

type LLMResponse added in v0.6.0

type LLMResponse struct {
	Content  string
	Usage    *TokenInfo
	Metadata map[string]any
}

type LMConfigProvider added in v0.27.0

type LMConfigProvider interface {
	GetLLMIdentifier() map[string]string // Returns map like {"provider": "OpenAI", "model": "gpt-4"}
}

LMConfigProvider is an interface that modules can optionally implement to allow their LM configuration identifiers to be saved and checked.

type LoadProgramOptions added in v0.83.0

type LoadProgramOptions struct {
	// WarningHandler receives non-fatal compatibility or shape warnings encountered
	// while loading state. When nil, warnings are ignored.
	WarningHandler func(message string)
}

LoadProgramOptions controls optional behavior for program-state loading.

type Metric

type Metric func(expected, actual map[string]any) float64

Metric is a function type that evaluates the performance of a program.

type ModelConfig added in v0.37.0

type ModelConfig struct {
	// Model identifier
	ID string `json:"id" yaml:"id"`

	// Display name
	Name string `json:"name,omitempty" yaml:"name,omitempty"`

	// Model capabilities
	Capabilities []string `json:"capabilities,omitempty" yaml:"capabilities,omitempty"`

	// Model-specific parameters
	Params map[string]any `json:"params,omitempty" yaml:"params,omitempty"`

	// Default generation options
	DefaultOptions *GenerateOptions `json:"default_options,omitempty" yaml:"default_options,omitempty"`
}

ModelConfig represents configuration for a specific model.

func (ModelConfig) Clone added in v0.83.1

func (config ModelConfig) Clone() ModelConfig

Clone returns a deep copy of the model configuration.

type ModelContextDecorator deprecated added in v0.14.0

type ModelContextDecorator struct {
	BaseDecorator
}

Deprecated: prefer explicit RecordLLMCall calls at LLM boundaries instead of wrapping LLM implementations with decorators. ModelContextDecorator adds model context tracking.

func NewModelContextDecorator deprecated added in v0.14.0

func NewModelContextDecorator(base LLM) *ModelContextDecorator

Deprecated: prefer explicit RecordLLMCall calls at LLM boundaries instead of wrapping LLM implementations with decorators.

func (*ModelContextDecorator) Generate added in v0.14.0

func (d *ModelContextDecorator) Generate(ctx context.Context, prompt string, options ...GenerateOption) (*LLMResponse, error)

func (*ModelContextDecorator) GenerateWithTools added in v0.80.0

func (d *ModelContextDecorator) GenerateWithTools(ctx context.Context, messages []ChatMessage, tools []map[string]any, opts ...GenerateOption) (map[string]any, error)

type ModelID

type ModelID string

ModelID represents the available model IDs.

const (
	// Anthropic models - Claude 3.x and newer series.
	ModelAnthropicHaiku          ModelID = ModelID(anthropic.ModelClaude_3_Haiku_20240307)
	ModelAnthropicSonnet         ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929) // For backwards compatibility
	ModelAnthropicSonnet35       ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)
	ModelAnthropicOpus           ModelID = ModelID(anthropic.ModelClaudeOpus4_1_20250805)
	ModelAnthropicClaude4Opus    ModelID = ModelID(anthropic.ModelClaudeOpus4_1_20250805)
	ModelAnthropicClaude4Sonnet  ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)
	ModelAnthropicClaude45Sonnet ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)
	ModelAnthropicClaude45Opus   ModelID = ModelID(anthropic.ModelClaudeOpus4_5_20251101)
	// Claude 4.6 series.
	ModelAnthropicClaude46Sonnet ModelID = ModelID(anthropic.ModelClaudeSonnet4_6)
	ModelAnthropicClaude46Opus   ModelID = ModelID(anthropic.ModelClaudeOpus4_6)

	// Google Gemini models (existing).
	ModelGoogleGeminiFlash     ModelID = "gemini-2.5-flash"
	ModelGoogleGeminiPro       ModelID = "gemini-2.5-pro"
	ModelGoogleGeminiFlashLite ModelID = "gemini-2.5-flash-lite"
	// Google Gemini 3 series (new).
	ModelGoogleGemini3ProPreview   ModelID = "gemini-3-pro-preview"
	ModelGoogleGemini3FlashPreview ModelID = "gemini-3-flash-preview"
	// Google Gemini 2.0 series (new).
	ModelGoogleGemini20Flash     ModelID = "gemini-2.0-flash"
	ModelGoogleGemini20FlashLite ModelID = "gemini-2.0-flash-lite"

	// OpenAI models - GPT-4 series.
	ModelOpenAIGPT4       ModelID = "gpt-4"
	ModelOpenAIGPT4Turbo  ModelID = "gpt-4-turbo"
	ModelOpenAIGPT35Turbo ModelID = "gpt-3.5-turbo"
	ModelOpenAIGPT4o      ModelID = "gpt-4o"
	ModelOpenAIGPT4oMini  ModelID = "gpt-4o-mini"
	// OpenAI models - GPT-4.1 series.
	ModelOpenAIGPT41     ModelID = "gpt-4.1"
	ModelOpenAIGPT41Mini ModelID = "gpt-4.1-mini"
	ModelOpenAIGPT41Nano ModelID = "gpt-4.1-nano"
	// OpenAI models - o1 reasoning series.
	ModelOpenAIO1     ModelID = "o1"
	ModelOpenAIO1Pro  ModelID = "o1-pro"
	ModelOpenAIO1Mini ModelID = "o1-mini"
	// OpenAI models - o3 reasoning series.
	ModelOpenAIO3     ModelID = "o3"
	ModelOpenAIO3Mini ModelID = "o3-mini"
	// OpenAI models - GPT-5 series.
	ModelOpenAIGPT5     ModelID = "gpt-5"
	ModelOpenAIGPT5Mini ModelID = "gpt-5-mini"
	ModelOpenAIGPT5Nano ModelID = "gpt-5-nano"
	// OpenAI models - GPT-5.2 series (instant, thinking, pro, codex).
	ModelOpenAIGPT52          ModelID = "gpt-5.2"
	ModelOpenAIGPT52Instant   ModelID = "gpt-5.2-instant"
	ModelOpenAIGPT52Thinking  ModelID = "gpt-5.2-thinking"
	ModelOpenAIGPT52ThinkHigh ModelID = "gpt-5.2-thinking-high"
	ModelOpenAIGPT52Pro       ModelID = "gpt-5.2-pro"
	ModelOpenAIGPT52Codex     ModelID = "gpt-5.2-codex"

	// LiteLLM models (can proxy to any provider).
	ModelLiteLLMGPT4    ModelID = "gpt-4"
	ModelLiteLLMClaude3 ModelID = "claude-3-sonnet-20240229"
	ModelLiteLLMLlama2  ModelID = "llama-2-70b-chat"
	ModelLiteLLMGemini  ModelID = "gemini-pro"

	// Ollama models (OpenAI-compatible mode).
	ModelOllamaLlama3_8B    ModelID = "llama3:8b"
	ModelOllamaLlama3_70B   ModelID = "llama3:70b"
	ModelOllamaLlama3_1_8B  ModelID = "llama3.1:8b"
	ModelOllamaLlama3_1_70B ModelID = "llama3.1:70b"
	ModelOllamaLlama3_2_3B  ModelID = "llama3.2:3b"
	ModelOllamaCodeLlama13B ModelID = "codellama:13b"
	ModelOllamaCodeLlama34B ModelID = "codellama:34b"
	ModelOllamaMistral7B    ModelID = "mistral:7b"
	ModelOllamaGemma2B      ModelID = "gemma:2b"
	ModelOllamaGemma7B      ModelID = "gemma:7b"
	ModelOllamaQwen2_5_7B   ModelID = "qwen2.5:7b"
	ModelOllamaQwen2_5_14B  ModelID = "qwen2.5:14b"

	// Ollama embedding models.
	ModelOllamaNomicEmbed ModelID = "nomic-embed-text"
	ModelOllamaMxbaiEmbed ModelID = "mxbai-embed-large"
	ModelOllamaAllMiniLM  ModelID = "all-minilm"

	// LocalAI models.
	ModelLocalAILlama2    ModelID = "llama-2-7b-chat"
	ModelLocalAICodeLlama ModelID = "codellama-13b-instruct"
	ModelLocalAIAlpaca    ModelID = "alpaca-7b"
	ModelLocalAIVicuna    ModelID = "vicuna-7b"

	// FastChat models.
	ModelFastChatVicuna    ModelID = "vicuna-7b-v1.5"
	ModelFastChatAlpaca    ModelID = "alpaca-13b"
	ModelFastChatCodeLlama ModelID = "codellama-7b-instruct"
	ModelFastChatLlama2    ModelID = "llama-2-7b-chat"

	// Generic OpenAI-compatible models.
	ModelCompatibleGPT4   ModelID = "gpt-4"
	ModelCompatibleGPT35  ModelID = "gpt-3.5-turbo"
	ModelCompatibleClaude ModelID = "claude-3-sonnet"
	ModelCompatibleLlama2 ModelID = "llama-2-chat"
)

type ModelIdentifier added in v0.83.0

type ModelIdentifier interface {
	ModelID() string
}

ModelIdentifier identifies the underlying model.

type Module

type Module interface {
	// Process executes the module's logic
	Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)

	// GetSignature returns the module's input and output signature
	GetSignature() Signature

	SetSignature(signature Signature)

	// SetLLM sets the language model for the module
	SetLLM(llm LLM)

	// Clone creates a deep copy of the module
	Clone() Module

	// GetDisplayName returns a human-readable name for this module instance
	GetDisplayName() string

	// GetModuleType returns the category/type of this module
	GetModuleType() string
}

Module represents a basic unit of computation in DSPy.

type ModuleChain

type ModuleChain struct {
	BaseModule
	Modules []Module
}

ModuleChain represents a chain of modules.

func NewModuleChain

func NewModuleChain(modules ...Module) *ModuleChain

NewModuleChain creates a new module chain.

func (*ModuleChain) Clone added in v0.83.1

func (mc *ModuleChain) Clone() Module

Clone deep-copies the module chain and its child modules.

func (*ModuleChain) Process added in v0.83.1

func (mc *ModuleChain) Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)

Process executes each module in sequence, merging prior outputs into the next module's inputs while returning the last module's outputs.

func (*ModuleChain) SetLLM added in v0.83.1

func (mc *ModuleChain) SetLLM(llm LLM)

SetLLM sets the LLM on the chain and propagates it to child modules.

type ModuleHandler added in v0.45.0

type ModuleHandler func(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)

ModuleHandler represents the actual module process function.

type ModuleInfo added in v0.45.0

type ModuleInfo struct {
	// ModuleName is the human-readable name of the module instance
	ModuleName string

	// ModuleType is the category/type of the module (e.g., "ChainOfThought", "React")
	ModuleType string

	// Signature contains the input/output structure definition
	Signature Signature

	// LLM is the module-local language model when one is configured.
	// Interceptors should prefer this over process-global defaults.
	LLM LLM

	// Version is the module version for compatibility tracking
	Version string

	// Metadata contains additional module-specific information
	Metadata map[string]any
}

ModuleInfo contains metadata about a module for interceptor use.

func NewModuleInfo added in v0.45.0

func NewModuleInfo(moduleName, moduleType string, signature Signature) *ModuleInfo

NewModuleInfo creates a ModuleInfo with the provided details. This is a convenience function to ensure consistent ModuleInfo creation.

func (*ModuleInfo) WithLLM added in v0.83.1

func (mi *ModuleInfo) WithLLM(llm LLM) *ModuleInfo

WithLLM records the module-local LLM for downstream interceptor resolution.

func (*ModuleInfo) WithMetadata added in v0.45.0

func (mi *ModuleInfo) WithMetadata(key string, value any) *ModuleInfo

WithMetadata adds metadata to ModuleInfo.

func (*ModuleInfo) WithVersion added in v0.45.0

func (mi *ModuleInfo) WithVersion(version string) *ModuleInfo

WithVersion sets the version for ModuleInfo.

type ModuleInterceptor added in v0.45.0

type ModuleInterceptor func(ctx context.Context, inputs map[string]any, info *ModuleInfo, handler ModuleHandler, opts ...Option) (map[string]any, error)

ModuleInterceptor wraps Module.Process() calls with additional functionality. It follows the gRPC interceptor pattern: the interceptor can inspect/modify the request, call the handler, and inspect/modify the response.

func ChainModuleInterceptors added in v0.45.0

func ChainModuleInterceptors(interceptors ...ModuleInterceptor) ModuleInterceptor

ChainModuleInterceptors combines multiple module interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type ModuleOptions added in v0.17.0

type ModuleOptions struct {
	// LLM generation options
	GenerateOptions []GenerateOption

	StreamHandler StreamHandler
}

ModuleOptions holds configuration that can be passed to modules.

func (*ModuleOptions) Clone added in v0.17.1

func (o *ModuleOptions) Clone() *ModuleOptions

Clone creates a copy of ModuleOptions.

func (*ModuleOptions) MergeWith added in v0.17.1

func (o *ModuleOptions) MergeWith(other *ModuleOptions) *ModuleOptions

MergeWith merges this options with other options, with other taking precedence.

type Optimizer

type Optimizer interface {
	// Compile optimizes a given program using the provided dataset and metric
	Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)
}

Optimizer represents an interface for optimizing DSPy programs.

type OptimizerFactory

type OptimizerFactory func() (Optimizer, error)

OptimizerFactory is a function type for creating Optimizer instances.

type OptimizerRegistry

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

OptimizerRegistry maintains a registry of available Optimizer implementations.

func NewOptimizerRegistry

func NewOptimizerRegistry() *OptimizerRegistry

NewOptimizerRegistry creates a new OptimizerRegistry.

func (*OptimizerRegistry) Create

func (r *OptimizerRegistry) Create(name string) (Optimizer, error)

Create instantiates a new Optimizer based on the given name.

func (*OptimizerRegistry) Register

func (r *OptimizerRegistry) Register(name string, factory OptimizerFactory)

Register adds a new Optimizer factory to the registry.

type Option added in v0.17.0

type Option func(*ModuleOptions)

func WithGenerateOptions added in v0.17.0

func WithGenerateOptions(opts ...GenerateOption) Option

WithGenerateOptions adds LLM generation options.

func WithOptions added in v0.23.0

func WithOptions(opts ...Option) Option

func WithStreamHandler added in v0.23.0

func WithStreamHandler(handler StreamHandler) Option

WithStreamHandler returns an option to enable streaming.

type OutputField

type OutputField struct {
	Field
}

OutputField represents an output field.

type ParameterConsumer added in v0.27.0

type ParameterConsumer interface {
	SetTunedParameters(params map[string]any) error // Return error for validation
}

ParameterConsumer is an interface that modules can optionally implement to allow their tuned parameters to be loaded.

type ParameterProvider added in v0.27.0

type ParameterProvider interface {
	GetTunedParameters() map[string]any
}

ParameterProvider is an interface that modules can optionally implement to allow their tuned parameters to be saved.

type Program

type Program struct {
	Modules map[string]Module
	Forward func(ctx context.Context, inputs map[string]any) (map[string]any, error)
	// contains filtered or unexported fields
}

Program represents a complete DSPy pipeline or workflow.

func NewProgram

func NewProgram(modules map[string]Module, forward func(context.Context, map[string]any) (map[string]any, error)) Program

NewProgram creates a new Program with the given modules and forward function.

func NewProgramWithForwardFactory added in v0.80.0

func NewProgramWithForwardFactory(modules map[string]Module, factory ForwardFactory) Program

NewProgramWithForwardFactory creates a program whose forward function can be safely rebound against cloned or replaced modules.

func (*Program) AddModule

func (p *Program) AddModule(name string, module Module)

AddModule adds a new module to the Program.

func (Program) Clone

func (p Program) Clone() Program

Clone creates a deep copy of the Program.

func (Program) Equal

func (p Program) Equal(other Program) bool

Equal checks if two Programs are equivalent.

func (Program) Execute

func (p Program) Execute(ctx context.Context, inputs map[string]any) (map[string]any, error)

Execute runs the program with the given inputs.

func (*Program) GetModules added in v0.1.0

func (p *Program) GetModules() []Module

GetModules returns all modules in deterministic alphabetical order by name. This ensures consistent behavior across operations that iterate over modules.

func (Program) GetSignature

func (p Program) GetSignature() Signature

GetSignature returns the overall signature of the program This would need to be defined based on the Forward function's expected inputs and outputs.

func (Program) RebindModules added in v0.80.0

func (p Program) RebindModules(modules map[string]Module) Program

RebindModules returns a new program with the provided module map. When a forward factory is available, the returned program rebuilds its forward function against the supplied modules; otherwise it preserves the original forward function pointer.

func (*Program) SetForward

func (p *Program) SetForward(forward func(context.Context, map[string]any) (map[string]any, error))

SetForward sets the forward function for the Program.

func (*Program) SetForwardFactory added in v0.80.0

func (p *Program) SetForwardFactory(factory ForwardFactory)

SetForwardFactory sets a clone-safe forward factory for the Program.

type ProgressReporter

type ProgressReporter interface {
	Report(stage string, processed, total int)
}

type PromptModel added in v0.83.0

type PromptModel interface {
	TextGenerator
	ModelIdentifier
}

PromptModel identifies a model that can generate text from prompts and expose its model ID.

type ProviderConfig added in v0.37.0

type ProviderConfig struct {
	// Provider name (e.g., "anthropic", "google", "ollama")
	Name string `json:"name" yaml:"name"`

	// API configuration
	APIKey  string `json:"api_key,omitempty" yaml:"api_key,omitempty"`
	BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`

	// Model-specific configuration
	Models map[string]ModelConfig `json:"models,omitempty" yaml:"models,omitempty"`

	// Provider-specific parameters
	Params map[string]any `json:"params,omitempty" yaml:"params,omitempty"`

	// Endpoint configuration
	Endpoint *EndpointConfig `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}

ProviderConfig represents configuration for a specific provider.

func (ProviderConfig) Clone added in v0.83.1

func (config ProviderConfig) Clone() ProviderConfig

Clone returns a deep copy of the provider configuration.

type ProviderFactory added in v0.37.0

type ProviderFactory func(ctx context.Context, config ProviderConfig, modelID ModelID) (LLM, error)

ProviderFactory creates LLM instances for a specific provider.

type ProviderNamer added in v0.83.0

type ProviderNamer interface {
	ProviderName() string
}

ProviderNamer identifies the underlying LLM provider.

type RegistryBasedFactory added in v0.37.0

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

RegistryBasedFactory implements LLMFactory using the registry system.

func NewRegistryBasedFactory added in v0.37.0

func NewRegistryBasedFactory(registry LLMRegistry) *RegistryBasedFactory

NewRegistryBasedFactory creates a new factory that uses the registry system.

func (*RegistryBasedFactory) CreateLLM added in v0.37.0

func (f *RegistryBasedFactory) CreateLLM(apiKey string, modelID ModelID) (LLM, error)

CreateLLM creates an LLM instance using the registry.

type RegistryConfig added in v0.37.0

type RegistryConfig struct {
	// Providers configuration
	Providers map[string]ProviderConfig `json:"providers,omitempty" yaml:"providers,omitempty"`

	// Default provider
	DefaultProvider string `json:"default_provider,omitempty" yaml:"default_provider,omitempty"`

	// Cache settings
	CacheEnabled bool          `json:"cache_enabled,omitempty" yaml:"cache_enabled,omitempty"`
	CacheTTL     time.Duration `json:"cache_ttl,omitempty" yaml:"cache_ttl,omitempty"`
}

RegistryConfig represents the configuration for the registry system.

type RegistryConstructor added in v0.83.1

type RegistryConstructor func() LLMRegistry

RegistryConstructor creates a concrete LLMRegistry backend for the compatibility-facing core registry entry points.

type Runtime added in v0.83.1

type Runtime struct {
	DefaultLLM LLM
	TeacherLLM LLM
}

Runtime holds explicit runtime-scoped dependencies for module execution. It is intended as a compatibility-friendly alternative to relying solely on package-level globals when resolving models during data-plane execution.

func RuntimeFromContext added in v0.83.1

func RuntimeFromContext(ctx context.Context) *Runtime

RuntimeFromContext retrieves a runtime from context when present.

type SavedExample added in v0.27.0

type SavedExample struct {
	Inputs  map[string]any `json:"inputs"`
	Outputs map[string]any `json:"outputs"`
}

SavedExample represents a serializable dspy.Example.

type SavedModuleState added in v0.27.0

type SavedModuleState struct {
	Signature       string            `json:"signature"`                  // Module's signature string representation (READ-ONLY during load)
	Demos           []SavedExample    `json:"demos,omitempty"`            // Saved demos, if the module provides them
	LMIdentifier    map[string]string `json:"lm_identifier,omitempty"`    // Identifying info for the LM used (e.g., provider, model)
	TunedParameters map[string]any    `json:"tuned_parameters,omitempty"` // Module-specific tuned parameters (e.g., k for retriever)
	ModuleType      string            `json:"module_type"`                // Concrete type name (e.g., "Predict")

}

SavedModuleState represents the serializable state of a single DSPy module. Specific fields might vary depending on the module type (Predict, CoT, etc.).

type SavedProgramState added in v0.27.0

type SavedProgramState struct {
	Modules  map[string]SavedModuleState `json:"modules"`  // Map module ID/name to its state
	Metadata map[string]string           `json:"metadata"` // e.g., {"dspy_go_version": "..."}
}

SavedProgramState represents the serializable state of an entire DSPy program.

type Signature

type Signature struct {
	Inputs      []InputField
	Outputs     []OutputField
	Instruction string
}

Signature represents the input and output specification of a module.

func NewSignature

func NewSignature(inputs []InputField, outputs []OutputField) Signature

NewSignature creates a new Signature with the given inputs and outputs.

func ParseSignature

func ParseSignature(signatureStr string) (Signature, error)

ParseSignature parses a signature string into a Signature struct.

func ShorthandNotation deprecated

func ShorthandNotation(notation string) (Signature, error)

Deprecated: use ParseSignature. ShorthandNotation creates a Signature from a shorthand notation string.

func (Signature) AppendInput added in v0.30.0

func (s Signature) AppendInput(name string, prefix string, description string) Signature

AppendInput adds an input field to the signature.

func (Signature) PrependOutput added in v0.30.0

func (s Signature) PrependOutput(name string, prefix string, description string) Signature

PrependOutput adds an output field to the beginning of the outputs.

func (Signature) String

func (s Signature) String() string

String returns a string representation of the Signature.

func (Signature) WithInstruction

func (s Signature) WithInstruction(instruction string) Signature

WithInstruction adds an instruction to the Signature.

type SignatureMetadata added in v0.52.0

type SignatureMetadata struct {
	Inputs      []FieldMetadata
	Outputs     []FieldMetadata
	Instruction string
}

SignatureMetadata contains parsed information from struct tags.

type Span added in v0.9.0

type Span struct {
	ID          string
	ParentID    string
	Operation   string
	StartTime   time.Time
	EndTime     time.Time
	Error       error
	Annotations map[string]any
	// contains filtered or unexported fields
}

Span represents a single operation within the execution.

func CollectSpans added in v0.9.0

func CollectSpans(ctx context.Context) []*Span

Helper method to collect all spans.

func StartSpan added in v0.9.0

func StartSpan(ctx context.Context, operation string) (context.Context, *Span)

StartSpan begins a new operation span.

func StartSpanWithContext added in v0.36.0

func StartSpanWithContext(ctx context.Context, operation string, moduleName string, metadata map[string]any) (context.Context, *Span)

StartSpanWithContext begins a new operation span with additional context information.

func (*Span) WithAnnotation added in v0.9.0

func (s *Span) WithAnnotation(key string, value any)

func (*Span) WithError added in v0.9.0

func (s *Span) WithError(err error)

Span methods.

type StreamChunk added in v0.23.0

type StreamChunk struct {
	Content string     // The text content of this chunk
	Done    bool       // Indicates if this is the final chunk
	Error   error      // Any error that occurred during streaming
	Usage   *TokenInfo // Optional token usage information (may be nil)
}

type StreamContentGenerator added in v0.83.0

type StreamContentGenerator interface {
	StreamGenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*StreamResponse, error)
}

StreamContentGenerator streams completions from multimodal content blocks.

type StreamGenerator added in v0.83.0

type StreamGenerator interface {
	StreamGenerate(ctx context.Context, prompt string, options ...GenerateOption) (*StreamResponse, error)
}

StreamGenerator streams text completions from a string prompt.

type StreamHandler added in v0.23.0

type StreamHandler func(chunk StreamChunk) error

type StreamResponse added in v0.23.0

type StreamResponse struct {
	ChunkChannel <-chan StreamChunk // Channel receiving response chunks
	Cancel       func()             // Function to cancel the stream
}

StreamResponse encapsulates a streaming response.

type TextGenerator added in v0.83.0

type TextGenerator interface {
	Generate(ctx context.Context, prompt string, options ...GenerateOption) (*LLMResponse, error)
}

TextGenerator produces text completions from a string prompt.

type TokenInfo added in v0.6.0

type TokenInfo struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

type TokenUsage added in v0.9.0

type TokenUsage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	Cost             float64
}

TokenUsage tracks token consumption.

type Tool added in v0.18.0

type Tool interface {
	// Name returns the tool's identifier
	Name() string

	// Description returns human-readable explanation of the tool's purpose
	Description() string

	// Metadata returns the tool's metadata
	Metadata() *ToolMetadata

	// CanHandle checks if the tool can handle a specific action/intent
	CanHandle(ctx context.Context, intent string) bool

	// Execute runs the tool with provided parameters
	Execute(ctx context.Context, params map[string]any) (ToolResult, error)

	// Validate checks if the parameters match the expected schema
	Validate(params map[string]any) error

	// InputSchema returns the expected parameter structure
	InputSchema() models.InputSchema
}

Tool represents a capability that can be used by both agents and modules.

type ToolApprovalDecision added in v0.82.0

type ToolApprovalDecision struct {
	Allowed bool
	Reason  string
}

ToolApprovalDecision records the result of a policy or user approval check.

type ToolApprovalFunc added in v0.82.0

type ToolApprovalFunc func(ctx context.Context, info *ToolInfo, args map[string]any) (ToolApprovalDecision, error)

ToolApprovalFunc decides whether a tool call should be allowed to proceed.

type ToolBlockedError added in v0.82.0

type ToolBlockedError struct {
	Reason string
}

ToolBlockedError indicates that a tool call was intentionally denied before execution.

func (*ToolBlockedError) Error added in v0.82.0

func (e *ToolBlockedError) Error() string

func (*ToolBlockedError) Unwrap added in v0.82.0

func (e *ToolBlockedError) Unwrap() error

type ToolCall added in v0.76.0

type ToolCall struct {
	ID        string         `json:"id"` // provider-assigned ID; required for tool-result correlation
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments"`
	Metadata  map[string]any `json:"metadata,omitempty"`
}

ToolCall represents a tool invocation requested by an LLM. Used across packages: core.ChatMessage, agents.Message, tools.ParseToolCalls.

type ToolCallingChatLLM added in v0.76.0

type ToolCallingChatLLM interface {
	GenerateWithTools(ctx context.Context, messages []ChatMessage, tools []map[string]any, opts ...GenerateOption) (map[string]any, error)
}

ToolCallingChatLLM is an OPTIONAL interface for providers that support multi-turn conversations with native tool calling (OpenAI, Anthropic, Gemini). NativeStrategy checks for this at runtime via type assertion.

Providers that don't implement this fall back to GenerateWithFunctions with a flattened prompt string (single-turn, best-effort compatibility).

type ToolHandler added in v0.45.0

type ToolHandler func(ctx context.Context, args map[string]any) (ToolResult, error)

ToolHandler represents the actual tool execute function.

type ToolInfo added in v0.45.0

type ToolInfo struct {
	// Name is the unique identifier for the tool
	Name string

	// Description is a human-readable explanation of the tool's purpose
	Description string

	// InputSchema defines the expected parameter structure
	InputSchema models.InputSchema

	// ToolType is the category/type of the tool (e.g., "MCPTool", "FunctionTool")
	ToolType string

	// Version is the tool version for compatibility tracking
	Version string

	// Capabilities lists the specific capabilities this tool provides
	Capabilities []string

	// Metadata contains additional tool-specific information
	Metadata map[string]any
}

ToolInfo contains metadata about a tool for interceptor use.

func NewToolInfo added in v0.45.0

func NewToolInfo(name, description, toolType string, inputSchema models.InputSchema) *ToolInfo

NewToolInfo creates a ToolInfo with the provided details. This is a convenience function to ensure consistent ToolInfo creation.

func NewToolInfoFromTool added in v0.82.0

func NewToolInfoFromTool(tool Tool) *ToolInfo

NewToolInfoFromTool creates ToolInfo from a core.Tool while tolerating nil metadata.

func (*ToolInfo) WithCapabilities added in v0.45.0

func (ti *ToolInfo) WithCapabilities(capabilities ...string) *ToolInfo

WithCapabilities sets the capabilities for ToolInfo.

func (*ToolInfo) WithMetadata added in v0.45.0

func (ti *ToolInfo) WithMetadata(key string, value any) *ToolInfo

WithMetadata adds metadata to ToolInfo.

func (*ToolInfo) WithVersion added in v0.45.0

func (ti *ToolInfo) WithVersion(version string) *ToolInfo

WithVersion sets the version for ToolInfo.

type ToolInterceptor added in v0.45.0

type ToolInterceptor func(ctx context.Context, args map[string]any, info *ToolInfo, handler ToolHandler) (ToolResult, error)

ToolInterceptor wraps Tool.Execute() calls with additional functionality. This allows interception of tool invocations for logging, validation, security, etc.

func ChainToolInterceptors added in v0.45.0

func ChainToolInterceptors(interceptors ...ToolInterceptor) ToolInterceptor

ChainToolInterceptors combines multiple tool interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type ToolMetadata added in v0.18.0

type ToolMetadata struct {
	Name          string             // Unique identifier for the tool
	Description   string             // Human-readable description
	InputSchema   models.InputSchema // Rich schema from MCP-Go - now consistent with Tool interface
	OutputSchema  map[string]string  // Keep this field for backward compatibility
	Capabilities  []string           // List of supported capabilities
	ContextNeeded []string           // Required context keys
	Version       string             // Tool version for compatibility
}

ToolMetadata contains information about a tool's capabilities and requirements.

type ToolRegistry added in v0.18.0

type ToolRegistry interface {
	Register(tool Tool) error
	Get(name string) (Tool, error)
	List() []Tool
	Match(intent string) []Tool
}

ToolRegistry manages available tools.

type ToolResult added in v0.18.0

type ToolResult struct {
	Data        any            // The actual result data
	Metadata    map[string]any // Execution metadata (timing, resources used, etc)
	Annotations map[string]any // Additional context for result interpretation
}

ToolResult wraps tool execution results with metadata.

type TransportConfig added in v0.65.0

type TransportConfig struct {
	MaxIdleConns        int           // Total idle connections across all hosts (default: 100)
	MaxIdleConnsPerHost int           // Idle connections per host (default: 100)
	MaxConnsPerHost     int           // Max concurrent connections per host (default: 100)
	IdleConnTimeout     time.Duration // How long idle connections stay open (default: 90s)
	TLSHandshakeTimeout time.Duration // TLS handshake timeout (default: 10s)
}

TransportConfig configures HTTP connection pooling behavior for LLM requests. Tuning these values can significantly improve performance for parallel workloads.

func DefaultTransportConfig added in v0.65.0

func DefaultTransportConfig() TransportConfig

DefaultTransportConfig returns optimized defaults for parallel LLM workloads. These values support up to 100 concurrent requests to the same API endpoint.

func (TransportConfig) ToTransport added in v0.65.0

func (tc TransportConfig) ToTransport() *http.Transport

ToTransport converts the config to an http.Transport.

type TypedSignature added in v0.52.0

type TypedSignature[TInput, TOutput any] interface {
	// GetInputType returns the reflect.Type for the input struct
	GetInputType() reflect.Type

	// GetOutputType returns the reflect.Type for the output struct
	GetOutputType() reflect.Type

	// ValidateInput performs compile-time and runtime validation of input
	ValidateInput(input TInput) error

	// ValidateOutput performs compile-time and runtime validation of output
	ValidateOutput(output TOutput) error

	// GetFieldMetadata returns parsed struct tag metadata
	GetFieldMetadata() SignatureMetadata

	// ToLegacySignature converts to the existing Signature interface for backward compatibility
	ToLegacySignature() Signature

	// WithInstruction returns a new TypedSignature with the specified instruction
	WithInstruction(instruction string) TypedSignature[TInput, TOutput]
}

TypedSignature provides compile-time type safety for module inputs and outputs.

func FromLegacySignature added in v0.52.0

func FromLegacySignature(sig Signature) TypedSignature[map[string]any, map[string]any]

Backward compatibility: convert legacy signature to typed.

func NewTypedSignature added in v0.52.0

func NewTypedSignature[TInput, TOutput any]() TypedSignature[TInput, TOutput]

NewTypedSignature creates a new typed signature for the given input/output types.

func NewTypedSignatureCached added in v0.52.0

func NewTypedSignatureCached[TInput, TOutput any]() TypedSignature[TInput, TOutput]

NewTypedSignatureCached creates a cached typed signature for the given input/output types. This function provides better performance for repeated calls with the same types.

type WorkloadType added in v0.65.0

type WorkloadType int

WorkloadType indicates whether LLM calls are I/O-bound or CPU-bound. This affects the optimal number of parallel workers.

const (
	// WorkloadIOBound indicates remote API calls (Gemini, OpenAI, Anthropic).
	// I/O-bound workloads benefit from many concurrent workers (default: 100).
	WorkloadIOBound WorkloadType = iota

	// WorkloadCPUBound indicates local model inference (Ollama, llama.cpp).
	// CPU-bound workloads should use NumCPU() workers to avoid thrashing.
	WorkloadCPUBound
)

Jump to

Keyboard shortcuts

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