api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FalseSchema added in v0.3.0

func FalseSchema() *jsonschema.Schema

FalseSchema returns a new Schema that fails to validate any value. This represents "additionalProperties": false in JSON Schema. In JSON Schema 2020-12, false can also be represented as {"not": {}} and that's what the `jsonschema` package does.

func GetMetadata

func GetMetadata[T any](provider string, source MetadataSource) *T

GetMetadata is a generic helper function to retrieve provider-specific metadata as a pointer to the requested type.

If the provider is not found or the type doesn't match, it returns nil.

We recommend providers use this helper to expose predefined metadata functions.

Types

type AISDKError

type AISDKError struct {
	// Name is the name of the error
	Name string

	// Message is the error message
	Message string

	// Cause is the underlying cause of the error, if any
	Cause any
}

AISDKError is a custom error class for AI SDK related errors.

func NewAISDKError

func NewAISDKError(name, message string, cause any) *AISDKError

NewAISDKError creates an AI SDK Error. Parameters:

  • name: The name of the error.
  • message: The error message.
  • cause: The underlying cause of the error.

func (*AISDKError) Error

func (e *AISDKError) Error() string

Error implements the error interface

type APICallError

type APICallError struct {
	*AISDKError

	// URL of the API endpoint that was called
	URL *url.URL

	// Request contains the original HTTP request
	Request *http.Request

	// StatusCode is the HTTP status code of the response if any
	StatusCode int

	// Response contains the original HTTP response
	Response *http.Response

	// Data contains additional error data, if any
	Data any
}

APICallError represents an error that occurred during an API call

func (*APICallError) IsRetryable

func (e *APICallError) IsRetryable() bool

IsRetryable indicates whether the request can be retried Returns true for status codes: 408 (timeout), 409 (conflict), 429 (too many requests), or 5xx (server errors)

type AssistantMessage

