Documentation
¶
Overview ¶
Package fantasy provides a unified interface for interacting with various AI language models.
Index ¶
- Variables
- func AsContentType[T Content](content Content) (T, bool)
- func AsMessagePart[T MessagePart](content MessagePart) (T, bool)
- func AsToolResultOutputType[T ToolResultOutputContent](content ToolResultOutputContent) (T, bool)
- func ErrorTitleForStatusCode(statusCode int) string
- func IsNoObjectGeneratedError(err error) bool
- func IsTransportError(err error) bool
- func MarshalProviderType[T any](typeID string, data T) ([]byte, error)
- func Opt[T any](v T) *T
- func ParseOptions[T any](options map[string]any, m *T) error
- func RegisterProviderType(typeID string, unmarshalFn UnmarshalFunc)
- func UnmarshalProviderType[T any](data []byte, target *T) error
- func WrapTransportError(err error) error
- type Agent
- type AgentCall
- type AgentOption
- func WithFrequencyPenalty(penalty float64) AgentOption
- func WithHeaders(headers map[string]string) AgentOption
- func WithMaxOutputTokens(tokens int64) AgentOption
- func WithMaxRetries(maxRetries int) AgentOption
- func WithOnRetry(callback OnRetryCallback) AgentOption
- func WithPrepareStep(fn PrepareStepFunction) AgentOption
- func WithPresencePenalty(penalty float64) AgentOption
- func WithProviderDefinedTools(tools ...ProviderTool) AgentOption
- func WithProviderOptions(providerOptions ProviderOptions) AgentOption
- func WithRepairToolCall(fn RepairToolCallFunction) AgentOption
- func WithStopConditions(conditions ...StopCondition) AgentOption
- func WithSystemPrompt(prompt string) AgentOption
- func WithTemperature(temp float64) AgentOption
- func WithToolChoice(choice ToolChoice) AgentOption
- func WithTools(tools ...AgentTool) AgentOption
- func WithTopK(topK int64) AgentOption
- func WithTopP(topP float64) AgentOption
- func WithUserAgent(ua string) AgentOption
- type AgentResult
- type AgentStreamCall
- type AgentTool
- type Call
- type CallWarning
- type CallWarningType
- type Content
- type ContentType
- type Error
- type ExecutableProviderTool
- type FileContent
- type FilePart
- type FinishReason
- type FunctionTool
- type LanguageModel
- type Message
- type MessagePart
- type MessageRole
- type NoObjectGeneratedError
- type ObjectCall
- type ObjectMode
- type ObjectResponse
- type ObjectResult
- type ObjectStreamPart
- type ObjectStreamPartType
- type ObjectStreamResponse
- type OnAgentFinishFunc
- type OnAgentStartFunc
- type OnAuthRefreshFunc
- type OnChunkFunc
- type OnErrorFunc
- type OnFinishFunc
- type OnReasoningDeltaFunc
- type OnReasoningEndFunc
- type OnReasoningStartFunc
- type OnRetryCallback
- type OnSourceFunc
- type OnStepFinishFunc
- type OnStepFinishedFunction
- type OnStepStartFunc
- type OnStreamFinishFunc
- type OnTextDeltaFunc
- type OnTextEndFunc
- type OnTextStartFunc
- type OnToolCallFunc
- type OnToolInputDeltaFunc
- type OnToolInputEndFunc
- type OnToolInputStartFunc
- type OnToolResultFunc
- type OnWarningsFunc
- type PrepareStepFunction
- type PrepareStepFunctionOptions
- type PrepareStepResult
- type Prompt
- type Provider
- type ProviderDefinedTool
- type ProviderError
- type ProviderMetadata
- type ProviderOptions
- type ProviderOptionsData
- type ProviderTool
- type ReasoningContent
- type ReasoningPart
- type RepairToolCallFunction
- type Response
- type ResponseContent
- func (r ResponseContent) Files() []FileContent
- func (r ResponseContent) Reasoning() []ReasoningContent
- func (r ResponseContent) ReasoningText() string
- func (r ResponseContent) Sources() []SourceContent
- func (r ResponseContent) Text() string
- func (r ResponseContent) ToolCalls() []ToolCallContent
- func (r ResponseContent) ToolResults() []ToolResultContent
- type RetryError
- type RetryFn
- type RetryFunction
- type RetryOptions
- type Schema
- type SourceContent
- type SourceType
- type StepResult
- type StopCondition
- type StreamObjectResult
- type StreamPart
- type StreamPartType
- type StreamResponse
- type TextContent
- type TextPart
- type Tool
- type ToolCall
- type ToolCallContent
- type ToolCallPart
- type ToolCallRepairOptions
- type ToolChoice
- type ToolInfo
- type ToolResponse
- func NewImageResponse(data []byte, mediaType string) ToolResponse
- func NewMediaResponse(data []byte, mediaType string) ToolResponse
- func NewTextErrorResponse(content string) ToolResponse
- func NewTextResponse(content string) ToolResponse
- func WithResponseMetadata(response ToolResponse, metadata any) ToolResponse
- type ToolResultContent
- type ToolResultContentType
- type ToolResultOutputContent
- type ToolResultOutputContentError
- type ToolResultOutputContentMedia
- type ToolResultOutputContentText
- type ToolResultPart
- type ToolType
- type UnmarshalFunc
- type Usage
Constants ¶
This section is empty.
Variables ¶
var Version = strings.TrimSpace(version)
Version is the SDK version, read from version.txt.
Functions ¶
func AsContentType ¶
AsContentType converts a Content interface to a specific content type.
func AsMessagePart ¶
func AsMessagePart[T MessagePart](content MessagePart) (T, bool)
AsMessagePart converts a MessagePart interface to a specific message part type.
func AsToolResultOutputType ¶
func AsToolResultOutputType[T ToolResultOutputContent](content ToolResultOutputContent) (T, bool)
AsToolResultOutputType converts a ToolResultOutputContent interface to a specific type.
func ErrorTitleForStatusCode ¶ added in v0.2.0
ErrorTitleForStatusCode returns a human-readable title for a given HTTP status code.
func IsNoObjectGeneratedError ¶ added in v0.3.0
IsNoObjectGeneratedError checks if an error is of type NoObjectGeneratedError.
func IsTransportError ¶ added in v0.38.0
IsTransportError reports whether err or any error in its chain is a transient transport-level failure that is safe to retry on a fresh connection. In practice these are HTTP/2 stream resets, connection errors, and GOAWAY frames, which originate from the transport rather than the application.
x/net/http2 error types are matched by type; Go's stdlib-bundled http2 uses unexported types, so those are matched by their message fragments.
func MarshalProviderType ¶ added in v0.3.0
MarshalProviderType marshals provider data with a type wrapper using generics. To avoid infinite recursion, use the "type plain T" pattern before calling this.
Usage in provider types:
func (o ProviderOptions) MarshalJSON() ([]byte, error) {
type plain ProviderOptions
return fantasy.MarshalProviderType(TypeProviderOptions, plain(o))
}
func ParseOptions ¶
ParseOptions parses the given options map into the provided struct.
func RegisterProviderType ¶ added in v0.3.0
func RegisterProviderType(typeID string, unmarshalFn UnmarshalFunc)
RegisterProviderType registers a provider type ID with its unmarshal function. Type IDs must be globally unique (e.g. "openai.options"). This should only be called during package initialization (init functions).
func UnmarshalProviderType ¶ added in v0.3.0
UnmarshalProviderType unmarshals provider data without type wrapper using generics. To avoid infinite recursion, unmarshal to a plain type first. Note: This receives the inner 'data' field after type routing by the registry.
Usage in provider types:
func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
type plain ProviderOptions
var p plain
if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
return err
}
*o = ProviderOptions(p)
return nil
}
func WrapTransportError ¶ added in v0.38.0
WrapTransportError wraps a transient transport failure in a retryable ProviderError so callers get a clean message and .IsRetryable() reports true. It recognizes an unexpected mid-stream EOF and HTTP/2 stream, connection, and GOAWAY resets. Any other error is returned unchanged.
This is the canonical entry point for provider error handlers: they can hand off whatever the transport surfaced without re-encoding which failures count as transient.
Types ¶
type Agent ¶
type Agent interface {
Generate(context.Context, AgentCall) (*AgentResult, error)
Stream(context.Context, AgentStreamCall) (*AgentResult, error)
}
Agent represents an AI agent that can generate responses and stream responses.
func NewAgent ¶
func NewAgent(model LanguageModel, opts ...AgentOption) Agent
NewAgent creates a new agent with the given language model and options.
type AgentCall ¶
type AgentCall struct {
Prompt string `json:"prompt"`
Files []FilePart `json:"files"`
Messages []Message `json:"messages"`
MaxOutputTokens *int64
Temperature *float64 `json:"temperature"`
TopP *float64 `json:"top_p"`
TopK *int64 `json:"top_k"`
PresencePenalty *float64 `json:"presence_penalty"`
FrequencyPenalty *float64 `json:"frequency_penalty"`
ActiveTools []string `json:"active_tools"`
ToolChoice *ToolChoice `json:"tool_choice"`
Headers map[string]string
ProviderOptions ProviderOptions
OnRetry OnRetryCallback
OnAuthRefresh OnAuthRefreshFunc
MaxRetries *int
// ModelProvider, when non-nil, is called on each retry attempt to
// obtain the language model. This allows callers to swap in a
// refreshed model after OnAuthRefresh rebuilds credentials. When
// nil, the model captured at step preparation time is used.
ModelProvider func() LanguageModel
StopWhen []StopCondition
PrepareStep PrepareStepFunction
RepairToolCall RepairToolCallFunction
}
AgentCall represents a call to an agent.
type AgentOption ¶
type AgentOption = func(*agentSettings)
AgentOption defines a function that configures agent settings.
func WithFrequencyPenalty ¶
func WithFrequencyPenalty(penalty float64) AgentOption
WithFrequencyPenalty sets the frequency penalty for the agent.
func WithHeaders ¶
func WithHeaders(headers map[string]string) AgentOption
WithHeaders sets the headers for the agent.
func WithMaxOutputTokens ¶
func WithMaxOutputTokens(tokens int64) AgentOption
WithMaxOutputTokens sets the maximum output tokens for the agent.
func WithMaxRetries ¶ added in v0.2.1
func WithMaxRetries(maxRetries int) AgentOption
WithMaxRetries sets the maximum number of retries for the agent.
func WithOnRetry ¶ added in v0.2.1
func WithOnRetry(callback OnRetryCallback) AgentOption
WithOnRetry sets the retry callback for the agent.
func WithPrepareStep ¶
func WithPrepareStep(fn PrepareStepFunction) AgentOption
WithPrepareStep sets the prepare step function for the agent.
func WithPresencePenalty ¶
func WithPresencePenalty(penalty float64) AgentOption
WithPresencePenalty sets the presence penalty for the agent.
func WithProviderDefinedTools ¶ added in v0.13.0
func WithProviderDefinedTools(tools ...ProviderTool) AgentOption
WithProviderDefinedTools registers provider-defined tools with the agent. Provider-executed tools (e.g. web search) are passed through to the API. Client-executed tools (ExecutableProviderTool) are also registered for local execution.
func WithProviderOptions ¶
func WithProviderOptions(providerOptions ProviderOptions) AgentOption
WithProviderOptions sets the provider options for the agent.
func WithRepairToolCall ¶
func WithRepairToolCall(fn RepairToolCallFunction) AgentOption
WithRepairToolCall sets the repair tool call function for the agent.
func WithStopConditions ¶
func WithStopConditions(conditions ...StopCondition) AgentOption
WithStopConditions sets the stop conditions for the agent.
func WithSystemPrompt ¶
func WithSystemPrompt(prompt string) AgentOption
WithSystemPrompt sets the system prompt for the agent.
func WithTemperature ¶
func WithTemperature(temp float64) AgentOption
WithTemperature sets the temperature for the agent.
func WithToolChoice ¶ added in v0.22.0
func WithToolChoice(choice ToolChoice) AgentOption
WithToolChoice sets the default tool choice for the agent. It is overridden by the ToolChoice on a specific call, and by PrepareStep at the step level.
func WithTools ¶
func WithTools(tools ...AgentTool) AgentOption
WithTools sets the tools for the agent.
func WithTopP ¶
func WithTopP(topP float64) AgentOption
WithTopP sets the top-p value for the agent.
func WithUserAgent ¶ added in v0.12.0
func WithUserAgent(ua string) AgentOption
WithUserAgent sets the User-Agent header for the agent. This overrides any provider-level User-Agent setting.
type AgentResult ¶
type AgentResult struct {
Steps []StepResult
// Final response. When the last step is tool-only (no text content),
// this is the response from the most recent step that contained text,
// so callers always see meaningful output without walking Steps manually.
Response Response
TotalUsage Usage
}
AgentResult represents the result of an agent execution.
type AgentStreamCall ¶
type AgentStreamCall struct {
Prompt string `json:"prompt"`
Files []FilePart `json:"files"`
Messages []Message `json:"messages"`
MaxOutputTokens *int64
Temperature *float64 `json:"temperature"`
TopP *float64 `json:"top_p"`
TopK *int64 `json:"top_k"`
PresencePenalty *float64 `json:"presence_penalty"`
FrequencyPenalty *float64 `json:"frequency_penalty"`
ActiveTools []string `json:"active_tools"`
ToolChoice *ToolChoice `json:"tool_choice"`
Headers map[string]string
ProviderOptions ProviderOptions
OnRetry OnRetryCallback
OnAuthRefresh OnAuthRefreshFunc
MaxRetries *int
// ModelProvider, when non-nil, is called on each retry attempt to
// obtain the language model. This allows callers to swap in a
// refreshed model after OnAuthRefresh rebuilds credentials. When
// nil, the model captured at step preparation time is used.
ModelProvider func() LanguageModel
StopWhen []StopCondition
PrepareStep PrepareStepFunction
RepairToolCall RepairToolCallFunction
// Agent-level callbacks
OnAgentStart OnAgentStartFunc // Called when agent starts
OnAgentFinish OnAgentFinishFunc // Called when agent finishes
OnStepStart OnStepStartFunc // Called when a step starts
OnStepFinish OnStepFinishFunc // Called when a step finishes
OnFinish OnFinishFunc // Called when entire agent completes
OnError OnErrorFunc // Called when an error occurs
// Stream part callbacks - called for each corresponding stream part type
OnChunk OnChunkFunc // Called for each stream part (catch-all)
OnWarnings OnWarningsFunc // Called for warnings
OnTextStart OnTextStartFunc // Called when text starts
OnTextDelta OnTextDeltaFunc // Called for text deltas
OnTextEnd OnTextEndFunc // Called when text ends
OnReasoningStart OnReasoningStartFunc // Called when reasoning starts
OnReasoningDelta OnReasoningDeltaFunc // Called for reasoning deltas
OnReasoningEnd OnReasoningEndFunc // Called when reasoning ends
OnToolInputStart OnToolInputStartFunc // Called when tool input starts
OnToolInputDelta OnToolInputDeltaFunc // Called for tool input deltas
OnToolInputEnd OnToolInputEndFunc // Called when tool input ends
OnToolCall OnToolCallFunc // Called when tool call is complete
OnToolResult OnToolResultFunc // Called when tool execution completes
OnSource OnSourceFunc // Called for source references
OnStreamFinish OnStreamFinishFunc // Called when stream finishes
}
AgentStreamCall represents a streaming call to an agent.
type AgentTool ¶
type AgentTool interface {
Info() ToolInfo
Run(ctx context.Context, params ToolCall) (ToolResponse, error)
ProviderOptions() ProviderOptions
SetProviderOptions(opts ProviderOptions)
}
AgentTool represents a tool that can be called by a language model. This matches the existing BaseTool interface pattern.
func NewAgentTool ¶
func NewAgentTool[TInput any]( name string, description string, fn func(ctx context.Context, input TInput, call ToolCall) (ToolResponse, error), ) AgentTool
NewAgentTool creates a typed tool from a function with automatic schema generation. This is the recommended way to create tools.
func NewParallelAgentTool ¶ added in v0.4.0
func NewParallelAgentTool[TInput any]( name string, description string, fn func(ctx context.Context, input TInput, call ToolCall) (ToolResponse, error), ) AgentTool
NewParallelAgentTool creates a typed tool from a function with automatic schema generation. This also marks a tool as safe to run in parallel with other tools.
type Call ¶
type Call struct {
Prompt Prompt `json:"prompt"`
MaxOutputTokens *int64 `json:"max_output_tokens"`
Temperature *float64 `json:"temperature"`
TopP *float64 `json:"top_p"`
TopK *int64 `json:"top_k"`
PresencePenalty *float64 `json:"presence_penalty"`
FrequencyPenalty *float64 `json:"frequency_penalty"`
Tools []Tool `json:"tools"`
ToolChoice *ToolChoice `json:"tool_choice"`
// UserAgent overrides the provider-level User-Agent header for this call.
UserAgent string `json:"-"`
// Headers overrides matching provider-level headers for this call.
Headers map[string]string `json:"-"`
// for provider specific options, the key is the provider id
ProviderOptions ProviderOptions `json:"provider_options"`
}
Call represents a call to a language model.
func (*Call) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler for Call.
type CallWarning ¶
type CallWarning struct {
Type CallWarningType `json:"type"`
Setting string `json:"setting"`
Tool Tool `json:"tool"`
Details string `json:"details"`
Message string `json:"message"`
}
CallWarning represents a warning from the model provider for this call. The call will proceed, but e.g. some settings might not be supported, which can lead to suboptimal results.
type CallWarningType ¶
type CallWarningType string
CallWarningType represents the type of call warning.
const ( // CallWarningTypeUnsupportedSetting indicates an unsupported setting. CallWarningTypeUnsupportedSetting CallWarningType = "unsupported-setting" // CallWarningTypeUnsupportedTool indicates an unsupported tool. CallWarningTypeUnsupportedTool CallWarningType = "unsupported-tool" // CallWarningTypeOther indicates other warnings. CallWarningTypeOther CallWarningType = "other" )
type Content ¶
type Content interface {
GetType() ContentType
}
Content represents generated content from the model.
func UnmarshalContent ¶ added in v0.3.0
UnmarshalContent unmarshals JSON into the appropriate Content type.
type ContentType ¶
type ContentType string
ContentType represents the type of content.
const ( // ContentTypeText represents text content. ContentTypeText ContentType = "text" // ContentTypeReasoning represents reasoning content. ContentTypeReasoning ContentType = "reasoning" // ContentTypeFile represents file content. ContentTypeFile ContentType = "file" // ContentTypeSource represents source content. ContentTypeSource ContentType = "source" // ContentTypeToolCall represents a tool call. ContentTypeToolCall ContentType = "tool-call" // ContentTypeToolResult represents a tool result. ContentTypeToolResult ContentType = "tool-result" )
type ExecutableProviderTool ¶ added in v0.17.0
type ExecutableProviderTool struct {
// contains filtered or unexported fields
}
ExecutableProviderTool pairs a ProviderDefinedTool with a client-side execution function. Use this for provider-defined tools that require local execution (e.g. Anthropic computer use). Register it via WithProviderDefinedTools.
func NewExecutableProviderTool ¶ added in v0.17.0
func NewExecutableProviderTool( pdt ProviderDefinedTool, run func(ctx context.Context, call ToolCall) (ToolResponse, error), ) ExecutableProviderTool
NewExecutableProviderTool creates a provider-defined tool with client-side execution. The tool is sent to the API using the provider's native wire format, but executed locally by run.
func (ExecutableProviderTool) Definition ¶ added in v0.17.0
func (e ExecutableProviderTool) Definition() ProviderDefinedTool
Definition returns the underlying ProviderDefinedTool.
func (ExecutableProviderTool) GetName ¶ added in v0.17.0
func (e ExecutableProviderTool) GetName() string
GetName returns the name of the underlying ProviderDefinedTool.
func (ExecutableProviderTool) GetType ¶ added in v0.17.0
func (e ExecutableProviderTool) GetType() ToolType
GetType returns the type of the underlying ProviderDefinedTool.
func (ExecutableProviderTool) Run ¶ added in v0.17.0
func (e ExecutableProviderTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
Run executes the tool's client-side function.
type FileContent ¶
type FileContent struct {
// The IANA media type of the file, e.g. `image/png` or `audio/mp3`.
// @see https://www.iana.org/assignments/media-types/media-types.xhtml
MediaType string `json:"media_type"`
// Generated file data as binary data.
Data []byte `json:"data"`
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
FileContent represents a file that has been generated by the model. Generated files as base64 encoded strings or binary data. The files should be returned without any unnecessary conversion.
func (FileContent) GetType ¶
func (f FileContent) GetType() ContentType
GetType returns the type of the file content.
func (FileContent) MarshalJSON ¶ added in v0.3.0
func (f FileContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for FileContent.
func (*FileContent) UnmarshalJSON ¶ added in v0.3.0
func (f *FileContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for FileContent.
type FilePart ¶
type FilePart struct {
Filename string `json:"filename"`
Data []byte `json:"data"`
MediaType string `json:"media_type"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
FilePart represents file content in a message.
func (FilePart) GetType ¶
func (f FilePart) GetType() ContentType
GetType returns the type of the file part.
func (FilePart) MarshalJSON ¶ added in v0.3.0
MarshalJSON implements json.Marshaler for FilePart.
func (FilePart) Options ¶
func (f FilePart) Options() ProviderOptions
Options returns the provider options for the file part.
func (*FilePart) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler for FilePart.
type FinishReason ¶
type FinishReason string
FinishReason represents why a language model finished generating a response.
Can be one of the following: - `stop`: model generated stop sequence - `length`: model generated maximum number of tokens - `content-filter`: content filter violation stopped the model - `tool-calls`: model triggered tool calls - `error`: model stopped because of an error - `other`: model stopped for other reasons - `unknown`: the model has not transmitted a finish reason.
const ( // FinishReasonStop indicates the model generated a stop sequence. FinishReasonStop FinishReason = "stop" // model generated stop sequence // FinishReasonLength indicates the model generated maximum number of tokens. FinishReasonLength FinishReason = "length" // model generated maximum number of tokens // FinishReasonContentFilter indicates content filter violation stopped the model. FinishReasonContentFilter FinishReason = "content-filter" // content filter violation stopped the model // FinishReasonToolCalls indicates the model triggered tool calls. FinishReasonToolCalls FinishReason = "tool-calls" // model triggered tool calls // FinishReasonError indicates the model stopped because of an error. FinishReasonError FinishReason = "error" // model stopped because of an error // FinishReasonOther indicates the model stopped for other reasons. FinishReasonOther FinishReason = "other" // model stopped for other reasons // FinishReasonUnknown indicates the model has not transmitted a finish reason. FinishReasonUnknown FinishReason = "unknown" // the model has not transmitted a finish reason )
type FunctionTool ¶
type FunctionTool struct {
// Name of the tool. Unique within this model call.
Name string `json:"name"`
// Description of the tool. The language model uses this to understand the
// tool's purpose and to provide better completion suggestions.
Description string `json:"description"`
// InputSchema - the parameters that the tool expects. The language model uses this to
// understand the tool's input requirements and to provide matching suggestions.
InputSchema map[string]any `json:"input_schema"` // JSON Schema
// ProviderOptions are provider-specific options for the tool.
ProviderOptions ProviderOptions `json:"provider_options"`
}
FunctionTool represents a function tool.
A tool has a name, a description, and a set of parameters.
func (FunctionTool) GetName ¶
func (f FunctionTool) GetName() string
GetName returns the name of the function tool.
func (FunctionTool) GetType ¶
func (f FunctionTool) GetType() ToolType
GetType returns the type of the function tool.
func (FunctionTool) MarshalJSON ¶ added in v0.3.0
func (f FunctionTool) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for FunctionTool.
func (*FunctionTool) UnmarshalJSON ¶ added in v0.3.0
func (f *FunctionTool) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for FunctionTool.
type LanguageModel ¶
type LanguageModel interface {
Generate(context.Context, Call) (*Response, error)
Stream(context.Context, Call) (StreamResponse, error)
GenerateObject(context.Context, ObjectCall) (*ObjectResponse, error)
StreamObject(context.Context, ObjectCall) (ObjectStreamResponse, error)
Provider() string
Model() string
}
LanguageModel represents a language model that can generate responses and stream responses.
type Message ¶
type Message struct {
Role MessageRole `json:"role"`
Content []MessagePart `json:"content"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
Message represents a message in a prompt.
func NewSystemMessage ¶
NewSystemMessage creates a new system message with the given prompts.
func NewUserMessage ¶
NewUserMessage creates a new user message with the given prompt and optional files.
func (*Message) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler for Message.
type MessagePart ¶
type MessagePart interface {
GetType() ContentType
Options() ProviderOptions
}
MessagePart represents a part of a message content.
func UnmarshalMessagePart ¶ added in v0.3.0
func UnmarshalMessagePart(data []byte) (MessagePart, error)
UnmarshalMessagePart unmarshals JSON into the appropriate MessagePart type.
type MessageRole ¶
type MessageRole string
MessageRole represents the role of a message.
const ( // MessageRoleSystem represents a system message. MessageRoleSystem MessageRole = "system" // MessageRoleUser represents a user message. MessageRoleUser MessageRole = "user" // MessageRoleAssistant represents an assistant message. MessageRoleAssistant MessageRole = "assistant" // MessageRoleTool represents a tool message. MessageRoleTool MessageRole = "tool" )
type NoObjectGeneratedError ¶ added in v0.3.0
type NoObjectGeneratedError struct {
RawText string
ParseError error
ValidationError error
Usage Usage
FinishReason FinishReason
}
NoObjectGeneratedError is returned when object generation fails due to parsing errors, validation errors, or model failures.
func (*NoObjectGeneratedError) Error ¶ added in v0.3.0
func (e *NoObjectGeneratedError) Error() string
Error implements the error interface.
type ObjectCall ¶ added in v0.3.0
type ObjectCall struct {
Prompt Prompt
Schema Schema
SchemaName string
SchemaDescription string
MaxOutputTokens *int64
Temperature *float64
TopP *float64
TopK *int64
PresencePenalty *float64
FrequencyPenalty *float64
// UserAgent overrides the provider-level User-Agent header for this call.
UserAgent string `json:"-"`
// Headers overrides matching provider-level headers for this call.
Headers map[string]string `json:"-"`
ProviderOptions ProviderOptions
RepairText schema.ObjectRepairFunc
}
ObjectCall represents a request to generate a structured object.
type ObjectMode ¶ added in v0.3.0
type ObjectMode string
ObjectMode specifies how structured output should be generated.
const ( // ObjectModeAuto lets the provider choose the best approach. ObjectModeAuto ObjectMode = "auto" // ObjectModeJSON forces the use of native JSON mode (if supported). ObjectModeJSON ObjectMode = "json" // ObjectModeTool forces the use of tool-based approach. ObjectModeTool ObjectMode = "tool" // ObjectModeText uses text generation with schema in prompt (fallback for models without tool/JSON support). ObjectModeText ObjectMode = "text" )
type ObjectResponse ¶ added in v0.3.0
type ObjectResponse struct {
Object any
RawText string
Usage Usage
FinishReason FinishReason
Warnings []CallWarning
ProviderMetadata ProviderMetadata
}
ObjectResponse represents the response from a structured object generation.
type ObjectResult ¶ added in v0.3.0
type ObjectResult[T any] struct { Object T RawText string Usage Usage FinishReason FinishReason Warnings []CallWarning ProviderMetadata ProviderMetadata }
ObjectResult is a typed result wrapper returned by GenerateObject[T].
type ObjectStreamPart ¶ added in v0.3.0
type ObjectStreamPart struct {
Type ObjectStreamPartType
Object any
Delta string
Error error
Usage Usage
FinishReason FinishReason
Warnings []CallWarning
ProviderMetadata ProviderMetadata
}
ObjectStreamPart represents a single chunk in the object stream.
type ObjectStreamPartType ¶ added in v0.3.0
type ObjectStreamPartType string
ObjectStreamPartType indicates the type of stream part.
const ( // ObjectStreamPartTypeObject is emitted when a new partial object is available. ObjectStreamPartTypeObject ObjectStreamPartType = "object" // ObjectStreamPartTypeTextDelta is emitted for text deltas (if model generates text). ObjectStreamPartTypeTextDelta ObjectStreamPartType = "text-delta" // ObjectStreamPartTypeError is emitted when an error occurs. ObjectStreamPartTypeError ObjectStreamPartType = "error" // ObjectStreamPartTypeFinish is emitted when streaming completes. ObjectStreamPartTypeFinish ObjectStreamPartType = "finish" )
type ObjectStreamResponse ¶ added in v0.3.0
type ObjectStreamResponse = iter.Seq[ObjectStreamPart]
ObjectStreamResponse is an iterator over ObjectStreamPart.
type OnAgentFinishFunc ¶
type OnAgentFinishFunc func(result *AgentResult) error
OnAgentFinishFunc is called when agent finishes.
type OnAuthRefreshFunc ¶ added in v0.37.0
type OnAuthRefreshFunc func(ctx context.Context, err *ProviderError) error
OnAuthRefreshFunc is called when a stream fails with an authentication error that the caller may be able to resolve (e.g. an expired SSO session or OAuth token). The function should perform whatever credential refresh is needed and return nil on success, in which case fantasy retries the operation transparently. Returning an error surfaces the original auth error to the caller without retry. Pair this with ModelProvider to supply a rebuilt model carrying the refreshed credentials on the retry attempt.
type OnChunkFunc ¶
type OnChunkFunc func(StreamPart) error
OnChunkFunc is called for each stream part (catch-all).
type OnFinishFunc ¶
type OnFinishFunc func(result *AgentResult)
OnFinishFunc is called when entire agent completes.
type OnReasoningDeltaFunc ¶
OnReasoningDeltaFunc is called for reasoning deltas.
type OnReasoningEndFunc ¶
type OnReasoningEndFunc func(id string, reasoning ReasoningContent) error
OnReasoningEndFunc is called when reasoning ends.
type OnReasoningStartFunc ¶
type OnReasoningStartFunc func(id string, reasoning ReasoningContent) error
OnReasoningStartFunc is called when reasoning starts.
type OnRetryCallback ¶
type OnRetryCallback = func(err *ProviderError, delay time.Duration)
OnRetryCallback is called before each retry attempt, after the retry delay is chosen but before it elapses. err is the failure that triggered the retry (nil if the failure was not a *ProviderError) and delay is how long the middleware will wait before the next attempt.
A retry re-runs the entire step from scratch: the stream is recreated and the stream callbacks (OnTextStart, OnTextDelta, OnReasoningStart, OnReasoningDelta, OnToolInputStart, etc.) fire again from the beginning of the new response. Consumers that accumulate streamed content must reset that accumulated state here, otherwise the retried response is appended to the partial content from the failed attempt.
type OnSourceFunc ¶
type OnSourceFunc func(source SourceContent) error
OnSourceFunc is called for source references.
type OnStepFinishFunc ¶
type OnStepFinishFunc func(stepResult StepResult) error
OnStepFinishFunc is called when a step finishes.
type OnStepFinishedFunction ¶
type OnStepFinishedFunction = func(step StepResult)
OnStepFinishedFunction defines a function that is called when a step finishes.
type OnStepStartFunc ¶
OnStepStartFunc is called when a step starts.
type OnStreamFinishFunc ¶
type OnStreamFinishFunc func(usage Usage, finishReason FinishReason, providerMetadata ProviderMetadata) error
OnStreamFinishFunc is called when stream finishes.
type OnTextDeltaFunc ¶
OnTextDeltaFunc is called for text deltas.
type OnTextEndFunc ¶
OnTextEndFunc is called when text ends.
type OnTextStartFunc ¶
OnTextStartFunc is called when text starts.
type OnToolCallFunc ¶
type OnToolCallFunc func(toolCall ToolCallContent) error
OnToolCallFunc is called when tool call is complete.
type OnToolInputDeltaFunc ¶
OnToolInputDeltaFunc is called for tool input deltas.
type OnToolInputEndFunc ¶
OnToolInputEndFunc is called when tool input ends.
type OnToolInputStartFunc ¶
OnToolInputStartFunc is called when tool input starts.
type OnToolResultFunc ¶
type OnToolResultFunc func(result ToolResultContent) error
OnToolResultFunc is called when tool execution completes.
type OnWarningsFunc ¶
type OnWarningsFunc func(warnings []CallWarning) error
OnWarningsFunc is called for warnings.
type PrepareStepFunction ¶
type PrepareStepFunction = func(ctx context.Context, options PrepareStepFunctionOptions) (context.Context, PrepareStepResult, error)
PrepareStepFunction defines a function that prepares a step in an agent execution.
type PrepareStepFunctionOptions ¶
type PrepareStepFunctionOptions struct {
Steps []StepResult
StepNumber int
Model LanguageModel
Messages []Message
}
PrepareStepFunctionOptions contains the options for preparing a step in an agent execution.
type PrepareStepResult ¶
type PrepareStepResult struct {
Model LanguageModel
Messages []Message
System *string
ToolChoice *ToolChoice
ActiveTools []string
DisableAllTools bool
Tools []AgentTool
}
PrepareStepResult contains the result of preparing a step in an agent execution.
type Provider ¶
type Provider interface {
Name() string
LanguageModel(ctx context.Context, modelID string) (LanguageModel, error)
}
Provider represents a provider of language models.
type ProviderDefinedTool ¶
type ProviderDefinedTool struct {
// ID of the tool. Should follow the format `<provider-name>.<unique-tool-name>`.
ID string `json:"id"`
// Name of the tool that the user must use in the tool set.
Name string `json:"name"`
// Args for configuring the tool. Must match the expected arguments defined by the provider for this tool.
Args map[string]any `json:"args"`
}
ProviderDefinedTool represents the configuration of a tool that is defined by the provider.
func (ProviderDefinedTool) GetName ¶
func (p ProviderDefinedTool) GetName() string
GetName returns the name of the provider-defined tool.
func (ProviderDefinedTool) GetType ¶
func (p ProviderDefinedTool) GetType() ToolType
GetType returns the type of the provider-defined tool.
func (ProviderDefinedTool) MarshalJSON ¶ added in v0.3.0
func (p ProviderDefinedTool) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ProviderDefinedTool.
func (*ProviderDefinedTool) UnmarshalJSON ¶ added in v0.3.0
func (p *ProviderDefinedTool) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ProviderDefinedTool.
type ProviderError ¶ added in v0.2.0
type ProviderError struct {
Message string
Title string
Cause error
URL string
StatusCode int
RequestBody []byte
ResponseHeaders map[string]string
ResponseBody []byte
ContextUsedTokens int
ContextMaxTokens int
ContextTooLargeErr bool
// AuthError marks the error as an authentication failure a provider
// flagged as resolvable by refreshing credentials (e.g. re-running an
// interactive login). It covers auth failures that do not carry an HTTP
// 401 status, so a caller-supplied OnAuthRefresh hook still engages.
AuthError bool
}
ProviderError represents an error returned by an external provider.
func NewIncompleteStreamError ¶ added in v0.24.0
func NewIncompleteStreamError() *ProviderError
NewIncompleteStreamError returns a retryable ProviderError indicating that an upstream stream closed cleanly without delivering its terminal signal (finish_reason, stop_reason, response.completed, candidate.finishReason, etc.). The cause is io.ErrUnexpectedEOF so ProviderError.IsRetryable() engages and the retry middleware re-runs the step.
func NewTransportError ¶ added in v0.38.0
func NewTransportError(err error) *ProviderError
NewTransportError wraps a transient transport error into a retryable ProviderError with a human-friendly title and message.
func (*ProviderError) Error ¶ added in v0.2.0
func (m *ProviderError) Error() string
func (*ProviderError) IsContextTooLarge ¶ added in v0.10.0
func (m *ProviderError) IsContextTooLarge() bool
IsContextTooLarge checks if the error is due to the context exceeding the model's limit.
func (*ProviderError) IsRetryable ¶ added in v0.2.0
func (m *ProviderError) IsRetryable() bool
IsRetryable reports whether the error should be retried. It returns true if the underlying cause is io.ErrUnexpectedEOF, if the "x-should-retry" response header evaluates to true, if the HTTP status code indicates a retryable condition (408, 409, 429, or any 5xx), or if the cause is a transient HTTP/2 transport error.
func (*ProviderError) Unwrap ¶ added in v0.38.0
func (m *ProviderError) Unwrap() error
Unwrap returns the underlying cause so errors.Is and errors.As can inspect the wrapped error (e.g. an HTTP/2 transport error).
type ProviderMetadata ¶
type ProviderMetadata map[string]ProviderOptionsData
ProviderMetadata represents additional provider-specific metadata. They are passed through from the provider to the AI SDK and enable provider-specific results that can be fully encapsulated in the provider.
The outer map is keyed by the provider name, and the inner map is keyed by the provider-specific metadata key.
Example:
{
"anthropic": {
"signature": "sig....."
}
}
func UnmarshalProviderMetadata ¶ added in v0.3.0
func UnmarshalProviderMetadata(data map[string]json.RawMessage) (ProviderMetadata, error)
UnmarshalProviderMetadata unmarshals a map of provider metadata by type.
type ProviderOptions ¶
type ProviderOptions map[string]ProviderOptionsData
ProviderOptions represents additional provider-specific options. Options are additional input to the provider. They are passed through to the provider from the AI SDK and enable provider-specific functionality that can be fully encapsulated in the provider.
This enables us to quickly ship provider-specific functionality without affecting the core AI SDK.
The outer map is keyed by the provider name, and the inner map is keyed by the provider-specific option key.
Example:
{
"anthropic": {
"cacheControl": { "type": "ephemeral" }
}
}
func UnmarshalProviderOptions ¶ added in v0.3.0
func UnmarshalProviderOptions(data map[string]json.RawMessage) (ProviderOptions, error)
UnmarshalProviderOptions unmarshals a map of provider options by type.
type ProviderOptionsData ¶
type ProviderOptionsData interface {
// Options is a marker method that identifies types implementing this interface.
Options()
json.Marshaler
json.Unmarshaler
}
ProviderOptionsData is an interface for provider-specific options data. All implementations MUST also implement encoding/json.Marshaler and encoding/json.Unmarshaler interfaces to ensure proper JSON serialization with the provider registry system.
Recommended implementation pattern using generic helpers:
// Define type constants at the top of your file
const TypeMyProviderOptions = "myprovider.options"
type MyProviderOptions struct {
Field string `json:"field"`
}
// Register the type in init() - place at top of file after constants
func init() {
fantasy.RegisterProviderType(TypeMyProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
var opts MyProviderOptions
if err := json.Unmarshal(data, &opts); err != nil {
return nil, err
}
return &opts, nil
})
}
// Implement ProviderOptionsData interface
func (*MyProviderOptions) Options() {}
// Implement json.Marshaler using the generic helper
func (m MyProviderOptions) MarshalJSON() ([]byte, error) {
type plain MyProviderOptions
return fantasy.MarshalProviderType(TypeMyProviderOptions, plain(m))
}
// Implement json.Unmarshaler using the generic helper
// Note: Receives inner data after type routing by the registry.
func (m *MyProviderOptions) UnmarshalJSON(data []byte) error {
type plain MyProviderOptions
var p plain
if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
return err
}
*m = MyProviderOptions(p)
return nil
}
type ProviderTool ¶ added in v0.17.0
type ProviderTool interface {
// contains filtered or unexported methods
}
ProviderTool is a tool whose schema and wire format are defined by the model provider. Both pure provider-executed tools (ProviderDefinedTool) and client-executed provider tools (ExecutableProviderTool) implement this interface. The unexported method seals this interface to the types in this package. External packages should use NewExecutableProviderTool instead.
type ReasoningContent ¶
type ReasoningContent struct {
Text string `json:"text"`
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
ReasoningContent represents reasoning that the model has generated.
func (ReasoningContent) GetType ¶
func (r ReasoningContent) GetType() ContentType
GetType returns the type of the reasoning content.
func (ReasoningContent) MarshalJSON ¶ added in v0.3.0
func (r ReasoningContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ReasoningContent.
func (*ReasoningContent) UnmarshalJSON ¶ added in v0.3.0
func (r *ReasoningContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ReasoningContent.
type ReasoningPart ¶
type ReasoningPart struct {
Text string `json:"text"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
ReasoningPart represents reasoning content in a message.
func (ReasoningPart) GetType ¶
func (r ReasoningPart) GetType() ContentType
GetType returns the type of the reasoning part.
func (ReasoningPart) MarshalJSON ¶ added in v0.3.0
func (r ReasoningPart) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ReasoningPart.
func (ReasoningPart) Options ¶
func (r ReasoningPart) Options() ProviderOptions
Options returns the provider options for the reasoning part.
func (*ReasoningPart) UnmarshalJSON ¶ added in v0.3.0
func (r *ReasoningPart) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ReasoningPart.
type RepairToolCallFunction ¶
type RepairToolCallFunction = func(ctx context.Context, options ToolCallRepairOptions) (*ToolCallContent, error)
RepairToolCallFunction defines a function that repairs a tool call.
type Response ¶
type Response struct {
Content ResponseContent `json:"content"`
FinishReason FinishReason `json:"finish_reason"`
Usage Usage `json:"usage"`
Warnings []CallWarning `json:"warnings"`
// for provider specific response metadata, the key is the provider id
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
Response represents a response from a language model.
func (*Response) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler for Response.
type ResponseContent ¶
type ResponseContent []Content
ResponseContent represents the content of a model response.
func (ResponseContent) Files ¶
func (r ResponseContent) Files() []FileContent
Files returns all file content parts.
func (ResponseContent) Reasoning ¶
func (r ResponseContent) Reasoning() []ReasoningContent
Reasoning returns all reasoning content parts.
func (ResponseContent) ReasoningText ¶
func (r ResponseContent) ReasoningText() string
ReasoningText returns all reasoning content as a concatenated string.
func (ResponseContent) Sources ¶
func (r ResponseContent) Sources() []SourceContent
Sources returns all source content parts.
func (ResponseContent) Text ¶
func (r ResponseContent) Text() string
Text returns the first text content of the response.
func (ResponseContent) ToolCalls ¶
func (r ResponseContent) ToolCalls() []ToolCallContent
ToolCalls returns all tool call content parts.
func (ResponseContent) ToolResults ¶
func (r ResponseContent) ToolResults() []ToolResultContent
ToolResults returns all tool result content parts.
type RetryError ¶
type RetryError struct {
Errors []error
}
RetryError represents an error that occurred during retry operations.
func (*RetryError) Error ¶ added in v0.2.0
func (e *RetryError) Error() string
func (RetryError) Unwrap ¶ added in v0.2.0
func (e RetryError) Unwrap() error
type RetryFunction ¶
RetryFunction is a function that retries another function.
func RetryWithExponentialBackoffRespectingRetryHeaders ¶
func RetryWithExponentialBackoffRespectingRetryHeaders[T any](options RetryOptions) RetryFunction[T]
RetryWithExponentialBackoffRespectingRetryHeaders creates a retry function that retries a failed operation with exponential backoff, while respecting rate limit headers (retry-after-ms and retry-after) if they are provided and reasonable (0-60 seconds).
When OnAuthRefresh is set and the operation ends in an authentication error, the hook is given one chance to refresh credentials. On success the entire retry pass runs again with a fresh budget; on failure the original auth error is returned. At most one refresh is attempted, so a credential that stays invalid cannot spin.
type RetryOptions ¶
type RetryOptions struct {
MaxRetries int
InitialDelayIn time.Duration
BackoffFactor float64
OnRetry OnRetryCallback
// OnAuthRefresh is called when an operation fails with an authentication
// error the caller may be able to resolve (e.g. an expired SSO session).
// If it returns nil, the entire retry pass restarts with a fresh retry
// budget; if it returns an error, the original auth error is returned
// without retry. At most one refresh is attempted, since auth refresh is
// a one-shot human-in-the-loop step and a second attempt would not fare
// better.
OnAuthRefresh OnAuthRefreshFunc
}
RetryOptions configures the retry behavior.
func DefaultRetryOptions ¶
func DefaultRetryOptions() RetryOptions
DefaultRetryOptions returns the default retry options.
type SourceContent ¶
type SourceContent struct {
SourceType SourceType `json:"source_type"` // "url" or "document"
ID string `json:"id"`
URL string `json:"url"` // for URL sources
Title string `json:"title"`
MediaType string `json:"media_type"` // for document sources (IANA media type)
Filename string `json:"filename"` // for document sources
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
SourceContent represents a source that has been used as input to generate the response.
func (SourceContent) GetType ¶
func (s SourceContent) GetType() ContentType
GetType returns the type of the source content.
func (SourceContent) MarshalJSON ¶ added in v0.3.0
func (s SourceContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for SourceContent.
func (*SourceContent) UnmarshalJSON ¶ added in v0.3.0
func (s *SourceContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for SourceContent.
type SourceType ¶
type SourceType string
SourceType represents the type of source.
const ( // SourceTypeURL represents a URL source. SourceTypeURL SourceType = "url" // SourceTypeDocument represents a document source. SourceTypeDocument SourceType = "document" )
type StepResult ¶
StepResult represents the result of a single step in an agent execution.
type StopCondition ¶
type StopCondition = func(steps []StepResult) bool
StopCondition defines a function that determines when an agent should stop executing.
func FinishReasonIs ¶
func FinishReasonIs(reason FinishReason) StopCondition
FinishReasonIs returns a stop condition that stops when the specified finish reason occurs.
func HasContent ¶
func HasContent(contentType ContentType) StopCondition
HasContent returns a stop condition that stops when the specified content type appears in the last step.
func HasToolCall ¶
func HasToolCall(toolName string) StopCondition
HasToolCall returns a stop condition that stops when the specified tool is called in the last step.
func MaxTokensUsed ¶
func MaxTokensUsed(maxTokens int64) StopCondition
MaxTokensUsed returns a stop condition that stops when total token usage exceeds the specified limit.
func StepCountIs ¶
func StepCountIs(stepCount int) StopCondition
StepCountIs returns a stop condition that stops after the specified number of steps.
type StreamObjectResult ¶ added in v0.3.0
type StreamObjectResult[T any] struct { // contains filtered or unexported fields }
StreamObjectResult provides typed access to a streaming object generation result.
func NewStreamObjectResult ¶ added in v0.3.0
func NewStreamObjectResult[T any](ctx context.Context, stream ObjectStreamResponse) *StreamObjectResult[T]
NewStreamObjectResult creates a typed stream result from an untyped stream.
func (*StreamObjectResult[T]) FullStream ¶ added in v0.3.0
func (s *StreamObjectResult[T]) FullStream() iter.Seq[ObjectStreamPart]
FullStream returns an iterator that yields all stream parts including errors and metadata.
func (*StreamObjectResult[T]) Object ¶ added in v0.3.0
func (s *StreamObjectResult[T]) Object() (*ObjectResult[T], error)
Object waits for the stream to complete and returns the final object. Returns an error if streaming fails or no valid object was generated.
func (*StreamObjectResult[T]) PartialObjectStream ¶ added in v0.3.0
func (s *StreamObjectResult[T]) PartialObjectStream() iter.Seq[T]
PartialObjectStream returns an iterator that yields progressively more complete objects. Only emits when the object actually changes (deduplication).
func (*StreamObjectResult[T]) TextStream ¶ added in v0.3.0
func (s *StreamObjectResult[T]) TextStream() iter.Seq[string]
TextStream returns an iterator that yields text deltas. Useful if the model generates explanatory text alongside the object.
type StreamPart ¶
type StreamPart struct {
Type StreamPartType `json:"type"`
ID string `json:"id"`
ToolCallName string `json:"tool_call_name"`
ToolCallInput string `json:"tool_call_input"`
Delta string `json:"delta"`
ProviderExecuted bool `json:"provider_executed"`
Usage Usage `json:"usage"`
FinishReason FinishReason `json:"finish_reason"`
Error error `json:"error"`
Warnings []CallWarning `json:"warnings"`
// Source-related fields
SourceType SourceType `json:"source_type"`
URL string `json:"url"`
Title string `json:"title"`
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
StreamPart represents a part of a streaming response.
func (StreamPart) MarshalJSON ¶ added in v0.3.2
func (s StreamPart) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for StreamPart.
func (*StreamPart) UnmarshalJSON ¶ added in v0.3.0
func (s *StreamPart) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for StreamPart.
type StreamPartType ¶
type StreamPartType string
StreamPartType represents the type of a stream part.
const ( // StreamPartTypeWarnings represents warnings stream part type. StreamPartTypeWarnings StreamPartType = "warnings" // StreamPartTypeTextStart represents text start stream part type. StreamPartTypeTextStart StreamPartType = "text_start" // StreamPartTypeTextDelta represents text delta stream part type. StreamPartTypeTextDelta StreamPartType = "text_delta" // StreamPartTypeTextEnd represents text end stream part type. StreamPartTypeTextEnd StreamPartType = "text_end" // StreamPartTypeReasoningStart represents reasoning start stream part type. StreamPartTypeReasoningStart StreamPartType = "reasoning_start" // StreamPartTypeReasoningDelta represents reasoning delta stream part type. StreamPartTypeReasoningDelta StreamPartType = "reasoning_delta" // StreamPartTypeReasoningEnd represents reasoning end stream part type. StreamPartTypeReasoningEnd StreamPartType = "reasoning_end" // StreamPartTypeToolInputStart represents tool input start stream part type. StreamPartTypeToolInputStart StreamPartType = "tool_input_start" // StreamPartTypeToolInputDelta represents tool input delta stream part type. StreamPartTypeToolInputDelta StreamPartType = "tool_input_delta" // StreamPartTypeToolInputEnd represents tool input end stream part type. StreamPartTypeToolInputEnd StreamPartType = "tool_input_end" // StreamPartTypeToolCall represents tool call stream part type. StreamPartTypeToolCall StreamPartType = "tool_call" // StreamPartTypeToolResult represents tool result stream part type. StreamPartTypeToolResult StreamPartType = "tool_result" // StreamPartTypeSource represents source stream part type. StreamPartTypeSource StreamPartType = "source" // StreamPartTypeFinish represents finish stream part type. StreamPartTypeFinish StreamPartType = "finish" // StreamPartTypeError represents error stream part type. StreamPartTypeError StreamPartType = "error" )
type StreamResponse ¶
type StreamResponse = iter.Seq[StreamPart]
StreamResponse represents a streaming response sequence.
type TextContent ¶
type TextContent struct {
// The text content.
Text string `json:"text"`
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
TextContent represents text that the model has generated.
func (TextContent) GetType ¶
func (t TextContent) GetType() ContentType
GetType returns the type of the text content.
func (TextContent) MarshalJSON ¶ added in v0.3.0
func (t TextContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for TextContent.
func (*TextContent) UnmarshalJSON ¶ added in v0.3.0
func (t *TextContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for TextContent.
type TextPart ¶
type TextPart struct {
Text string `json:"text"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
TextPart represents text content in a message.
func (TextPart) GetType ¶
func (t TextPart) GetType() ContentType
GetType returns the type of the text part.
func (TextPart) MarshalJSON ¶ added in v0.3.0
MarshalJSON implements json.Marshaler for TextPart.
func (TextPart) Options ¶
func (t TextPart) Options() ProviderOptions
Options returns the provider options for the text part.
func (*TextPart) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler for TextPart.
type Tool ¶
Tool represents a tool that can be used by the model.
Note: this is **not** the user-facing tool definition. The AI SDK methods will map the user-facing tool definitions to this format.
func UnmarshalTool ¶ added in v0.3.0
UnmarshalTool unmarshals JSON into the appropriate Tool type.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input"`
}
ToolCall represents a tool invocation, matching the existing pattern.
type ToolCallContent ¶
type ToolCallContent struct {
ToolCallID string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
// Stringified JSON object with the tool call arguments.
// Must match the parameters schema of the tool.
Input string `json:"input"`
// Whether the tool call will be executed by the provider.
// If this flag is not set or is false, the tool call will be executed by the client.
ProviderExecuted bool `json:"provider_executed"`
// Additional provider-specific metadata for the tool call.
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
// Whether this tool call is invalid (failed validation/parsing)
Invalid bool `json:"invalid,omitempty"`
// Error that occurred during validation/parsing (only set if Invalid is true)
ValidationError error `json:"validation_error,omitempty"`
}
ToolCallContent represents tool calls that the model has generated.
func (ToolCallContent) GetType ¶
func (t ToolCallContent) GetType() ContentType
GetType returns the type of the tool call content.
func (ToolCallContent) MarshalJSON ¶ added in v0.3.0
func (t ToolCallContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolCallContent.
func (*ToolCallContent) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolCallContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolCallContent.
type ToolCallPart ¶
type ToolCallPart struct {
ToolCallID string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
Input string `json:"input"` // the json string
ProviderExecuted bool `json:"provider_executed"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
ToolCallPart represents a tool call in a message.
func (ToolCallPart) GetType ¶
func (t ToolCallPart) GetType() ContentType
GetType returns the type of the tool call part.
func (ToolCallPart) MarshalJSON ¶ added in v0.3.0
func (t ToolCallPart) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolCallPart.
func (ToolCallPart) Options ¶
func (t ToolCallPart) Options() ProviderOptions
Options returns the provider options for the tool call part.
func (*ToolCallPart) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolCallPart) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolCallPart.
type ToolCallRepairOptions ¶
type ToolCallRepairOptions struct {
OriginalToolCall ToolCallContent
ValidationError error
AvailableTools []AgentTool
SystemPrompt string
Messages []Message
}
ToolCallRepairOptions contains the options for repairing a tool call.
type ToolChoice ¶
type ToolChoice string
ToolChoice represents the tool choice preference for a model call.
const ( // ToolChoiceNone indicates no tools should be used. ToolChoiceNone ToolChoice = "none" // ToolChoiceAuto indicates tools should be used automatically. ToolChoiceAuto ToolChoice = "auto" // ToolChoiceRequired indicates tools are required. ToolChoiceRequired ToolChoice = "required" )
func SpecificToolChoice ¶
func SpecificToolChoice(name string) ToolChoice
SpecificToolChoice creates a tool choice for a specific tool name.
type ToolInfo ¶
type ToolInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
Required []string `json:"required"`
Parallel bool `json:"parallel"` // Whether this tool can run in parallel with other tools
}
ToolInfo represents tool metadata, matching the existing pattern.
type ToolResponse ¶
type ToolResponse struct {
Type string `json:"type"`
Content string `json:"content"`
// Data contains binary data for image/media responses (e.g., image bytes, audio data).
Data []byte `json:"data,omitempty"`
// MediaType specifies the MIME type of the media (e.g., "image/png", "audio/wav").
MediaType string `json:"media_type,omitempty"`
Metadata string `json:"metadata,omitempty"`
IsError bool `json:"is_error"`
StopTurn bool `json:"stop_turn,omitempty"`
}
ToolResponse represents the response from a tool execution, matching the existing pattern.
func NewImageResponse ¶ added in v0.5.0
func NewImageResponse(data []byte, mediaType string) ToolResponse
NewImageResponse creates an image response with binary data.
func NewMediaResponse ¶ added in v0.5.0
func NewMediaResponse(data []byte, mediaType string) ToolResponse
NewMediaResponse creates a media response with binary data (e.g., audio, video).
func NewTextErrorResponse ¶
func NewTextErrorResponse(content string) ToolResponse
NewTextErrorResponse creates an error response.
func NewTextResponse ¶
func NewTextResponse(content string) ToolResponse
NewTextResponse creates a text response.
func WithResponseMetadata ¶
func WithResponseMetadata(response ToolResponse, metadata any) ToolResponse
WithResponseMetadata adds metadata to a response.
type ToolResultContent ¶
type ToolResultContent struct {
// The ID of the tool call that this result is associated with.
ToolCallID string `json:"tool_call_id"`
// Name of the tool that generated this result.
ToolName string `json:"tool_name"`
// Result of the tool call. This is a JSON-serializable object.
Result ToolResultOutputContent `json:"result"`
ClientMetadata string `json:"client_metadata"` // Metadata from the client that executed the tool
// Whether the tool result was generated by the provider.
// If this flag is set to true, the tool result was generated by the provider.
// If this flag is not set or is false, the tool result was generated by the client.
ProviderExecuted bool `json:"provider_executed"`
// Additional provider-specific metadata for the tool result.
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
// StopTurn indicates that the agent loop should stop after this result.
// The tool result is still delivered to the model's context, but the model
// does not get another chance to make tool calls in the same turn.
StopTurn bool `json:"stop_turn,omitempty"`
}
ToolResultContent represents result of a tool call that has been executed by the provider.
func (ToolResultContent) GetType ¶
func (t ToolResultContent) GetType() ContentType
GetType returns the type of the tool result content.
func (ToolResultContent) MarshalJSON ¶ added in v0.3.0
func (t ToolResultContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolResultContent.
func (*ToolResultContent) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolResultContent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolResultContent.
type ToolResultContentType ¶
type ToolResultContentType string
ToolResultContentType represents the type of tool result output.
const ( // ToolResultContentTypeText represents text output. ToolResultContentTypeText ToolResultContentType = "text" // ToolResultContentTypeError represents error text output. ToolResultContentTypeError ToolResultContentType = "error" // ToolResultContentTypeMedia represents content output. ToolResultContentTypeMedia ToolResultContentType = "media" )
type ToolResultOutputContent ¶
type ToolResultOutputContent interface {
GetType() ToolResultContentType
}
ToolResultOutputContent represents the output content of a tool result.
func UnmarshalToolResultOutputContent ¶ added in v0.3.0
func UnmarshalToolResultOutputContent(data []byte) (ToolResultOutputContent, error)
UnmarshalToolResultOutputContent unmarshals JSON into the appropriate ToolResultOutputContent type.
type ToolResultOutputContentError ¶
type ToolResultOutputContentError struct {
Error error `json:"error"`
}
ToolResultOutputContentError represents error output content of a tool result.
func (ToolResultOutputContentError) GetType ¶
func (t ToolResultOutputContentError) GetType() ToolResultContentType
GetType returns the type of the tool result output content error.
func (ToolResultOutputContentError) MarshalJSON ¶ added in v0.3.0
func (t ToolResultOutputContentError) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolResultOutputContentError.
func (*ToolResultOutputContentError) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolResultOutputContentError) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolResultOutputContentError.
type ToolResultOutputContentMedia ¶
type ToolResultOutputContentMedia struct {
Data string `json:"data"` // for media type (base64)
MediaType string `json:"media_type"` // for media type
Text string `json:"text,omitempty"` // optional text content accompanying the media
}
ToolResultOutputContentMedia represents media output content of a tool result.
func (ToolResultOutputContentMedia) GetType ¶
func (t ToolResultOutputContentMedia) GetType() ToolResultContentType
GetType returns the type of the tool result output content media.
func (ToolResultOutputContentMedia) MarshalJSON ¶ added in v0.3.0
func (t ToolResultOutputContentMedia) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolResultOutputContentMedia.
func (*ToolResultOutputContentMedia) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolResultOutputContentMedia) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolResultOutputContentMedia.
type ToolResultOutputContentText ¶
type ToolResultOutputContentText struct {
Text string `json:"text"`
}
ToolResultOutputContentText represents text output content of a tool result.
func (ToolResultOutputContentText) GetType ¶
func (t ToolResultOutputContentText) GetType() ToolResultContentType
GetType returns the type of the tool result output content text.
func (ToolResultOutputContentText) MarshalJSON ¶ added in v0.3.0
func (t ToolResultOutputContentText) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolResultOutputContentText.
func (*ToolResultOutputContentText) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolResultOutputContentText) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolResultOutputContentText.
type ToolResultPart ¶
type ToolResultPart struct {
ToolCallID string `json:"tool_call_id"`
Output ToolResultOutputContent `json:"output"`
ProviderExecuted bool `json:"provider_executed"`
ProviderOptions ProviderOptions `json:"provider_options"`
}
ToolResultPart represents a tool result in a message.
func (ToolResultPart) GetType ¶
func (t ToolResultPart) GetType() ContentType
GetType returns the type of the tool result part.
func (ToolResultPart) MarshalJSON ¶ added in v0.3.0
func (t ToolResultPart) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolResultPart.
func (ToolResultPart) Options ¶
func (t ToolResultPart) Options() ProviderOptions
Options returns the provider options for the tool result part.
func (*ToolResultPart) UnmarshalJSON ¶ added in v0.3.0
func (t *ToolResultPart) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for ToolResultPart.
type UnmarshalFunc ¶ added in v0.3.0
type UnmarshalFunc func([]byte) (ProviderOptionsData, error)
UnmarshalFunc converts raw JSON into a ProviderOptionsData implementation.
type Usage ¶
type Usage struct {
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
ReasoningTokens int64 `json:"reasoning_tokens"`
CacheCreationTokens int64 `json:"cache_creation_tokens"`
CacheReadTokens int64 `json:"cache_read_tokens"`
}
Usage represents token usage statistics for a model call.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
module
|
|
|
Package jsonrepair provides utilities to repair malformed JSON.
|
Package jsonrepair provides utilities to repair malformed JSON. |
|
Package object provides utilities for generating structured objects with automatic schema generation.
|
Package object provides utilities for generating structured objects with automatic schema generation. |
|
providers
|
|
|
anthropic
Package anthropic provides an implementation of the fantasy AI SDK for Anthropic's language models.
|
Package anthropic provides an implementation of the fantasy AI SDK for Anthropic's language models. |
|
azure
Package azure provides an implementation of the fantasy AI SDK for Azure's language models.
|
Package azure provides an implementation of the fantasy AI SDK for Azure's language models. |
|
bedrock
Package bedrock provides an implementation of the fantasy AI SDK for AWS Bedrock's language models.
|
Package bedrock provides an implementation of the fantasy AI SDK for AWS Bedrock's language models. |
|
google
Package google provides an implementation of the fantasy AI SDK for Google's language models.
|
Package google provides an implementation of the fantasy AI SDK for Google's language models. |
|
internal/httpheaders
Package httpheaders provides shared User-Agent resolution for all HTTP-based providers.
|
Package httpheaders provides shared User-Agent resolution for all HTTP-based providers. |
|
kronk
Package kronk provides an implementation of the fantasy AI SDK for local models using the Kronk SDK.
|
Package kronk provides an implementation of the fantasy AI SDK for local models using the Kronk SDK. |
|
openai
Package openai provides an implementation of the fantasy AI SDK for OpenAI's language models.
|
Package openai provides an implementation of the fantasy AI SDK for OpenAI's language models. |
|
openaicompat
Package openaicompat provides an implementation of the fantasy AI SDK for OpenAI-compatible APIs.
|
Package openaicompat provides an implementation of the fantasy AI SDK for OpenAI-compatible APIs. |
|
openrouter
Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models.
|
Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models. |
|
vercel
Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway.
|
Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway. |
|
Package schema provides JSON schema generation and validation utilities.
|
Package schema provides JSON schema generation and validation utilities. |