type AssistantMessage struct {
	// Content contains an array of content blocks (text or tool calls)
	Content []ContentBlock `json:"content"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

AssistantMessage represents an assistant message that can contain text and tool calls

func (*AssistantMessage) GetProviderMetadata

func (m *AssistantMessage) GetProviderMetadata() *ProviderMetadata

func (*AssistantMessage) MarshalJSON added in v0.2.0

func (m *AssistantMessage) MarshalJSON() ([]byte, error)

MarshalJSON includes the role field when marshaling AssistantMessage

func (*AssistantMessage) Role

func (m *AssistantMessage) Role() MessageRole

func (*AssistantMessage) UnmarshalJSON added in v0.2.0

func (m *AssistantMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for AssistantMessage

type CallOptions

type CallOptions struct {
	// MaxOutputTokens specifies the maximum number of tokens to generate
	MaxOutputTokens int `json:"max_output_tokens,omitzero"`

	// Temperature controls randomness in the model's output.
	// It is recommended to set either Temperature or TopP, but not both.
	Temperature *float64 `json:"temperature,omitzero"`

	// TopP controls nucleus sampling.
	// It is recommended to set either Temperature or TopP, but not both.
	TopP float64 `json:"top_p,omitzero"`

	// TopK limits sampling to the top K options for each token.
	// Used to remove "long tail" low probability responses.
	// Recommended for advanced use cases only.
	TopK int `json:"top_k,omitzero"`

	// PresencePenalty affects the likelihood of the model repeating
	// information that is already in the prompt
	PresencePenalty float64 `json:"presence_penalty,omitzero"`

	// FrequencyPenalty affects the likelihood of the model
	// repeatedly using the same words or phrases
	FrequencyPenalty float64 `json:"frequency_penalty,omitzero"`

	// StopSequences specifies sequences that will stop generation when produced.
	// Providers may have limits on the number of stop sequences.
	StopSequences []string `json:"stop_sequences,omitempty"`

	// Seed provides an integer seed for random sampling.
	// If supported by the model, calls will generate deterministic results.
	Seed int `json:"seed,omitzero"`

	// Headers specifies additional HTTP headers to send with the request.
	// Only applicable for HTTP-based providers.
	Headers http.Header `json:"headers,omitempty"`

	// Tools that are available for the model to use.
	Tools []ToolDefinition `json:"tools,omitempty"`

	// ToolChoice specifies how the model should select which tool to use.
	// Defaults to 'auto'.
	ToolChoice *ToolChoice `json:"tool_choice,omitzero"`

	// ResponseFormat specifies whether the output should be text or JSON.
	// For JSON output, a schema can optionally guide the model.
	ResponseFormat *ResponseFormat `json:"response_format,omitzero"` // TODO: Only for providers, not exposed.

	// ProviderMetadata contains additional provider-specific metadata.
	// The metadata is passed through to the provider from the AI SDK and enables
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

CallOptions represents the options for language model calls.

func (*CallOptions) GetProviderMetadata

func (o *CallOptions) GetProviderMetadata() *ProviderMetadata

func (*CallOptions) UnmarshalJSON added in v0.2.0

func (o *CallOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for CallOptions to handle the polymorphic ToolDefinition interface

type CallWarning

type CallWarning struct {

	// Type indicates the kind of warning: "unsupported-setting", "unsupported-tool", or "other"
	Type string `json:"type"`

	// Setting contains the name of the unsupported setting when Type is "unsupported-setting"
	Setting string `json:"setting,omitzero"`

	// Tool contains the unsupported tool when Type is "unsupported-tool"
	Tool ToolDefinition `json:"tool,omitzero"`

	// Details provides additional information about the warning
	Details string `json:"details,omitzero"`

	// Message contains a human-readable warning message
	Message string `json:"message,omitzero"`
}

CallWarning represents a warning from the model provider for a call. The call will proceed, but some settings might not be supported, which can lead to suboptimal results.

type ContentBlock

type ContentBlock interface {
	// Type returns the type of the content block.
	// Valid types are: "text", "image", "file", "tool-call", "tool-result", "reasoning".
	Type() ContentBlockType
	// ProviderMetadata returns the provider-specific metadata for the content block.
	GetProviderMetadata() *ProviderMetadata
}

ContentBlock represents a block of content in a message

func ContentFromText

func ContentFromText(text string) []ContentBlock

ContentFromText creates a slice of content blocks with a single text block.

type ContentBlockType

type ContentBlockType string

ContentBlockType represents the type of content block in a message

const (
	// ContentBlockTypeText represents text content
	ContentBlockTypeText ContentBlockType = "text"
	// ContentBlockTypeImage represents image content
	ContentBlockTypeImage ContentBlockType = "image"
	// ContentBlockTypeFile represents file content
	ContentBlockTypeFile ContentBlockType = "file"
	// ContentBlockTypeToolCall represents a tool call
	ContentBlockTypeToolCall ContentBlockType = "tool-call"
	// ContentBlockTypeToolResult represents a tool result
	ContentBlockTypeToolResult ContentBlockType = "tool-result"
	// ContentBlockTypeReasoning represents reasoning text from the model
	ContentBlockTypeReasoning ContentBlockType = "reasoning"
	// ContentBlockTypeSource represents a source block
	ContentBlockTypeSource ContentBlockType = "source"
)

type EmptyResponseBodyError

type EmptyResponseBodyError struct {
	*AISDKError
}

EmptyResponseBodyError indicates that the response body is empty

func NewEmptyResponseBodyError

func NewEmptyResponseBodyError(message string) *EmptyResponseBodyError

NewEmptyResponseBodyError creates a new EmptyResponseBodyError instance Parameters:

  • message: The error message (optional, defaults to "Empty response body")

type ErrorEvent

type ErrorEvent struct {
	// Err contains any error messages or error objects encountered during the stream
	Err any `json:"error"`
}

ErrorEvent represents an error that occurred during streaming.

func (*ErrorEvent) Error

func (b *ErrorEvent) Error() string

func (*ErrorEvent) Type

func (b *ErrorEvent) Type() EventType

type EventType

type EventType string

EventType represents the different types of stream events.

const (
	// EventTextDelta represents an incremental text response from the model.
	EventTextDelta EventType = "text-delta"

	// EventReasoning is an optional reasoning or intermediate explanation generated by the model.
	EventReasoning EventType = "reasoning"

	// EventReasoningSignature represents a signature that verifies reasoning content.
	EventReasoningSignature EventType = "reasoning-signature"

	// EventSource represents a citation that was used to generate the response.
	EventSource EventType = "source"

	// EventFile represents a file generated by the model.
	EventFile EventType = "file"

	// EventToolCall represents a completed tool call with all arguments provided.
	EventToolCall EventType = "tool-call"

	// EventToolCallDelta is an incremental update for tool call arguments.
	EventToolCallDelta EventType = "tool-call-delta"

	// EventResponseMetadata contains additional response metadata, such as timestamps or provider details.
	EventResponseMetadata EventType = "response-metadata"

	// EventFinish is the final part of the stream, providing the finish reason and usage statistics.
	EventFinish EventType = "finish"

	// EventError indicates that an error occurred during the stream.
	EventError EventType = "error"
)

type FileBlock

type FileBlock struct {
	// Filename is the filename of the file. Optional.
	Filename string `json:"filename,omitzero"` // TODO: input only

	// URL is the external URL of the file.
	URL string `json:"url,omitzero"`

	// Data contains the file data as raw bytes.
	// If this is set, also set the MimeType so that the AI SDK knows
	// how to interpret the data.
	Data []byte `json:"data,omitempty"`

	// MediaType is the IANA media type (mime type) of the file.
	// It can support wildcards, e.g. `image/*` (in which case the provider needs to take appropriate action).
	// See: https://www.iana.org/assignments/media-types/media-types.xhtml
	MediaType string `json:"media_type,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

FileBlock represents a file in a message. Either URL or Data should be set, but not both.

func FileBlockFromData

func FileBlockFromData(data []byte, mediaType string) *FileBlock

FileBlockFromData creates a new file block from raw bytes.

func FileBlockFromURL

func FileBlockFromURL(url string) *FileBlock

FileBlockFromURL creates a new file block from a URL.

func (*FileBlock) GetProviderMetadata

func (b *FileBlock) GetProviderMetadata() *ProviderMetadata

func (*FileBlock) MarshalJSON added in v0.2.0

func (b *FileBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling FileBlock

func (*FileBlock) Type

func (b *FileBlock) Type() ContentBlockType

type FileEvent

type FileEvent struct {
	// MediaType is the IANA media type (mime type) of the file
	MediaType string `json:"media_type"`
	// Data contains the generated file as a byte array
	Data []byte `json:"data"`
}

FileEvent represents a file generated by the model.

Used to add a file to the response via a FileBlock.

func (*FileEvent) Type

func (b *FileEvent) Type() EventType

type FinishEvent

type FinishEvent struct {
	// Usage contains token usage statistics
	Usage Usage `json:"usage,omitzero"`

	// FinishReason indicates why the model stopped generating
	FinishReason FinishReason `json:"finish_reason"`

	// ProviderMetadata contains provider-specific metadata
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

FinishEvent represents the final part of the stream.

It will be sent once the stream has finished processing.

func (*FinishEvent) GetProviderMetadata

func (b *FinishEvent) GetProviderMetadata() *ProviderMetadata

func (*FinishEvent) Type

func (b *FinishEvent) Type() EventType

type FinishReason

type FinishReason string

FinishReason indicates why a language model finished generating a response.

const (
	// FinishReasonStop indicates the model generated a stop sequence
	FinishReasonStop FinishReason = "stop"

	// FinishReasonLength indicates the model reached the maximum number of tokens
	FinishReasonLength FinishReason = "length"

	// FinishReasonContentFilter indicates a content filter violation stopped the model
	FinishReasonContentFilter FinishReason = "content-filter"

	// FinishReasonToolCalls indicates the model triggered tool calls
	FinishReasonToolCalls FinishReason = "tool-calls"

	// FinishReasonError indicates the model stopped because of an error
	FinishReasonError FinishReason = "error"

	// FinishReasonOther indicates the model stopped for other reasons
	FinishReasonOther FinishReason = "other"

	// FinishReasonUnknown indicates the model has not transmitted a finish reason
	FinishReasonUnknown FinishReason = "unknown"
)

type FunctionTool

type FunctionTool struct {
	// Name is the unique identifier for this tool within this model call
	Name string `json:"name"`

	// Description explains the tool's purpose. The language model uses this to understand
	// the tool's purpose and provide better completion suggestions.
	Description string `json:"description,omitzero"`

	// InputSchema defines the expected inputs. The language model uses this to understand
	// the tool's input requirements and provide matching suggestions.
	// InputSchema should be defined using a JSON schema.
	InputSchema *jsonschema.Schema `json:"input_schema,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

FunctionTool represents a tool that has a name, description, and set of input arguments. Note: this is not the user-facing tool definition. The AI SDK methods will map the user-facing tool definitions to this format.

func (FunctionTool) GetProviderMetadata added in v0.4.0

func (t FunctionTool) GetProviderMetadata() *ProviderMetadata

GetProviderMetadata returns the provider-specific metadata for the function tool

func (*FunctionTool) MarshalJSON added in v0.2.0

func (t *FunctionTool) MarshalJSON() ([]byte, error)

FunctionTool JSON marshaling - automatically includes "type" field

func (*FunctionTool) Type added in v0.2.0

func (t *FunctionTool) Type() string

Type is the type of the tool (always "function")

type ImageBlock

type ImageBlock struct {
	// URL is the external URL of the image.
	URL string `json:"url,omitzero"`

	// Data contains the image data as raw bytes.
	// If this is set, also set the MimeType so that the AI SDK knows
	// how to interpret the data.
	Data []byte `json:"data,omitempty"`

	// MediaType is the IANA media type (mime type) of the image
	MediaType string `json:"media_type,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

ImageBlock represents an image in a message. Either URL or Data should be set, but not both. TODO: merge into file block in language model v2

func ImageBlockFromData

func ImageBlockFromData(data []byte, mediaType string) *ImageBlock

ImageBlockFromData creates a new image block from raw bytes.

func ImageBlockFromURL

func ImageBlockFromURL(url string) *ImageBlock

ImageBlockFromURL creates a new image block from a URL.

func (*ImageBlock) GetProviderMetadata

func (b *ImageBlock) GetProviderMetadata() *ProviderMetadata

func (*ImageBlock) MarshalJSON added in v0.2.0

func (b *ImageBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling ImageBlock

func (*ImageBlock) Type

func (b *ImageBlock) Type() ContentBlockType

type InvalidArgumentError

type InvalidArgumentError struct {
	*AISDKError

	// Argument is the name of the invalid argument
	Argument string
}

InvalidArgumentError indicates that a function argument is invalid

func NewInvalidArgumentError

func NewInvalidArgumentError(message, argument string, cause any) *InvalidArgumentError

NewInvalidArgumentError creates a new InvalidArgumentError instance Parameters:

  • message: The error message
  • argument: The name of the invalid argument
  • cause: The underlying cause of the error (optional)

type InvalidPromptError

type InvalidPromptError struct {
	*AISDKError

	// Prompt is the invalid prompt that caused the error
	Prompt any
}

InvalidPromptError indicates that a prompt is invalid. This error should be returned by providers when they cannot process a prompt.

func NewInvalidPromptError

func NewInvalidPromptError(prompt any, message string, cause any) *InvalidPromptError

NewInvalidPromptError creates a new InvalidPromptError instance Parameters:

  • prompt: The invalid prompt
  • message: The error message describing why the prompt is invalid
  • cause: The underlying cause of the error (optional)

type InvalidResponseDataError

type InvalidResponseDataError struct {
	*AISDKError

	// Data is the invalid response data that caused the error
	Data any
}

InvalidResponseDataError indicates that the server returned a response with invalid data content. This should be returned by providers when they cannot parse the response from the API.

func NewInvalidResponseDataError

func NewInvalidResponseDataError(data any, message string) *InvalidResponseDataError

NewInvalidResponseDataError creates a new InvalidResponseDataError instance Parameters:

  • data: The invalid response data
  • message: The error message (optional, will be auto-generated if empty)

type JSONParseError

type JSONParseError struct {
	*AISDKError

	// Text is the string that failed to parse as JSON
	Text string
}

JSONParseError indicates a failure in parsing JSON

func NewJSONParseError

func NewJSONParseError(text string, cause any) *JSONParseError

NewJSONParseError creates a new JSONParseError instance Parameters:

  • text: The text that failed to parse as JSON
  • cause: The underlying parsing error

type LanguageModel

type LanguageModel interface {
	// ProviderName returns the name of the provider for logging purposes.
	ProviderName() string

	// ModelID returns the provider-specific model ID for logging purposes.
	ModelID() string

	// SupportedUrls returns URL patterns supported by the model, grouped by media type.
	//
	// The MediaType field contains media type patterns or full media types (e.g. "*/*" for everything,
	// "audio/*", "video/*", or "application/pdf"). The URLPatterns field contains arrays of regular
	// expression patterns that match URL paths.
	//
	// The matching is performed against lowercase URLs.
	//
	// URLs that match these patterns are supported natively by the model and will not
	// be downloaded by the SDK. For non-matching URLs, the SDK will download the content
	// and pass it directly to the model.
	//
	// If nil or an empty slice is returned, the SDK will download all files.
	SupportedUrls() []SupportedURL

	// Generate generates a language model output (non-streaming).
	//
	// The prompt parameter is a standardized prompt type, not the user-facing prompt.
	// The AI SDK methods will map the user-facing prompt types such as chat or
	// instruction prompts to this format.
	Generate(ctx context.Context, prompt []Message, opts CallOptions) (*Response, error)

	// Stream generates a language model output (streaming).
	// Returns a stream of events from the model.
	//
	// The prompt parameter is a standardized prompt type, not the user-facing prompt.
	// The AI SDK methods will map the user-facing prompt types such as chat or
	// instruction prompts to this format.
	Stream(ctx context.Context, prompt []Message, opts CallOptions) (*StreamResponse, error)
}

LanguageModel represents a language model.

type LoadAPIKeyError

type LoadAPIKeyError struct {
	*AISDKError
}

LoadAPIKeyError indicates a failure in loading an API key

func NewLoadAPIKeyError

func NewLoadAPIKeyError(message string) *LoadAPIKeyError

NewLoadAPIKeyError creates a new LoadAPIKeyError instance Parameters:

  • message: The error message describing why the API key failed to load

type LoadSettingError

type LoadSettingError struct {
	*AISDKError
}

LoadSettingError indicates a failure in loading a setting

func NewLoadSettingError

func NewLoadSettingError(message string) *LoadSettingError

NewLoadSettingError creates a new LoadSettingError instance Parameters:

  • message: The error message describing why the setting failed to load

type LogProb

type LogProb struct {
	// Token is the text of the token
	Token string `json:"token"`

	// LogProb is the log probability of the token
	LogProb float64 `json:"logprob"`

	// TopLogProbs contains the log probabilities of alternative tokens
	TopLogProbs []TokenLogProb `json:"top_logprobs"`
}

LogProb represents the log probability information for a token, including its top alternative tokens.

type LogProbs

type LogProbs []LogProb

LogProbs represents a sequence of token log probabilities

type Message

type Message interface {
	// Role returns the role of the message sender.
	// Valid roles are: "system", "user", "assistant", or "tool".
	Role() MessageRole
	GetProviderMetadata() *ProviderMetadata
}

Message represents a message in a prompt sequence. Note: Not all models and prompt formats support multi-modal inputs and tool calls. The validation happens at runtime.

Note: This is not a user-facing prompt. The AI SDK methods will map the user-facing prompt types such as chat or instruction prompts to this format.

Note: there could be additional blocks for each role in the future, e.g. when the assistant can return images or the user can share files such as PDFs.

type MessageRole

type MessageRole string

MessageRole represents the role of a message sender

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 MetadataSource

type MetadataSource interface {
	GetProviderMetadata() *ProviderMetadata
}

type ModelType

type ModelType string

ModelType represents the type of AI model

const (
	// LanguageModelType represents a language model type
	LanguageModelType ModelType = "languageModel"
	// TextEmbeddingModelType represents a text embedding model type
	TextEmbeddingModelType ModelType = "textEmbeddingModel"
	// ImageModelType represents an image model type
	ImageModelType ModelType = "imageModel"
)

type NoContentGeneratedError

type NoContentGeneratedError struct {
	*AISDKError
}

NoContentGeneratedError is returned when the AI provider fails to generate any content

func NewNoContentGeneratedError

func NewNoContentGeneratedError(message string) *NoContentGeneratedError

NewNoContentGeneratedError creates a new NoContentGeneratedError instance Parameters:

  • message: The error message (optional, defaults to "No content generated.")

type NoSuchModelError

type NoSuchModelError struct {
	*AISDKError

	// ModelID is the identifier of the model that was not found
	ModelID string

	// ModelType is the type of model that was requested
	ModelType ModelType
}

NoSuchModelError indicates that the requested model does not exist

func NewNoSuchModelError

func NewNoSuchModelError(modelID string, modelType ModelType) *NoSuchModelError

NewNoSuchModelError creates a new NoSuchModelError instance Parameters:

  • modelID: The identifier of the model that was not found
  • modelType: The type of model that was requested

type ObjectGenerationMode

type ObjectGenerationMode string

ObjectGenerationMode specifies how the model should generate structured objects.

const (
	// ObjectGenerationModeNone indicates no specific object generation mode (empty string)
	ObjectGenerationModeNone ObjectGenerationMode = ""

	// ObjectGenerationModeJSON indicates the model should generate JSON directly
	ObjectGenerationModeJSON ObjectGenerationMode = "json"

	// ObjectGenerationModeTool indicates the model should use tool calls to generate objects
	ObjectGenerationModeTool ObjectGenerationMode = "tool"
)

type Provider

type Provider interface {
	// LanguageModel returns the language model with the given id.
	// The model id is then passed to the provider function to get the model.
	//
	// Parameters:
	//   modelID: The id of the model to return.
	//
	// Returns:
	//   The language model associated with the id
	//   error of type NoSuchModelError if no such model exists
	LanguageModel(modelID string) (LanguageModel, error)
}

Provider is a provider for language and text embedding models.

type ProviderDefinedTool

type ProviderDefinedTool struct {
	// ID is the tool identifier in format "<provider-name>.<tool-name>"
	ID string `json:"id"`

	// Name returns the unique name used to identify this tool within the model's messages.
	// This is the name that will be used by the language model as the value of the ToolName field
	// in ToolCall blocks.
	Name string `json:"name"`

	// Args contains the arguments for configuring the tool. Must match the expected arguments
	// defined by the provider for this tool.
	// Providers should support both a JSON-serializable type and a map[string]interface{} type.
	Args any `json:"args"`
}

ProviderDefinedTool represents a tool that has built-in support by the provider. Provider implementations will usually predefine these.

func (*ProviderDefinedTool) MarshalJSON added in v0.2.0

func (t *ProviderDefinedTool) MarshalJSON() ([]byte, error)

ProviderDefinedTool JSON marshaling - automatically includes "type" field

func (*ProviderDefinedTool) Type added in v0.2.0

func (t *ProviderDefinedTool) Type() string

Type is the type of the tool (always "provider-defined")

type ProviderMetadata

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

ProviderMetadata provides access to provider-specific metadata structures. It stores and retrieves strongly-typed metadata for specific providers.

func NewProviderMetadata

func NewProviderMetadata(data map[string]any) *ProviderMetadata

NewProviderMetadata creates a new ProviderMetadata with the given initial data. If data is nil, an empty map will be created.

func (*ProviderMetadata) Get

func (p *ProviderMetadata) Get(provider string) (any, bool)

Get retrieves the metadata for a specific provider. Returns the metadata and a boolean indicating whether the provider was found.

func (*ProviderMetadata) Has

func (p *ProviderMetadata) Has(provider string) bool

Has checks if metadata exists for a specific provider.

func (*ProviderMetadata) IsZero

func (p *ProviderMetadata) IsZero() bool

IsZero returns true if this ProviderMetadata is a zero value or contains no data.

func (*ProviderMetadata) MarshalJSON

func (p *ProviderMetadata) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It serializes the underlying data map to JSON.

func (*ProviderMetadata) Set

func (p *ProviderMetadata) Set(provider string, metadata any)

Set stores metadata for a specific provider.

func (*ProviderMetadata) UnmarshalJSON added in v0.2.0

func (p *ProviderMetadata) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It deserializes JSON into the underlying data map.

type ReasoningBlock

type ReasoningBlock struct {
	// Text contains the reasoning text.
	Text string `json:"text,omitempty"`

	// Signature is an optional signature for verifying that the reasoning originated from the model.
	Signature string `json:"signature,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

ReasoningBlock represents a reasoning content block of a prompt. It contains a string of reasoning text.

func (*ReasoningBlock) GetProviderMetadata

func (b *ReasoningBlock) GetProviderMetadata() *ProviderMetadata

func (*ReasoningBlock) MarshalJSON added in v0.2.0

func (b *ReasoningBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling ReasoningBlock

func (*ReasoningBlock) Type

func (b *ReasoningBlock) Type() ContentBlockType

type ReasoningEvent

type ReasoningEvent struct {
	// TextDelta is a partial reasoning text from the model
	TextDelta string `json:"text_delta"`
}

ReasoningEvent represents an incremental reasoning response from the model.

Used to update the text of a ReasoningBlock.

func (*ReasoningEvent) Type

func (b *ReasoningEvent) Type() EventType

type ReasoningSignatureEvent

type ReasoningSignatureEvent struct {
	// Signature is the cryptographic signature for verifying reasoning
	Signature string `json:"signature"`
}

ReasoningSignatureEvent represents an incremental signature update for reasoning text.

Used to update the signature field of a ReasoningBlock.

func (*ReasoningSignatureEvent) Type

type RequestInfo

type RequestInfo struct {
	// Body is the raw HTTP body that was sent to the provider
	Body []byte `json:"body,omitempty"`
}

RequestInfo contains optional request information for telemetry.

type Response

type Response struct {
	// Content contains the ordered list of content blocks that the model generated.
	Content []ContentBlock `json:"content"`

	// FinishReason contains an explanation for why the model finished generating.
	FinishReason FinishReason `json:"finish_reason"`

	// Usage contains information about the number of tokens used by the model.
	Usage Usage `json:"usage"`

	// Additional provider-specific metadata. They are passed through from the
	// provider to enable provider-specific functionality.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`

	// RequestInfo is optional request information for telemetry and debugging purposes.
	RequestInfo *RequestInfo `json:"request,omitzero"`

	// ResponseInfo is optional response information for telemetry and debugging purposes.
	ResponseInfo *ResponseInfo `json:"response,omitzero"`

	// Warnings is a list of warnings that occurred during the call,
	// e.g. unsupported settings.
	Warnings []CallWarning `json:"warnings,omitempty"`
}

Response represents the result of a non-streaming language model generation.

func (*Response) GetProviderMetadata

func (r *Response) GetProviderMetadata() *ProviderMetadata

func (*Response) UnmarshalJSON added in v0.2.0

func (r *Response) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for Response

type ResponseFormat

type ResponseFormat struct {
	// Type indicates the response format type ("text" or "json")
	Type string `json:"type"`

	// Schema optionally provides a JSON schema to guide the model's output
	Schema *jsonschema.Schema `json:"schema,omitzero"`

	// Name optionally provides a name for the output to guide the model
	Name string `json:"name,omitzero"`

	// Description optionally provides additional context to guide the model
	Description string `json:"description,omitzero"`
}

ResponseFormat specifies the format of the model's response.

type ResponseInfo

type ResponseInfo struct {
	// ID for the generated response, if the provider sends one.
	ID string `json:"id,omitzero"`

	// Timestamp for the start of the generated response, if the provider sends one.
	Timestamp time.Time `json:"timestamp,omitzero"`

	// ModelID of the model that was used to generate the response, if the provider sends one.
	ModelID string `json:"model_id,omitzero"`

	// Headers contains the HTTP response headers.
	Headers http.Header `json:"headers,omitzero"`

	// Body is the raw HTTP body that was returned by the provider.
	// Not provided for streaming responses.
	Body []byte `json:"body,omitempty"`

	// Status is a status code and message. e.g. "200 OK"
	Status string `json:"status,omitzero"`

	// StatusCode is a status code as integer. e.g. 200
	StatusCode int `json:"status_code,omitzero"`
}

ResponseInfo contains optional response information for telemetry.

type ResponseMetadataEvent

type ResponseMetadataEvent struct {
	// ID for the generated response, if the provider sends one
	ID string `json:"id,omitzero"`
	// Timestamp represents when the stream part was generated
	Timestamp time.Time `json:"timestamp,omitzero"`
	// ModelID for the generated response, if the provider sends one
	ModelID string `json:"model_id,omitzero"`
}

ResponseMetadataEvent contains additional response metadata.

It will be sent as soon as it is available, without having to wait for the FinishEvent.

func (*ResponseMetadataEvent) Type

func (b *ResponseMetadataEvent) Type() EventType

type Source

type Source struct {
	// SourceType indicates the type of source. Currently only "url" is supported.
	SourceType string `json:"source_type"`

	// ID is the unique identifier of the source.
	ID string `json:"id"`

	// URL is the URL of the source.
	URL string `json:"url"`

	// Title is the optional title of the source.
	Title string `json:"title,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	ProviderMetadata ProviderMetadata `json:"provider_metadata,omitzero"`
}

Source represents a source that has been used as input to generate the response.

type SourceBlock

type SourceBlock struct {
	// ID is the ID of the source.
	ID string `json:"id"`

	// URL is the external URL of the source.
	URL string `json:"url"`

	// Title is the title of the source.
	Title string `json:"title,omitzero"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

SourceBlock represents a source that has been used as input to generate the response.

func (*SourceBlock) GetProviderMetadata

func (b *SourceBlock) GetProviderMetadata() *ProviderMetadata

func (*SourceBlock) MarshalJSON added in v0.2.0

func (b *SourceBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling SourceBlock

func (*SourceBlock) Type

func (b *SourceBlock) Type() ContentBlockType

type SourceEvent

type SourceEvent struct {
	// Source contains information about the source
	Source Source `json:"source"`
}

SourceEvent represents a source that was used to generate the response.

Used to add a source to the response.

func (*SourceEvent) Type

func (b *SourceEvent) Type() EventType

type StreamEvent

type StreamEvent interface {
	// Type returns the type of event being received.
	Type() EventType
}

StreamEvent represents a streamed incremental update of the language model output.

type StreamResponse

type StreamResponse struct {
	// Stream is the sequence of events received from the model.
	// Iterating over events might block if we're waiting for the LLM to respond.
	Stream iter.Seq[StreamEvent]

	// RequestInfo is optional request information for telemetry and debugging purposes.
	RequestInfo *RequestInfo `json:"request,omitzero"`

	// ResponseInfo is optional response information for telemetry and debugging purposes.
	ResponseInfo *ResponseInfo `json:"response,omitzero"`
}

StreamResponse represents the result of a streaming language model call.

type SupportedURL

type SupportedURL struct {
	// MediaType is the IANA media type (mime type) of the URL.
	// A simple '*' wildcard is supported for the mime type or subtype
	// (e.g., "application/pdf", "audio/*", "*/*").
	MediaType string

	// TODO: change reasoning to support reasoning blocks as well
	// URLPatterns contains regex patterns for URL paths that match this media type
	URLPatterns []string
}

SupportedURL defines URL patterns supported for a specific media type

type SystemMessage

type SystemMessage struct {
	// Content contains the message text
	Content string `json:"content"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

SystemMessage represents a system message with plain text content

func (*SystemMessage) GetProviderMetadata

func (m *SystemMessage) GetProviderMetadata() *ProviderMetadata

func (*SystemMessage) MarshalJSON added in v0.2.0

func (m *SystemMessage) MarshalJSON() ([]byte, error)

MarshalJSON includes the role field when marshaling SystemMessage

func (*SystemMessage) Role

func (m *SystemMessage) Role() MessageRole

func (*SystemMessage) UnmarshalJSON added in v0.2.0

func (m *SystemMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for SystemMessage

type TextBlock

type TextBlock struct {
	// Text contains the text content
	Text string `json:"text"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

TextBlock represents text content in a message

func (*TextBlock) GetProviderMetadata

func (b *TextBlock) GetProviderMetadata() *ProviderMetadata

func (*TextBlock) MarshalJSON added in v0.2.0

func (b *TextBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling TextBlock

func (*TextBlock) Type

func (b *TextBlock) Type() ContentBlockType

type TextDeltaEvent

type TextDeltaEvent struct {
	// TextDelta is a partial text response from the model
	TextDelta string `json:"text_delta"`
}

TextDeltaEvent represents an incremental text response from the model

Used to update a TextBlock incrementally.

func (*TextDeltaEvent) Type

func (b *TextDeltaEvent) Type() EventType

type TokenLogProb

type TokenLogProb struct {
	// Token is the text of the token
	Token string `json:"token"`

	// LogProb is the log probability of the token
	LogProb float64 `json:"logprob"`
}

TokenLogProb represents the log probability for a single token.

type TooManyEmbeddingValuesForCallError

type TooManyEmbeddingValuesForCallError struct {
	*AISDKError

	// Provider is the name of the AI provider
	Provider string

	// ModelID is the identifier of the model
	ModelID string

	// MaxEmbeddingsPerCall is the maximum number of embeddings allowed per call
	MaxEmbeddingsPerCall int

	// Values are the embedding values that were provided
	Values []any
}

TooManyEmbeddingValuesForCallError indicates that too many values were provided for a single embedding call

func NewTooManyEmbeddingValuesForCallError

func NewTooManyEmbeddingValuesForCallError(provider, modelID string, maxEmbeddingsPerCall int, values []any) *TooManyEmbeddingValuesForCallError

NewTooManyEmbeddingValuesForCallError creates a new TooManyEmbeddingValuesForCallError instance Parameters:

  • provider: The name of the AI provider
  • modelID: The identifier of the model
  • maxEmbeddingsPerCall: The maximum number of embeddings allowed per call
  • values: The embedding values that were provided

type ToolCallBlock

type ToolCallBlock struct {

	// ToolCallID is the ID of the tool call. This ID is used to match the tool call with the tool result.
	ToolCallID string `json:"tool_call_id"`

	// ToolName is the name of the tool that is being called
	// TODO: rename to ToolID (and change implementation in each provider)
	ToolName string `json:"tool_name"`

	// Args contains the arguments of the tool call as a JSON payload matching
	// the tool's input schema.
	// Note that args are often generated by the language model and may be
	// malformed.
	Args json.RawMessage `json:"args"` // TODO: decide if this is the right type for this field

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

ToolCallBlock represents a tool call in a message (usually generated by the AI model)

func (*ToolCallBlock) GetProviderMetadata

func (b *ToolCallBlock) GetProviderMetadata() *ProviderMetadata

func (*ToolCallBlock) MarshalJSON added in v0.2.0

func (b *ToolCallBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling ToolCallBlock

func (*ToolCallBlock) Type

func (b *ToolCallBlock) Type() ContentBlockType

type ToolCallDeltaEvent

type ToolCallDeltaEvent struct {
	// ToolCallID is the ID of the tool call
	ToolCallID string `json:"tool_call_id"`

	// ToolName is the name of the tool being invoked
	ToolName string `json:"tool_name"`

	// ArgsDelta is a partial JSON byte slice update for the tool call arguments
	ArgsDelta []byte `json:"args_delta"`
}

ToolCallDeltaEvent represents a tool call with incremental arguments. Tool call deltas are only needed for object generation modes. The tool call deltas must be partial JSON.

func (*ToolCallDeltaEvent) Type

func (b *ToolCallDeltaEvent) Type() EventType

type ToolCallEvent

type ToolCallEvent struct {
	// ToolCallID is the ID of the tool call. This ID is used to match the tool call with the tool result.
	ToolCallID string `json:"tool_call_id"`

	// ToolName is the name of the tool being invoked.
	ToolName string `json:"tool_name"`

	// Args contains the arguments of the tool call as a JSON payload matching
	// the tool's input schema.
	// Note that args are often generated by the language model and may be
	// malformed.
	Args json.RawMessage `json:"args"`
}

ToolCallEvent represents a complete tool call with all arguments.

Used to add a tool call to the response via a ToolCallBlock.

func (*ToolCallEvent) Type

func (b *ToolCallEvent) Type() EventType

type ToolChoice

type ToolChoice struct {
	// Type indicates how tools should be selected:
	// - "auto": tool selection is automatic (can be no tool)
	// - "none": no tool must be selected
	// - "required": one of the available tools must be selected
	// - "tool": a specific tool must be selected
	Type string `json:"type"`

	// ToolName specifies which tool to use when Type is "tool"
	// TODO: rename to ToolID (and change implementation)
	ToolName string `json:"tool_name,omitzero"`
}

ToolChoice specifies how tools should be selected by the model.

type ToolDefinition

type ToolDefinition interface {
	// Type is the type of the tool. Either "function" or "provider-defined".
	Type() string
	// contains filtered or unexported methods
}

ToolDefinition represents a tool that can be used in a language model call. It can either be a user-defined tool or a built-in provider-defined tool.

type ToolMessage

type ToolMessage struct {
	// Content contains an array of tool results
	Content []ToolResultBlock `json:"content"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

ToolMessage represents a tool message containing tool results

func (*ToolMessage) GetProviderMetadata

func (m *ToolMessage) GetProviderMetadata() *ProviderMetadata

func (*ToolMessage) MarshalJSON added in v0.2.0

func (m *ToolMessage) MarshalJSON() ([]byte, error)

MarshalJSON includes the role field when marshaling ToolMessage

func (*ToolMessage) Role

func (m *ToolMessage) Role() MessageRole

func (*ToolMessage) UnmarshalJSON added in v0.2.0

func (m *ToolMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for ToolMessage

type ToolResultBlock

type ToolResultBlock struct {

	// ToolCallID is the ID of the tool call that this result is associated with
	ToolCallID string `json:"tool_call_id"`

	// ToolName is the ID of the tool that generated this result
	// TODO: rename to ToolID
	ToolName string `json:"tool_name"`

	// Result contains the result of the tool call. This is a JSON-serializable object.
	Result any `json:"result"`

	// IsError indicates if the result is an error or an error message
	IsError bool `json:"is_error,omitzero"`

	// Content contains tool results as an array of blocks.
	// This enables advanced tool results including images.
	// When this is used, the Result field should be ignored
	// (if the provider supports content).
	Content []ContentBlock `json:"content,omitempty"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

ToolResultBlock represents a tool result in a message. Usually sent back to the model as input, after it requested a tool call with a matching ToolCallID.

func (*ToolResultBlock) GetProviderMetadata

func (b *ToolResultBlock) GetProviderMetadata() *ProviderMetadata

func (*ToolResultBlock) MarshalJSON added in v0.2.0

func (b *ToolResultBlock) MarshalJSON() ([]byte, error)

MarshalJSON includes the type field when marshaling ToolResultBlock

func (*ToolResultBlock) Type

func (*ToolResultBlock) UnmarshalJSON added in v0.2.0

func (b *ToolResultBlock) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for ToolResultBlock

type TypeValidationError

type TypeValidationError struct {
	*AISDKError

	// Value is the value that failed validation
	Value any
}

TypeValidationError represents a type validation failure

func NewTypeValidationError

func NewTypeValidationError(value, cause any) *TypeValidationError

NewTypeValidationError creates a new TypeValidationError instance Parameters:

  • value: The value that failed validation
  • cause: The original error or cause of the validation failure

func WrapTypeValidationError

func WrapTypeValidationError(value any, cause error) *TypeValidationError

WrapTypeValidationError wraps an error into a TypeValidationError. If the cause is already a TypeValidationError with the same value, it returns the cause. Otherwise, it creates a new TypeValidationError. Parameters:

  • value: The value that failed validation
  • cause: The original error or cause of the validation failure

Returns a TypeValidationError instance

type UnsupportedFunctionalityError

type UnsupportedFunctionalityError struct {
	*AISDKError

	// Functionality is the name of the unsupported functionality
	Functionality string
}

UnsupportedFunctionalityError indicates that a requested functionality is not supported

func NewUnsupportedFunctionalityError

func NewUnsupportedFunctionalityError(functionality, message string) *UnsupportedFunctionalityError

NewUnsupportedFunctionalityError creates a new UnsupportedFunctionalityError instance Parameters:

  • functionality: The name of the unsupported functionality
  • message: The error message (optional, will be auto-generated if empty)

type Usage

type Usage struct {
	// InputTokens is the number of tokens used by the input (prompt).
	InputTokens int `json:"input_tokens"`

	// OutputTokens is the number of tokens in the generated output (completion or tool call).
	OutputTokens int `json:"output_tokens"`

	// TotalTokens is the total number of tokens used as reported by the provider.
	// Note that this might be different from the sum of input tokens and output tokens
	// because it might include reasoning tokens or other overhead.
	TotalTokens int `json:"total_tokens"`

	// ReasoningTokens is the number of tokens used by model as part of the reasoning process.
	ReasoningTokens int `json:"reasoning_tokens,omitzero"`

	// CachedInputTokens is the number of input tokens that were cached from a previous call.
	CachedInputTokens int `json:"cached_input_tokens,omitzero"`
}

Usage represents token usage statistics for a model call.

If a provider returns additional usage information besides the ones below, that information is added to the provider metadata field.

func (*Usage) IsZero

func (u *Usage) IsZero() bool

IsZero returns true if all fields of the Usage struct are zero.

type UserMessage

type UserMessage struct {
	// Content contains an array of content blocks (text, image, or file)
	Content []ContentBlock `json:"content"`

	// ProviderMetadata contains additional provider-specific metadata.
	// They are passed through to the provider from the AI SDK and enable
	// provider-specific functionality that can be fully encapsulated in the provider.
	ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
}

UserMessage represents a user message that can contain text, images, and files

func (*UserMessage) GetProviderMetadata

func (m *UserMessage) GetProviderMetadata() *ProviderMetadata

func (*UserMessage) MarshalJSON added in v0.2.0

func (m *UserMessage) MarshalJSON() ([]byte, error)

MarshalJSON includes the role field when marshaling UserMessage

func (*UserMessage) Role

func (m *UserMessage) Role() MessageRole

func (*UserMessage) UnmarshalJSON added in v0.2.0

func (m *UserMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for UserMessage

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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