aisdk

package module
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

README

Call language models, stream responses, execute tools, and serve AI-powered endpoints from Go. Use the SDK on its own or pair it with an AI SDK React frontend.

Quick start · Documentation · Examples · API reference


Why

The SDK gives Go applications one API for model calls, streaming, tools, structured output, and multi-step agents across supported providers. It follows the design of Vercel's AI SDK and stays wire-compatible with its TypeScript frontend hooks. A Go endpoint can stream Server-Sent Events (SSE) directly to hooks such as useChat.

   Go backend                          React frontend
   ──────────                          ──────────────
   aisdk.StreamText(...)   ── SSE ──▶  useChat({ transport })
   aisdk.WriteUIMessageStream(w, …)    // same protocol

See How a request runs for the generation, tool, and streaming flow. Reuse an existing AI SDK React frontend or replace a TypeScript backend with Go without adding a protocol adapter.

Features

  • StreamText / GenerateText — stream a response or wait for the complete result, with retries and multi-step tool execution
  • React compatibility — serve useChat, useCompletion, and useObject
  • Composable tools — call plain Go functions from a model and require approval for consequential actions
  • Structured output — generate schema-validated objects, arrays, and choices
  • Multiple providers — call Anthropic, Amazon Bedrock, OpenAI, OpenAI-compatible APIs, and Grafana's hosted endpoint from internal services
  • Production controls — configure timeouts, fallback, logging, Prometheus metrics, and Agent Observability

Install

Create a Go project and install the core module and one provider:

mkdir ai-sdk-quickstart
cd ai-sdk-quickstart
go mod init example.com/ai-sdk-quickstart
go get github.com/grafana/ai-sdk
go get github.com/grafana/ai-sdk/providers/anthropic

See Choose a provider for Amazon Bedrock, OpenAI, OpenAI-compatible APIs, and the internally provisioned Grafana hosted endpoint.

Quick start

Save this complete program as main.go. It makes one model call and prints the response:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	aisdk "github.com/grafana/ai-sdk"
	"github.com/grafana/ai-sdk/provider"
	"github.com/grafana/ai-sdk/providers/anthropic"
)

func main() {
	apiKey := os.Getenv("ANTHROPIC_API_KEY")
	if apiKey == "" {
		log.Fatal("ANTHROPIC_API_KEY is required")
	}

	model := anthropic.New(apiKey, "claude-sonnet-5")
	result, err := aisdk.GenerateText(context.Background(), model,
		aisdk.WithModelMessages(provider.UserText("Explain goroutines in one sentence.")),
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result.Text)
}

Run it with an Anthropic API key:

ANTHROPIC_API_KEY=sk-... go run .

For project initialization and credential guidance, follow Installation. To stream this response to a React client, continue with Build a full-stack chat.

Where to go next

Goal Start here
Make model calls from Go Generate text from Go
Build a React chat Full-stack chat
Return typed data Structured output
Let a model call Go code Tools
Build a reusable agent Agent loops
Choose a model provider Provider overview
Add logging or observability Middleware overview
Prepare for production Production checklist

Full index: Documentation · Runnable code: Examples · Exact APIs: pkg.go.dev

Contributing

Contributions are welcome. CONTRIBUTING.md covers the development setup, the two conventions that make this repository unusual — upstream parity with the Vercel AI SDK, and spec-driven development with OpenSpec — and the pull request checklist. All participants follow our Code of Conduct.

License

Apache License 2.0. This SDK follows the design of Vercel's AI SDK, also Apache-2.0 licensed; attribution is recorded in NOTICE.

Documentation

Overview

Package aisdk is a Go port of Vercel's AI SDK.

It provides streaming LLM orchestration, tool execution, and SSE output compatible with the @ai-sdk/react hooks (useChat, useCompletion, useObject). The AI SDK docs at https://ai-sdk.dev/docs serve as the primary reference for naming, behavior, and protocol details.

Architecture

The package has two layers:

  • aisdk/provider (sub-package): the provider interface and types that LLM provider implementations depend on. This is a leaf package with no dependencies on the root.
  • aisdk (this package): orchestration (StreamText, GenerateText), the UIMessage type, tool system, SSE stream writing, and HTTP helpers.

Streaming

StreamText is the main entry point. It calls a provider.LanguageModel, runs a multi-step tool execution loop, and emits TextStreamPart events on StreamTextResult.FullStream. These events can be converted to SSE chunks via StreamTextResult.ToUIMessageStream and piped to an HTTP response with PipeUIMessageStreamToResponse.

result := aisdk.StreamText(ctx, myProvider,
    aisdk.WithMessages(messages...),
    aisdk.WithTools(tools),
)
aisdk.PipeUIMessageStreamToResponse(w, result.ToUIMessageStream())

For non-streaming use, GenerateText blocks until the LLM finishes and returns a populated GenerateTextResult.

Messages

UIMessage is the canonical message type, matching the AI SDK's UIMessage. It is JSON-serializable for persistence. Parts are represented as an interface (Part) with concrete types (TextPart, ToolInvocationPart, etc.) for type-safe access via type switches.

Provider-level messages live in the provider sub-package as the flat discriminated provider.Message struct (with provider.NewSystemMessage, provider.NewUserMessage, provider.NewAssistantMessage, and provider.NewToolMessage constructors). ConvertToModelMessages bridges the UI-layer and provider-layer message types.

ToResponseMessages converts a slice of collected response content parts into the next-call assistant + tool messages, preserving reasoning blocks (and provider signatures) across multi-step tool execution. The same output is surfaced on ResponseMetadata.Messages for the last completed step.

ExtractTextContent and ExtractReasoningContent read the forward direction: they join text or reasoning parts from a [[]provider.ContentPart] slice (text concatenates with no separator, reasoning joins with "\n"). ExtractTextContentGenerate and ExtractReasoningContentGenerate are the equivalents for [[]provider.GenerateContentPart] returned by provider.LanguageModel's DoGenerate.

Tools

Tools are defined as a ToolSet (map[string]Tool). Each tool has an optional Execute function: if nil, the tool call is returned to the caller for external execution. If set, StreamText executes it locally and feeds the result back to the model.

Standalone stream composition

CreateUIMessageStream and UIMessageStreamWriter allow composing LLM output with custom data parts independently of StreamText, matching the AI SDK's createUIMessageStream API.

Index

Examples

Constants

View Source
const (
	RoleSystem    = provider.RoleSystem
	RoleUser      = provider.RoleUser
	RoleAssistant = provider.RoleAssistant
)

Role constants re-exported from the provider package.

Variables

View Source
var (
	ErrWriterClosed  = errors.New("aisdk: write on closed stream writer")
	ErrAlreadyClosed = errors.New("aisdk: stream writer already closed")
)

Sentinel errors for UIMessageStreamWriter.

View Source
var ErrNoObjectGenerated = errors.New("aisdk: no object generated")

ErrNoObjectGenerated is returned when structured output validation fails. The LLM's raw text response is still available via result.Text().

View Source
var ErrNoOutputGenerated = errors.New("aisdk: no output generated")

ErrNoOutputGenerated is returned when a model stream ends before producing output.

View Source
var SSEDone = []byte("data: [DONE]\n\n")

SSEDone is the sentinel SSE event that terminates a stream.

Functions

func ConvertToModelMessages

func ConvertToModelMessages(messages []UIMessage, opts ...ConvertOption) ([]provider.Message, error)

ConvertToModelMessages converts UIMessages to provider.Message slice suitable for passing to provider.LanguageModel.DoStream/DoGenerate.

func CreateAgentUIStream

func CreateAgentUIStream(ctx context.Context, agent Agent, messages []UIMessage, opts ...UIMessageStreamOption) (<-chan UIMessageChunk, error)

CreateAgentUIStream creates a UIMessageChunk stream from an Agent and UI message history.

The helper validates the current Go UI message model before starting the provider stream, converts UI messages to model messages, calls Agent.Stream, and returns the existing ToUIMessageStream output with original messages preserved for response assembly.

func CreateIDGenerator

func CreateIDGenerator(opts IDGeneratorOptions) func() string

CreateIDGenerator returns a function that generates IDs with the given prefix and size. Size defaults to 7 if not positive.

func CreateUIMessageStream

func CreateUIMessageStream(params CreateUIMessageStreamParams) <-chan UIMessageChunk

CreateUIMessageStream creates a standalone UIMessageChunk stream. The Execute callback receives a writer; chunks written to it appear on the returned channel. The stream is framed with start/finish chunks.

func ExtractReasoningContent

func ExtractReasoningContent(content []provider.ContentPart) string

ExtractReasoningContent returns the reasoning text from provider.ContentPartTypeReasoning parts joined by "\n", matching upstream's extractReasoningContent semantics. Non-reasoning parts are skipped. Returns "" when no reasoning parts are present.

func ExtractReasoningContentGenerate

func ExtractReasoningContentGenerate(content []provider.GenerateContentPart) string

ExtractReasoningContentGenerate is the provider.GenerateContentPart counterpart of ExtractReasoningContent, for use with the content slice returned by provider.LanguageModel.DoGenerate. Returns the reasoning text from all provider.ContentReasoning parts joined by "\n", or "" if none are present.

func ExtractTextContent

func ExtractTextContent(content []provider.ContentPart) string

ExtractTextContent returns the concatenation of every Text field from provider.ContentPartTypeText parts in order. Non-text parts (reasoning, tool calls, files, etc.) are skipped. Returns "" when no text parts are present.

Mirrors upstream's extractTextContent helper, which is a small but commonly needed primitive when post-processing a model's response content. Use this when you drive your own loop on top of provider.LanguageModel or post-process structures that carry [[]provider.ContentPart].

func ExtractTextContentGenerate

func ExtractTextContentGenerate(content []provider.GenerateContentPart) string

ExtractTextContentGenerate is the provider.GenerateContentPart counterpart of ExtractTextContent, for use with the content slice returned by provider.LanguageModel.DoGenerate. Returns the concatenated text from all provider.ContentText parts, or "" if none are present.

func FingerprintTools

func FingerprintTools(tools ToolSet) (map[string]string, error)

FingerprintTools returns a stable digest for each tool's security-relevant definition fields: string description, input JSON schema, and title.

func FormatSSEEvent

func FormatSSEEvent(chunk UIMessageChunk) ([]byte, error)

FormatSSEEvent serializes a UIMessageChunk as an SSE data line.

func GenerateID

func GenerateID() string

GenerateID returns a random URL-safe ID with the default length (7 chars). If the system CSPRNG fails, it falls back to an atomic counter.

func PipeAgentUIStreamToResponse

func PipeAgentUIStreamToResponse(w http.ResponseWriter, stream <-chan UIMessageChunk) error

PipeAgentUIStreamToResponse writes an already-created Agent UI stream as SSE.

func PipeUIMessageStreamToResponse

func PipeUIMessageStreamToResponse(w http.ResponseWriter, stream <-chan UIMessageChunk) error

PipeUIMessageStreamToResponse writes a UIMessageChunk stream to an HTTP response as Server-Sent Events. It sets the appropriate SSE headers and terminates with a [DONE] sentinel.

func StreamUIMessage

func StreamUIMessage(stream <-chan UIMessageChunk, opts ...UIMessageReaderOption) <-chan UIMessage

StreamUIMessage reads a UIMessageChunk stream and emits progressive UIMessage snapshots at upstream-compatible write points.

func ToResponseMessages

func ToResponseMessages(parts []provider.ContentPart) []provider.Message

ToResponseMessages converts collected response content parts into the assistant + tool messages that should be fed into the next call.

It mirrors the upstream Vercel AI SDK helper at packages/ai/src/generate-text/to-response-messages.ts. The conversion preserves provider-specific metadata on every variant (notably reasoning signatures used by Anthropic extended thinking), and routes tool results the same way upstream does:

  • text: assistant text part (empty text is skipped)
  • reasoning: assistant reasoning part with provider.ContentPart.ProviderOptions copied from the input (this preserves Anthropic's "signature")
  • reasoning-file: assistant reasoning-file part (passthrough)
  • file: assistant file part
  • custom: assistant custom part (Kind + ProviderOptions)
  • tool-call: assistant tool-call part (provider-executed flag + options copied through)
  • tool-result (provider-executed): assistant tool-result part emitted at its input position
  • tool-result (client-executed): goes in the tool message
  • tool-approval-request: assistant tool-approval-request part emitted at its input position
  • tool-approval-response: tool message; if Approved == false, a synthetic execution-denied tool-result is appended for the matching call
  • sources / unknown types: skipped

The order of parts in the assistant message follows the input order: every provider-executed tool-result appears at its own position rather than being inlined immediately after the matching tool-call. This matches upstream's main-loop traversal and lets public callers control the on-wire ordering of interleaved text / reasoning / call / result sequences.

Invalid tool-call inputs that are not JSON objects (e.g. raw strings) are sanitized to {} to mirror upstream's invalid-call collapse.

The function does not perform any I/O and never returns an error: tool output normalization happens earlier via ToolResult.ModelOutput and the existing provider.ToolResultOutput shape, populated during execution in StreamText by dispatching the per-tool Tool.ToModelOutput hook.

The returned slice contains zero, one, or two messages: an assistant message (if any assistant content was produced) and a tool message (if any tool-results or approval responses were produced), in that order.

Compared to upstream's signature, this function does not take a ToolSet: the Go port runs Tool.ToModelOutput eagerly during tool execution and stores the result on ToolResult.ModelOutput, so by the time content reaches this helper the conversion is already done. Public callers constructing parts from raw tool output can call Tool.ToModelOutput directly.

func WriteAgentUIStream

func WriteAgentUIStream(w http.ResponseWriter, ctx context.Context, agent Agent, messages []UIMessage, opts ...UIMessageStreamOption) error

WriteAgentUIStream creates an Agent UI stream and writes it as SSE.

func WriteTextStream

func WriteTextStream(w http.ResponseWriter, result *StreamTextResult) error

WriteTextStream writes the text deltas from a StreamTextResult to an HTTP response as a plain text stream (for useCompletion).

func WriteUIMessageStream

func WriteUIMessageStream(w http.ResponseWriter, result *StreamTextResult, opts ...UIMessageStreamOption) error

WriteUIMessageStream is a shortcut that converts a StreamTextResult to a UIMessageChunk stream and pipes it to the HTTP response as SSE.

Types

type Agent

type Agent interface {
	Version() AgentVersion
	ID() string
	Tools() ToolSet
	Generate(ctx context.Context, opts ...AgentGenerateOption) (*GenerateTextResult, error)
	Stream(ctx context.Context, opts ...AgentStreamOption) *StreamTextResult
}

Agent is a reusable LLM agent.

Implementations wrap autonomous orchestration behind Generate and Stream while returning the same result types as GenerateText and StreamText. ToolLoopAgent implements this interface by delegating to the existing StreamText engine.

type AgentGenerateOption

type AgentGenerateOption interface {
	// contains filtered or unexported methods
}

AgentGenerateOption configures an Agent Generate call.

func WithAgentGenerateOptions

func WithAgentGenerateOptions(opts ...GenerateOption) AgentGenerateOption

WithAgentGenerateOptions applies GenerateText options to an Agent Generate call.

type AgentOption

type AgentOption interface {
	AgentStreamOption
	AgentGenerateOption
}

AgentOption configures both Agent Stream and Agent Generate calls.

func WithAgentMessages

func WithAgentMessages(messages ...UIMessage) AgentOption

WithAgentMessages sets UI messages for an Agent call.

func WithAgentModelMessages

func WithAgentModelMessages(messages ...provider.Message) AgentOption

WithAgentModelMessages sets provider model messages for an Agent call.

func WithAgentOptions

func WithAgentOptions(opts ...Option) AgentOption

WithAgentOptions applies shared StreamText/GenerateText options to an Agent call.

func WithAgentPrompt

func WithAgentPrompt(prompt string) AgentOption

WithAgentPrompt sets a user text prompt for an Agent call.

func WithAgentRuntimeContext

func WithAgentRuntimeContext(value any) AgentOption

WithAgentRuntimeContext sets per-call Agent runtime context.

type AgentStreamOption

type AgentStreamOption interface {
	// contains filtered or unexported methods
}

AgentStreamOption configures an Agent Stream call.

func WithAgentStreamOptions

func WithAgentStreamOptions(opts ...StreamOption) AgentStreamOption

WithAgentStreamOptions applies StreamText options to an Agent Stream call.

type AgentVersion

type AgentVersion string

AgentVersion identifies the version of the Agent interface implemented by an Agent.

const (
	// AgentVersionV1 is the upstream-compatible Agent interface version.
	AgentVersionV1 AgentVersion = "agent-v1"
)

type ChunkType

type ChunkType string

ChunkType identifies the type of a UIMessageChunk SSE event.

const (
	ChunkStart           ChunkType = "start"
	ChunkFinish          ChunkType = "finish"
	ChunkAbort           ChunkType = "abort"
	ChunkStartStep       ChunkType = "start-step"
	ChunkFinishStep      ChunkType = "finish-step"
	ChunkMessageMetadata ChunkType = "message-metadata"

	ChunkTextStart ChunkType = "text-start"
	ChunkTextDelta ChunkType = "text-delta"
	ChunkTextEnd   ChunkType = "text-end"

	ChunkReasoningStart ChunkType = "reasoning-start"
	ChunkReasoningDelta ChunkType = "reasoning-delta"
	ChunkReasoningEnd   ChunkType = "reasoning-end"
	ChunkReasoningFile  ChunkType = "reasoning-file"

	ChunkToolInputStart       ChunkType = "tool-input-start"
	ChunkToolInputDelta       ChunkType = "tool-input-delta"
	ChunkToolInputAvailable   ChunkType = "tool-input-available"
	ChunkToolInputError       ChunkType = "tool-input-error"
	ChunkToolApprovalRequest  ChunkType = "tool-approval-request"
	ChunkToolApprovalResponse ChunkType = "tool-approval-response"
	ChunkToolOutputDenied     ChunkType = "tool-output-denied"
	ChunkToolOutputAvailable  ChunkType = "tool-output-available"
	ChunkToolOutputError      ChunkType = "tool-output-error"

	ChunkSourceURL      ChunkType = "source-url"
	ChunkSourceDocument ChunkType = "source-document"

	ChunkFile  ChunkType = "file"
	ChunkData  ChunkType = "data"
	ChunkError ChunkType = "error"
)

type ContentPart

type ContentPart interface {
	// contains filtered or unexported methods
}

ContentPart is the interface for structured content in a StepResult. NOT the same as UIMessage Part -- this is the model output side. Consumers type-switch on: TextContent, ReasoningContent, SourceContent, FileContent, ReasoningFileContent, ToolCallContent, ToolApprovalRequestContent, ToolApprovalResponseContent, ToolResultContent.

type ConvertOption

type ConvertOption interface {
	// contains filtered or unexported methods
}

ConvertOption configures ConvertToModelMessages.

func WithIgnoreIncompleteToolCalls

func WithIgnoreIncompleteToolCalls() ConvertOption

WithIgnoreIncompleteToolCalls skips tool calls whose input is not complete when converting UI messages to model messages.

type CreateUIMessageStreamParams

type CreateUIMessageStreamParams struct {
	Execute          func(writer *UIMessageStreamWriter) error
	OnError          func(error) string
	OriginalMessages []UIMessage
	OnFinish         func(UIMessageStreamOnFinishState)
	GenerateID       func() string
}

CreateUIMessageStreamParams configures CreateUIMessageStream.

type DataPart

type DataPart struct {
	DataName string          `json:"dataName"`
	ID       string          `json:"id,omitempty"`
	Data     json.RawMessage `json:"data"`
}

DataPart carries custom data in a UIMessage. On the wire, it serializes as type "data-{DataName}".

func (DataPart) PartType

func (p DataPart) PartType() string

PartType implements Part.

type DynamicToolUIPart

type DynamicToolUIPart struct {
	ToolCallID             string                    `json:"toolCallId"`
	ToolName               string                    `json:"toolName"`
	State                  ToolInvocationState       `json:"state"`
	Input                  json.RawMessage           `json:"input,omitempty"`
	Output                 json.RawMessage           `json:"output,omitempty"`
	ErrorText              string                    `json:"errorText,omitempty"`
	ProviderExecuted       bool                      `json:"providerExecuted,omitempty"`
	Approval               *ToolApproval             `json:"approval,omitempty"`
	CallProviderMetadata   provider.ProviderMetadata `json:"callProviderMetadata,omitempty"`
	ResultProviderMetadata provider.ProviderMetadata `json:"resultProviderMetadata,omitempty"`
}

DynamicToolUIPart carries a dynamic/MCP tool invocation in a UIMessage. Same shape as ToolInvocationPart but with type "dynamic-tool" on the wire.

func (DynamicToolUIPart) PartType

func (DynamicToolUIPart) PartType() string

PartType implements Part.

type FileContent

type FileContent struct {
	File             GeneratedFile             `json:"file"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

FileContent represents a generated file.

type FilePart

type FilePart struct {
	MediaType         string                    `json:"mediaType"`
	URL               string                    `json:"url"`
	Filename          string                    `json:"filename,omitempty"`
	ProviderReference map[string]string         `json:"providerReference,omitempty"`
	ProviderMetadata  provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

FilePart carries file data in a UIMessage. ProviderReference maps provider names to provider-specific file IDs and, when present, takes precedence over URL during conversion to model messages.

func (FilePart) MarshalJSON

func (p FilePart) MarshalJSON() ([]byte, error)

func (FilePart) PartType

func (FilePart) PartType() string

PartType implements Part.

type GenerateOption

type GenerateOption interface {
	// contains filtered or unexported methods
}

GenerateOption configures a GenerateText call.

type GenerateTextResult

type GenerateTextResult struct {
	Text             string
	Reasoning        []ReasoningOutput
	ToolCalls        []ToolCall
	ToolResults      []ToolResult
	Files            []GeneratedFile
	Sources          []Source
	FinishReason     provider.FinishReason
	Usage            provider.Usage
	TotalUsage       provider.Usage
	Steps            []StepResult
	Warnings         []provider.Warning
	Content          []ContentPart
	Response         ResponseMetadata
	ProviderMetadata provider.ProviderMetadata
	Output           any
	OutputError      error
}

GenerateTextResult holds the complete result of a non-streaming LLM call.

func GenerateText

func GenerateText(ctx context.Context, model provider.LanguageModel, opts ...GenerateOption) (*GenerateTextResult, error)

GenerateText performs a non-streaming LLM call. It runs StreamText internally and blocks until the stream completes, returning a populated result or the first error encountered.

type GeneratedFile

type GeneratedFile struct {
	Data      []byte `json:"-"`
	Base64    string `json:"-"`
	MediaType string `json:"mediaType"`
}

GeneratedFile represents a file generated by the model.

func (GeneratedFile) DataURL

func (f GeneratedFile) DataURL() string

type IDGeneratorOptions

type IDGeneratorOptions struct {
	Prefix string
	Size   int
}

IDGeneratorOptions configures CreateIDGenerator.

type InvalidToolApprovalError

type InvalidToolApprovalError struct {
	// ApprovalID identifies the approval response that triggered the error.
	ApprovalID string
	// Reason is non-empty when the response is structurally invalid (for
	// example, missing `approved`) and empty when the approval ID has no
	// matching prior assistant request.
	Reason string
}

InvalidToolApprovalError is returned when a tool-approval-response cannot be correlated with a prior assistant approval request, or when the response itself is structurally invalid (for example, missing the required `approved` field).

Mirrors upstream's `InvalidToolApprovalError` from the Vercel AI SDK so callers can type-assert via errors.As and inspect InvalidToolApprovalError.ApprovalID.

func (*InvalidToolApprovalError) Error

func (e *InvalidToolApprovalError) Error() string

type InvalidToolApprovalSignatureError

type InvalidToolApprovalSignatureError struct {
	ApprovalID string
	ToolCallID string
	Reason     string
}

InvalidToolApprovalSignatureError is returned when a signed tool approval request is replayed without a valid signature.

func (*InvalidToolApprovalSignatureError) Error

type MissingToolResultsError

type MissingToolResultsError struct {
	// ToolCallIDs identifies every unresolved tool call in prompt order.
	ToolCallIDs []string
}

MissingToolResultsError is returned when a prompt contains non-provider-executed tool calls that have no corresponding tool results or approval responses.

func (*MissingToolResultsError) Error

func (e *MissingToolResultsError) Error() string

type OnAbortState

type OnAbortState struct {
	Steps []StepResult
}

OnAbortState is passed to the OnAbort callback when the stream is cancelled.

type OnChunkState

type OnChunkState struct {
	Chunk TextStreamPart
}

OnChunkState is passed to the OnChunk callback.

type OnFinishState

type OnFinishState struct {
	StepResult
	Steps      []StepResult
	TotalUsage provider.Usage
}

OnFinishState is passed to the OnFinish callback after all steps complete.

type OnStartState

type OnStartState struct{}

OnStartState is passed to the OnStart callback.

type OnStepFinishState

type OnStepFinishState struct {
	StepResult
}

OnStepFinishState is passed to the OnStepFinish callback.

type OnStepStartState

type OnStepStartState struct {
	StepNumber int
	Model      StepModel
}

OnStepStartState is passed to the OnStepStart callback.

type OnToolCallFinishState

type OnToolCallFinishState struct {
	StepNumber int
	Model      StepModel
	ToolCall   ToolCall
	Messages   []provider.Message
	DurationMs int64
	Success    bool
	Output     json.RawMessage
	Error      error
}

OnToolCallFinishState is passed to the OnToolCallFinish callback. Either Output+Success=true or Error+Success=false.

type OnToolCallStartState

type OnToolCallStartState struct {
	StepNumber int
	Model      StepModel
	ToolCall   ToolCall
	Messages   []provider.Message
}

OnToolCallStartState is passed to the OnToolCallStart callback.

type Option

type Option interface {
	StreamOption
	GenerateOption
}

Option is an option that applies to both StreamText and GenerateText.

func OnError

func OnError(fn func(error)) Option

OnError sets a callback invoked on errors.

func OnFinish

func OnFinish(fn func(OnFinishState)) Option

OnFinish sets a callback invoked once after all steps complete successfully.

func OnStart

func OnStart(fn func(OnStartState)) Option

OnStart sets a callback invoked when the stream starts.

func OnStepEnd

func OnStepEnd(fn func(OnStepFinishState)) Option

OnStepEnd sets a callback invoked when a step completes.

func OnStepFinish

func OnStepFinish(fn func(OnStepFinishState)) Option

OnStepFinish sets a callback invoked when a step completes.

func OnStepStart

func OnStepStart(fn func(OnStepStartState)) Option

OnStepStart sets a callback invoked at the start of each step.

func OnToolCallFinish

func OnToolCallFinish(fn func(OnToolCallFinishState)) Option

OnToolCallFinish sets a callback invoked after a tool finishes execution.

func OnToolCallStart

func OnToolCallStart(fn func(OnToolCallStartState)) Option

OnToolCallStart sets a callback invoked before a tool is executed.

func WithActiveTools

func WithActiveTools(names ...string) Option

WithActiveTools filters which tools are active for a call.

func WithFrequencyPenalty

func WithFrequencyPenalty(f float64) Option

WithFrequencyPenalty sets the frequency penalty.

func WithGenerateID

func WithGenerateID(fn func() string) Option

WithGenerateID sets the ID generator used by orchestration-created IDs.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders sets additional request headers.

func WithInstructions

func WithInstructions(text string) Option

WithInstructions sets a simple text instruction prompt.

func WithMaxOutputTokens

func WithMaxOutputTokens(n int) Option

WithMaxOutputTokens sets the maximum number of output tokens.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retry attempts for transient errors. Default is 2 (up to 3 total attempts). Set to 0 to disable retry.

func WithMessages

func WithMessages(msgs ...UIMessage) Option

WithMessages sets the UI messages for the conversation.

func WithModelMessages

func WithModelMessages(msgs ...provider.Message) Option

WithModelMessages sets provider messages directly, bypassing UI message conversion.

func WithOutput

func WithOutput(out Output) Option

WithOutput sets structured output processing.

func WithPrepareStep

func WithPrepareStep(fn PrepareStepFunc) Option

WithPrepareStep sets a per-step preparation callback.

func WithPresencePenalty

func WithPresencePenalty(p float64) Option

WithPresencePenalty sets the presence penalty.

func WithProviderOptions

func WithProviderOptions(opts ...provider.ProviderOption) Option

WithProviderOptions sets provider-specific options from typed values. Each value's ProviderKey() determines its map key; last value wins per key.

func WithReasoning

func WithReasoning(level provider.ReasoningEffort) Option

WithReasoning sets the reasoning effort level for the model.

func WithResponseFormat

func WithResponseFormat(f provider.ResponseFormat) Option

WithResponseFormat sets the response format.

func WithSeed

func WithSeed(s int) Option

WithSeed sets the random seed for deterministic sampling.

func WithStopSequences

func WithStopSequences(seqs ...string) Option

WithStopSequences sets the stop sequences.

func WithStopWhen

func WithStopWhen(conditions ...StopCondition) Option

WithStopWhen sets stop conditions for the multi-step loop.

func WithSystem

func WithSystem(text string) Option

WithSystem sets a simple text system prompt.

func WithSystemMessages

func WithSystemMessages(msgs ...SystemModelMessage) Option

WithSystemMessages sets multiple system prompt segments.

func WithTemperature

func WithTemperature(t float64) Option

WithTemperature sets the sampling temperature.

func WithTimeout

func WithTimeout(cfg TimeoutConfig) Option

WithTimeout sets the timeout configuration for the operation.

func WithToolApproval

func WithToolApproval(policy ToolApprovalPolicyConfig) Option

WithToolApproval sets call-level tool approval policy. It takes precedence over per-tool NeedsApproval configuration. If called more than once, the latest generic or per-tool policy replaces the previous one.

func WithToolApprovalSecret

func WithToolApprovalSecret(secret string) Option

WithToolApprovalSecret sets the secret used to sign and verify tool approval requests.

func WithToolApprovalSecretBytes

func WithToolApprovalSecretBytes(secret []byte) Option

WithToolApprovalSecretBytes sets the byte secret used to sign and verify tool approval requests.

func WithToolChoice

func WithToolChoice(tc provider.ToolChoice) Option

WithToolChoice sets the tool choice strategy.

func WithTopK

func WithTopK(k int) Option

WithTopK sets the top-k sampling parameter.

func WithTopP

func WithTopP(p float64) Option

WithTopP sets the top-p (nucleus) sampling parameter.

type Output

type Output interface {
	// ResponseFormat returns the provider response format configuration
	// to be sent with the LLM request.
	ResponseFormat() *provider.ResponseFormat

	// ParseComplete validates and parses the complete LLM response text.
	// Returns the parsed value (type depends on the Output mode) or an error
	// if the response is invalid.
	ParseComplete(text string) (any, error)

	// ParsePartial attempts a best-effort parse of incomplete JSON text
	// during streaming. Returns the partial value and true if parsing
	// succeeded, or (nil, false) if the text is not yet parseable.
	ParsePartial(text string) (any, bool)
}

Output defines a structured output specification that controls how LLM responses are formatted, parsed, and validated.

Implementations are provided by the output package: Object[T], Array[T], Choice, JSON, and Text.

type Part

type Part interface {
	PartType() string
}

Part is the interface for all UIMessage part types. Consumers type-switch on concrete types: TextPart, ReasoningPart, ToolInvocationPart, DynamicToolUIPart, FilePart, ReasoningFilePart, SourceURLPart, SourceDocumentPart, DataPart, StepStartPart.

type PrepareStepFunc

type PrepareStepFunc func(PrepareStepState) (*PrepareStepResult, error)

PrepareStepFunc is a callback that customizes each step before the provider call.

type PrepareStepResult

type PrepareStepResult struct {
	Model           provider.LanguageModel
	System          []SystemModelMessage
	ToolChoice      *provider.ToolChoice
	ActiveTools     []string
	Messages        []provider.Message
	ProviderOptions provider.ProviderOptions
	Context         any
}

PrepareStepResult contains per-step overrides from PrepareStep.

type PrepareStepState

type PrepareStepState struct {
	Steps      []StepResult
	StepNumber int
	Model      provider.LanguageModel
	// Messages contains the provider messages for the current step. PrepareStep
	// message overrides carry forward to later steps.
	Messages []provider.Message
	// InitialMessages contains the original input messages before configured
	// system messages and approval-generated tool results are added.
	InitialMessages []provider.Message
	// ResponseMessages contains response messages accumulated before the current
	// step, including approval-generated tool results from the initial input.
	ResponseMessages []provider.Message
}

PrepareStepState is passed to the PrepareStep callback.

type ReasoningContent

type ReasoningContent struct {
	Text             string                    `json:"text"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningContent represents generated reasoning text.

type ReasoningFileContent

type ReasoningFileContent struct {
	File             GeneratedFile             `json:"file"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningFileContent represents a file generated as part of model reasoning.

type ReasoningFileOutput

type ReasoningFileOutput struct {
	File             GeneratedFile             `json:"file"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningFileOutput represents a file generated as model reasoning.

type ReasoningFilePart

type ReasoningFilePart struct {
	MediaType        string                    `json:"mediaType"`
	URL              string                    `json:"url"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningFilePart carries a file generated as part of model reasoning.

func (ReasoningFilePart) PartType

func (ReasoningFilePart) PartType() string

PartType implements Part.

type ReasoningOutput

type ReasoningOutput interface {
	// contains filtered or unexported methods
}

ReasoningOutput is a text or file artifact generated as model reasoning.

type ReasoningPart

type ReasoningPart struct {
	Text             string                    `json:"text"`
	State            string                    `json:"state,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningPart carries model reasoning in a UIMessage.

func (ReasoningPart) PartType

func (ReasoningPart) PartType() string

PartType implements Part.

type ReasoningTextOutput

type ReasoningTextOutput struct {
	Text             string                    `json:"text"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ReasoningTextOutput represents generated reasoning text.

type ResponseMetadata

type ResponseMetadata struct {
	provider.ResponseMetadata
	Headers  map[string]string  `json:"headers,omitempty"`
	Messages []provider.Message `json:"-"`
}

ResponseMetadata carries metadata about the provider response.

Messages holds the assistant + tool messages produced from this step's content via ToResponseMessages. It mirrors upstream's `result.response.messages` semantics — the next-call message tail that consumers can append to their prompt to continue the conversation. The field is in-process only (json:"-") and is not transported on any wire.

type RetryError

type RetryError struct {
	Reason RetryErrorReason
	Errors []error
}

RetryError is returned when all retry attempts have been exhausted or a non-retryable error is encountered after retries have already started. It carries every error from each attempt and the reason retries stopped.

func (*RetryError) Error

func (e *RetryError) Error() string

func (*RetryError) LastError

func (e *RetryError) LastError() error

func (*RetryError) Unwrap

func (e *RetryError) Unwrap() error

type RetryErrorReason

type RetryErrorReason string

RetryErrorReason describes why retry stopped.

const (
	RetryMaxRetriesExceeded RetryErrorReason = "maxRetriesExceeded"
	RetryErrorNotRetryable  RetryErrorReason = "errorNotRetryable"
)

type Role

type Role = provider.Role

Role is an alias for provider.Role, used on UIMessage. UIMessages use system, user, and assistant roles.

type SingleToolApprovalFunc

type SingleToolApprovalFunc func(input json.RawMessage, opts ToolExecutionOptions) (ToolApprovalDecision, error)

SingleToolApprovalFunc decides approval policy for a specific tool.

type Source

type Source struct {
	SourceType       provider.SourceType       `json:"sourceType"`
	ID               string                    `json:"id,omitempty"`
	URL              string                    `json:"url,omitempty"`
	Title            string                    `json:"title,omitempty"`
	MediaType        string                    `json:"mediaType,omitempty"`
	Filename         string                    `json:"filename,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

Source represents a reference source (URL or document) in the generated output.

type SourceContent

type SourceContent struct {
	Source Source `json:"source"`
}

SourceContent represents a reference source.

type SourceDocumentPart

type SourceDocumentPart struct {
	SourceID         string                    `json:"sourceId"`
	MediaType        string                    `json:"mediaType"`
	Title            string                    `json:"title,omitempty"`
	Filename         string                    `json:"filename,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

SourceDocumentPart carries a document source reference in a UIMessage.

func (SourceDocumentPart) PartType

func (SourceDocumentPart) PartType() string

PartType implements Part.

type SourceURLPart

type SourceURLPart struct {
	SourceID         string                    `json:"sourceId"`
	URL              string                    `json:"url"`
	Title            string                    `json:"title,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

SourceURLPart carries a URL source reference in a UIMessage.

func (SourceURLPart) PartType

func (SourceURLPart) PartType() string

PartType implements Part.

type StepModel

type StepModel struct {
	Provider string `json:"provider"`
	ModelID  string `json:"modelId"`
}

StepModel identifies which provider and model were used for a step.

type StepResult

type StepResult struct {
	StepNumber    int               `json:"stepNumber"`
	StepType      StepType          `json:"stepType"`
	Model         StepModel         `json:"model"`
	Text          string            `json:"text"`
	ReasoningText string            `json:"reasoningText,omitempty"`
	Reasoning     []ReasoningOutput `json:"reasoning,omitempty"`
	Content       []ContentPart     `json:"-"`

	ToolCalls             []ToolCall                `json:"toolCalls,omitempty"`
	ToolApprovalRequests  []ToolApprovalRequest     `json:"toolApprovalRequests,omitempty"`
	ToolApprovalResponses []ToolApprovalResponse    `json:"toolApprovalResponses,omitempty"`
	ToolResults           []ToolResult              `json:"toolResults,omitempty"`
	Files                 []GeneratedFile           `json:"files,omitempty"`
	Sources               []Source                  `json:"sources,omitempty"`
	FinishReason          provider.FinishReason     `json:"finishReason"`
	IsContinued           bool                      `json:"isContinued,omitempty"`
	Usage                 provider.Usage            `json:"usage"`
	Warnings              []provider.Warning        `json:"warnings,omitempty"`
	Request               provider.RequestMetadata  `json:"request,omitempty"`
	Response              ResponseMetadata          `json:"response,omitempty"`
	ProviderMetadata      provider.ProviderMetadata `json:"providerMetadata,omitempty"`
	// contains filtered or unexported fields
}

StepResult contains per-step data from the StreamText orchestration loop.

type StepStartPart

type StepStartPart struct{}

StepStartPart marks the beginning of a new step in a UIMessage.

func (StepStartPart) PartType

func (StepStartPart) PartType() string

PartType implements Part.

type StepType

type StepType string

StepType identifies the kind of orchestration step.

const (
	StepTypeInitial    StepType = "initial"
	StepTypeToolResult StepType = "tool-result"
)

type StopCondition

type StopCondition func(state StopConditionState) bool

StopCondition is a function that determines whether the step loop should stop.

func HasToolCall

func HasToolCall(toolName string) StopCondition

HasToolCall returns a StopCondition that stops when the named tool is called.

func StepCountIs

func StepCountIs(n int) StopCondition

StepCountIs returns a StopCondition that stops after n steps.

type StopConditionState

type StopConditionState struct {
	Steps []StepResult
}

StopConditionState is passed to stop condition functions.

type StreamAbort

type StreamAbort struct {
	Reason string
}

StreamAbort signals that the stream was cancelled.

type StreamError

type StreamError struct {
	Error error
}

StreamError carries an error from the provider or orchestration.

type StreamFile

type StreamFile struct {
	File             GeneratedFile
	ProviderMetadata provider.ProviderMetadata
}

StreamFile carries a file generated by the model.

type StreamFinish

type StreamFinish struct {
	FinishReason provider.FinishReason
	TotalUsage   *provider.Usage
}

StreamFinish signals the end of the entire StreamText orchestration.

type StreamFinishStep

type StreamFinishStep struct {
	Response         *provider.ResponseMetadata
	Usage            *provider.Usage
	FinishReason     provider.FinishReason
	ProviderMetadata provider.ProviderMetadata
}

StreamFinishStep signals the end of a step with usage and metadata.

type StreamOption

type StreamOption interface {
	// contains filtered or unexported methods
}

StreamOption configures a StreamText call.

func OnAbort

func OnAbort(fn func(OnAbortState)) StreamOption

OnAbort sets a callback invoked when the stream is cancelled via context. Only available for StreamText.

func OnChunk

func OnChunk(fn func(OnChunkState)) StreamOption

OnChunk sets a callback invoked for each streaming chunk. Only available for StreamText.

func WithIncludeRawChunks

func WithIncludeRawChunks() StreamOption

WithIncludeRawChunks enables raw provider chunks in the stream. Only available for StreamText.

type StreamRaw

type StreamRaw struct {
	RawValue json.RawMessage
}

StreamRaw carries unprocessed provider data (when IncludeRawChunks is set).

type StreamReasoningDelta

type StreamReasoningDelta struct {
	ID               string
	Text             string
	ProviderMetadata provider.ProviderMetadata
}

StreamReasoningDelta carries incremental reasoning within a block.

type StreamReasoningEnd

type StreamReasoningEnd struct {
	ID               string
	ProviderMetadata provider.ProviderMetadata
}

StreamReasoningEnd closes a reasoning block.

type StreamReasoningFile

type StreamReasoningFile struct {
	File             GeneratedFile
	ProviderMetadata provider.ProviderMetadata
}

StreamReasoningFile carries a file generated as part of model reasoning.

type StreamReasoningStart

type StreamReasoningStart struct {
	ID               string
	ProviderMetadata provider.ProviderMetadata
}

StreamReasoningStart opens a reasoning block.

type StreamSource

type StreamSource struct {
	Source Source
}

StreamSource carries a reference source (URL or document).

type StreamStart

type StreamStart struct{}

StreamStart signals the beginning of a StreamText orchestration.

type StreamStartStep

type StreamStartStep struct {
	Warnings []provider.Warning
	Request  *provider.RequestMetadata
}

StreamStartStep signals the beginning of a new step in the orchestration loop.

type StreamTextDelta

type StreamTextDelta struct {
	ID               string
	Text             string
	ProviderMetadata provider.ProviderMetadata
}

StreamTextDelta carries incremental text within a block.

type StreamTextEnd

type StreamTextEnd struct {
	ID               string
	ProviderMetadata provider.ProviderMetadata
}

StreamTextEnd closes a text block.

type StreamTextResult

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

StreamTextResult holds the result of a StreamText call. It provides both a streaming interface (FullStream) and blocking accessors that resolve after the stream completes.

func StreamText

func StreamText(ctx context.Context, model provider.LanguageModel, opts ...StreamOption) *StreamTextResult

StreamText starts an LLM streaming call with multi-step tool execution. It returns immediately; consume events via FullStream() or convert to SSE via ToUIMessageStream(). Call Wait() or use blocking accessors to wait for completion.

func (*StreamTextResult) AggregateUsage

func (r *StreamTextResult) AggregateUsage() provider.Usage

AggregateUsage returns the aggregate token usage across all steps. Blocks until the stream completes.

func (*StreamTextResult) Content

func (r *StreamTextResult) Content() []ContentPart

Content returns the structured content from the last step. Blocks until the stream completes.

func (*StreamTextResult) ElementStream

func (r *StreamTextResult) ElementStream() <-chan json.RawMessage

ElementStream returns a channel of complete validated array elements during streaming. Only produces values when the Output is in array mode. Elements are delivered exactly once and in order. The channel is closed after the stream finishes and all elements are delivered.

func (*StreamTextResult) Err

func (r *StreamTextResult) Err() error

Err returns the first error encountered during streaming, or nil. Blocks until the stream completes.

func (*StreamTextResult) Files

func (r *StreamTextResult) Files() []GeneratedFile

Files returns the generated files from the last step. Blocks until the stream completes.

func (*StreamTextResult) FinishReason

func (r *StreamTextResult) FinishReason() provider.FinishReason

FinishReason returns the finish reason from the last step. Blocks until the stream completes.

func (*StreamTextResult) FullStream

func (r *StreamTextResult) FullStream() <-chan TextStreamPart

FullStream returns a channel of TextStreamPart events. The channel is closed when the stream completes. Only one consumer may read the stream; a second call to FullStream or ToUIMessageStream returns a closed (empty) channel.

func (*StreamTextResult) OutputError

func (r *StreamTextResult) OutputError() error

OutputError returns the structured output validation error, or nil if validation succeeded or no Output was specified. Blocks until the stream completes.

func (*StreamTextResult) OutputValue

func (r *StreamTextResult) OutputValue() any

OutputValue returns the parsed structured output value, or nil if no Output was specified or parsing failed. Blocks until the stream completes.

func (*StreamTextResult) PartialOutputStream

func (r *StreamTextResult) PartialOutputStream() <-chan json.RawMessage

PartialOutputStream returns a channel of partial JSON snapshots during streaming. Each emission is a json.RawMessage representing the partial parse of the accumulated text so far. Emissions are delivered exactly once and in order. The channel is closed after the stream finishes and all emissions are delivered. If no Output is set, returns a closed channel.

func (*StreamTextResult) ProviderMetadata

func (r *StreamTextResult) ProviderMetadata() provider.ProviderMetadata

ProviderMetadata returns the provider metadata from the last step. Blocks until the stream completes.

func (*StreamTextResult) Reasoning

func (r *StreamTextResult) Reasoning() []ReasoningOutput

Reasoning returns the reasoning output from the last step. Blocks until the stream completes.

func (*StreamTextResult) Response

func (r *StreamTextResult) Response() ResponseMetadata

Response returns the response metadata from the last step. Blocks until the stream completes.

func (*StreamTextResult) Sources

func (r *StreamTextResult) Sources() []Source

Sources returns the sources from the last step. Blocks until the stream completes.

func (*StreamTextResult) Steps

func (r *StreamTextResult) Steps() []StepResult

Steps returns all step results. Blocks until the stream completes.

func (*StreamTextResult) Stream

func (r *StreamTextResult) Stream() <-chan TextStreamPart

Stream returns a channel of TextStreamPart events.

func (*StreamTextResult) Text

func (r *StreamTextResult) Text() string

Text returns the generated text from the last step. Blocks until the stream completes.

func (*StreamTextResult) ToUIMessageStream

func (r *StreamTextResult) ToUIMessageStream(opts ...UIMessageStreamOption) <-chan UIMessageChunk

ToUIMessageStream converts the internal event stream to UIMessageChunk events suitable for SSE output to @ai-sdk/react hooks.

func (*StreamTextResult) ToolCalls

func (r *StreamTextResult) ToolCalls() []ToolCall

ToolCalls returns the tool calls from the last step. Blocks until the stream completes.

func (*StreamTextResult) ToolResults

func (r *StreamTextResult) ToolResults() []ToolResult

ToolResults returns the tool results from the last step. Blocks until the stream completes.

func (*StreamTextResult) TotalUsage

func (r *StreamTextResult) TotalUsage() provider.Usage

TotalUsage returns the aggregate token usage across all steps. Blocks until the stream completes.

func (*StreamTextResult) Usage

func (r *StreamTextResult) Usage() provider.Usage

Usage returns the aggregate token usage across all steps. Blocks until the stream completes.

func (*StreamTextResult) Wait

func (r *StreamTextResult) Wait()

Wait blocks until the stream completes.

func (*StreamTextResult) Warnings

func (r *StreamTextResult) Warnings() []provider.Warning

Warnings returns all warnings accumulated across steps. Blocks until the stream completes.

type StreamTextStart

type StreamTextStart struct {
	ID               string
	ProviderMetadata provider.ProviderMetadata
}

StreamTextStart opens a text block.

type StreamToolApprovalRequest

type StreamToolApprovalRequest struct {
	ApprovalID       string
	ToolCallID       string
	ToolName         string
	Input            json.RawMessage
	Signature        string
	ProviderExecuted bool
	Dynamic          *bool
	Title            string
	IsAutomatic      bool
	ProviderMetadata provider.ProviderMetadata
}

StreamToolApprovalRequest carries a request for user approval before tool execution.

type StreamToolApprovalResponse

type StreamToolApprovalResponse struct {
	ApprovalID       string
	ToolCallID       string
	ToolName         string
	Approved         bool
	Reason           string
	ProviderExecuted bool
	Dynamic          *bool
	Title            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolApprovalResponse carries a user approval or denial decision.

type StreamToolCall

type StreamToolCall struct {
	ToolCallID       string
	ToolName         string
	Input            json.RawMessage
	ProviderExecuted bool
	Dynamic          *bool
	Title            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolCall carries a complete tool call (input parsed from string).

type StreamToolError

type StreamToolError struct {
	ToolCallID       string
	ToolName         string
	Input            json.RawMessage
	Error            error
	ProviderExecuted bool
	Dynamic          *bool
	Title            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolError carries a tool execution failure.

type StreamToolInputDelta

type StreamToolInputDelta struct {
	ID               string
	Delta            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolInputDelta carries incremental tool arguments.

type StreamToolInputEnd

type StreamToolInputEnd struct {
	ID               string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolInputEnd closes tool argument streaming.

type StreamToolInputStart

type StreamToolInputStart struct {
	ID               string
	ToolName         string
	ProviderExecuted bool
	Dynamic          *bool
	Title            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolInputStart opens tool argument streaming.

type StreamToolOutputDenied

type StreamToolOutputDenied struct {
	ToolCallID string
	ToolName   string
}

StreamToolOutputDenied carries a denied tool execution marker.

type StreamToolResult

type StreamToolResult struct {
	ToolCallID       string
	ToolName         string
	Input            json.RawMessage
	Output           json.RawMessage
	IsError          bool
	ProviderExecuted bool
	Dynamic          *bool
	Preliminary      bool
	Title            string
	ProviderMetadata provider.ProviderMetadata
}

StreamToolResult carries a tool execution result (local or provider-executed).

type SystemModelMessage

type SystemModelMessage struct {
	Content         string                   `json:"content"`
	ProviderOptions provider.ProviderOptions `json:"providerOptions,omitempty"`
}

SystemModelMessage represents a segment of the system prompt.

type TextContent

type TextContent struct {
	Text             string                    `json:"text"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

TextContent represents generated text content.

type TextPart

type TextPart struct {
	Text             string                    `json:"text"`
	State            string                    `json:"state,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

TextPart carries text content in a UIMessage.

func (TextPart) PartType

func (TextPart) PartType() string

PartType implements Part.

type TextStreamPart

type TextStreamPart interface {
	// contains filtered or unexported methods
}

TextStreamPart is the interface for all orchestration-level stream events. StreamText emits these on FullStream(). Consumers type-switch to handle specific event types.

This is the middle layer between provider.StreamPart (raw provider events) and UIMessageChunk (SSE wire format).

type TimeoutConfig

type TimeoutConfig struct {
	Total      time.Duration
	Step       time.Duration
	FirstChunk time.Duration
	Chunk      time.Duration
}

TimeoutConfig configures timeout levels for StreamText and GenerateText. All fields default to zero (disabled).

type Tool

type Tool struct {
	Type UserToolType               // "" or UserToolFunction for user-defined tools, UserToolProvider for provider tools
	ID   string                     // provider tool identifier (e.g. "anthropic.web_search_20250305"), only for provider tools
	Args map[string]json.RawMessage // provider-specific tool configuration, only for provider tools

	Description     string
	Title           string
	InputSchema     schema.Schema
	OutputSchema    schema.Schema
	InputExamples   []json.RawMessage
	Strict          *bool
	ProviderOptions provider.ProviderOptions

	Execute       ToolExecuteFunc
	NeedsApproval *ToolApprovalConfig
	ValidateInput func(input json.RawMessage) error
	ToModelOutput func(ToolOutputContext) (*provider.ToolResultOutput, error)

	OnInputStart     func(ToolExecutionOptions)
	OnInputDelta     func(inputTextDelta string, opts ToolExecutionOptions)
	OnInputAvailable func(input json.RawMessage, opts ToolExecutionOptions)
}

Tool defines a tool that a language model can call.

For function tools (Type "" or UserToolFunction), the tool is defined by the user with InputSchema, Execute, etc.

For provider tools (Type UserToolProvider), the tool's schema and behavior are defined by the provider. Set ID to the provider tool identifier (e.g. "anthropic.web_search_20250305") and Args for tool-specific config. Callbacks (OnInputStart, OnInputAvailable, etc.) are still supported.

Execute is optional: nil means tool calls are returned to the caller without execution (external/remote tool pattern).

func TypedTool

func TypedTool[I, O any](def TypedToolDef[I, O]) (tool Tool, err error)

TypedTool constructs a Tool from a typed definition. The input JSON Schema is derived from type I via schema.SchemaFor[I]() at construction time. The returned Tool.Execute wraps the typed execute function with automatic JSON unmarshal of input and marshal of output.

type ToolApproval

type ToolApproval struct {
	ID          string `json:"id,omitempty"`
	Approved    *bool  `json:"approved,omitempty"`
	Reason      string `json:"reason,omitempty"`
	IsAutomatic bool   `json:"isAutomatic,omitempty"`
	Signature   string `json:"signature,omitempty"`
}

ToolApproval carries approval status for a tool invocation. Mirrors the upstream `approval` object on `ToolUIPart` (see Vercel AI SDK `ui-messages.ts`), where ID identifies the approval request and Approved is unset while a request is pending and set true/false once the user responds.

type ToolApprovalConfig

type ToolApprovalConfig struct {
	Required bool
	Check    ToolNeedsApprovalFunc
}

ToolApprovalConfig configures whether a tool invocation requires approval.

func ApprovalIf

ApprovalIf returns a dynamic approval configuration evaluated per tool call.

func ApprovalRequired

func ApprovalRequired() *ToolApprovalConfig

ApprovalRequired returns a static approval configuration that always requires approval.

type ToolApprovalDecision

type ToolApprovalDecision struct {
	Status ToolApprovalStatus
	Reason string
}

ToolApprovalDecision is the normalized approval policy result.

type ToolApprovalFunc

type ToolApprovalFunc func(ToolApprovalOptions) (ToolApprovalDecision, error)

ToolApprovalFunc decides approval policy for any tool call in a request.

type ToolApprovalMap

type ToolApprovalMap map[string]ToolApprovalPolicy

ToolApprovalMap configures call-level approval policies by tool name.

type ToolApprovalOptions

type ToolApprovalOptions struct {
	ToolCall ToolCall
	Tools    ToolSet
	Messages []provider.Message
	Context  any
}

ToolApprovalOptions carries context to a generic approval policy function.

type ToolApprovalPolicy

type ToolApprovalPolicy struct {
	Decision ToolApprovalDecision
	Check    SingleToolApprovalFunc
}

ToolApprovalPolicy configures call-level approval for a single tool.

func ApprovalPolicy

func ApprovalPolicy(status ToolApprovalStatus, reason ...string) ToolApprovalPolicy

ApprovalPolicy returns a static call-level approval policy.

func ApprovalPolicyFunc

func ApprovalPolicyFunc(fn SingleToolApprovalFunc) ToolApprovalPolicy

ApprovalPolicyFunc returns a dynamic call-level approval policy.

type ToolApprovalPolicyConfig

type ToolApprovalPolicyConfig interface {
	// contains filtered or unexported methods
}

ToolApprovalPolicyConfig is accepted by WithToolApproval.

type ToolApprovalRequest

type ToolApprovalRequest struct {
	ApprovalID       string                    `json:"approvalId"`
	ToolCallID       string                    `json:"toolCallId"`
	ToolName         string                    `json:"toolName"`
	Input            json.RawMessage           `json:"input,omitempty"`
	Signature        string                    `json:"signature,omitempty"`
	ProviderExecuted bool                      `json:"providerExecuted,omitempty"`
	Dynamic          *bool                     `json:"dynamic,omitempty"`
	Title            string                    `json:"title,omitempty"`
	IsAutomatic      bool                      `json:"isAutomatic,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ToolApprovalRequest represents a request for user approval before executing a tool.

type ToolApprovalRequestContent

type ToolApprovalRequestContent struct {
	ToolApprovalRequest
}

ToolApprovalRequestContent represents a request for approval before tool execution.

type ToolApprovalResponse

type ToolApprovalResponse struct {
	ApprovalID       string                    `json:"approvalId"`
	ToolCallID       string                    `json:"toolCallId"`
	ToolName         string                    `json:"toolName"`
	Approved         bool                      `json:"approved"`
	Reason           string                    `json:"reason,omitempty"`
	ProviderExecuted bool                      `json:"providerExecuted,omitempty"`
	Dynamic          *bool                     `json:"dynamic,omitempty"`
	Title            string                    `json:"title,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ToolApprovalResponse represents a user's approval or denial of a tool request.

type ToolApprovalResponseContent

type ToolApprovalResponseContent struct {
	ToolApprovalResponse
}

ToolApprovalResponseContent represents an approval decision for a tool call.

type ToolApprovalStatus

type ToolApprovalStatus string

ToolApprovalStatus identifies the approval policy result for a tool call.

const (
	ToolApprovalNotApplicable ToolApprovalStatus = "not-applicable"
	ToolApprovalUserApproval  ToolApprovalStatus = "user-approval"
	ToolApprovalApproved      ToolApprovalStatus = "approved"
	ToolApprovalDenied        ToolApprovalStatus = "denied"
)

type ToolCall

type ToolCall struct {
	ToolCallID       string                    `json:"toolCallId"`
	ToolName         string                    `json:"toolName"`
	Input            json.RawMessage           `json:"input"`
	Invalid          bool                      `json:"-"`
	ProviderExecuted bool                      `json:"providerExecuted,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
	Dynamic          *bool                     `json:"dynamic,omitempty"`
	Title            string                    `json:"title,omitempty"`
}

ToolCall represents a complete tool call from the model.

type ToolCallContent

type ToolCallContent struct {
	ToolCallID       string                    `json:"toolCallId"`
	ToolName         string                    `json:"toolName"`
	Input            json.RawMessage           `json:"input"`
	ProviderExecuted bool                      `json:"providerExecuted,omitempty"`
	Dynamic          *bool                     `json:"dynamic,omitempty"`
	Title            string                    `json:"title,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ToolCallContent represents a tool call in the generated content.

type ToolCallNotFoundForApprovalError

type ToolCallNotFoundForApprovalError struct {
	ToolCallID string
	ApprovalID string
}

ToolCallNotFoundForApprovalError is returned when an approval request's referenced tool call cannot be found in any prior assistant message.

Mirrors upstream's `ToolCallNotFoundForApprovalError` from the Vercel AI SDK so callers can type-assert via errors.As and inspect both ToolCallNotFoundForApprovalError.ToolCallID and ToolCallNotFoundForApprovalError.ApprovalID.

func (*ToolCallNotFoundForApprovalError) Error

type ToolDrift

type ToolDrift struct {
	Added   []string `json:"added"`
	Removed []string `json:"removed"`
	Changed []string `json:"changed"`
}

ToolDrift describes differences between two tool fingerprint maps.

func DetectToolDrift

func DetectToolDrift(current, baseline map[string]string) ToolDrift

DetectToolDrift compares current tool fingerprints with a trusted baseline.

type ToolExecuteFunc

type ToolExecuteFunc func(ctx context.Context, input json.RawMessage, opts ToolExecutionOptions) (json.RawMessage, error)

ToolExecuteFunc is the signature for a tool's execution function. It receives the parsed input and returns the tool output as JSON.

type ToolExecutionOptions

type ToolExecutionOptions struct {
	ToolCallID string
	Messages   []provider.Message
	Context    any // user-defined context (experimental_context in AI SDK)
}

ToolExecutionOptions carries context into tool execution.

type ToolInvocationPart

type ToolInvocationPart struct {
	ToolCallID             string                    `json:"toolCallId"`
	ToolName               string                    `json:"toolName"`
	State                  ToolInvocationState       `json:"state"`
	Input                  json.RawMessage           `json:"input,omitempty"`
	Output                 json.RawMessage           `json:"output,omitempty"`
	ErrorText              string                    `json:"errorText,omitempty"`
	ProviderExecuted       bool                      `json:"providerExecuted,omitempty"`
	Approval               *ToolApproval             `json:"approval,omitempty"`
	CallProviderMetadata   provider.ProviderMetadata `json:"callProviderMetadata,omitempty"`
	ResultProviderMetadata provider.ProviderMetadata `json:"resultProviderMetadata,omitempty"`
}

ToolInvocationPart carries a tool invocation lifecycle in a UIMessage.

func (ToolInvocationPart) PartType

func (p ToolInvocationPart) PartType() string

PartType implements Part.

type ToolInvocationState

type ToolInvocationState string

ToolInvocationState identifies the lifecycle state of a tool invocation.

const (
	ToolStateInputStreaming    ToolInvocationState = "input-streaming"
	ToolStateInputAvailable    ToolInvocationState = "input-available"
	ToolStateApprovalRequested ToolInvocationState = "approval-requested"
	ToolStateApprovalResponded ToolInvocationState = "approval-responded"
	ToolStateOutputAvailable   ToolInvocationState = "output-available"
	ToolStateOutputError       ToolInvocationState = "output-error"
	ToolStateOutputDenied      ToolInvocationState = "output-denied"
)

type ToolLoopAgent

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

ToolLoopAgent is the built-in Agent implementation backed by StreamText and GenerateText.

ToolLoopAgent stores reusable settings and merges them with per-call options for each Generate or Stream call. It defaults to StepCountIs(20) when no stop condition is configured, while direct StreamText calls keep their one-step default. Reusable and per-call lifecycle callbacks compose in reusable-then-call order and inherit StreamText's concurrency behavior.

Agent runtime context is a wrapper-level Go adaptation of upstream runtimeContext: the resolved value is propagated through PrepareStepResult.Context and ToolExecutionOptions.Context. A nil PrepareStepResult.Context preserves the resolved Agent context because the current result shape cannot distinguish an omitted context from an intentional nil clear.

The Agent user-agent marker is added only to provider.CallOptions headers; provider modules must honor call headers for it to reach the network. OpenAI Responses currently does not honor call headers, and provider default User-Agent append semantics require separate provider work.

Example
_ = NewToolLoopAgent(nil,
	WithToolLoopAgentID("assistant"),
	WithToolLoopAgentOptions(WithInstructions("Be concise.")),
)
fmt.Println(AgentVersionV1)
Output:
agent-v1

func NewToolLoopAgent

func NewToolLoopAgent(model provider.LanguageModel, opts ...ToolLoopAgentOption) *ToolLoopAgent

NewToolLoopAgent constructs a ToolLoopAgent for model.

Construction records settings only and does not call the provider.

func (*ToolLoopAgent) Generate

Generate runs an Agent call to completion.

func (*ToolLoopAgent) ID

func (a *ToolLoopAgent) ID() string

ID returns the optional Agent ID.

func (*ToolLoopAgent) Stream

Stream starts an Agent stream call.

func (*ToolLoopAgent) Tools

func (a *ToolLoopAgent) Tools() ToolSet

Tools returns a copy of the reusable tool set configured on the Agent.

func (*ToolLoopAgent) Version

func (a *ToolLoopAgent) Version() AgentVersion

Version returns agent-v1.

type ToolLoopAgentOption

type ToolLoopAgentOption interface {
	// contains filtered or unexported methods
}

ToolLoopAgentOption configures a ToolLoopAgent.

func WithToolLoopAgentID

func WithToolLoopAgentID(id string) ToolLoopAgentOption

WithToolLoopAgentID sets the optional Agent ID.

func WithToolLoopAgentOptions

func WithToolLoopAgentOptions(opts ...StreamOption) ToolLoopAgentOption

WithToolLoopAgentOptions adds reusable StreamText/GenerateText settings to a ToolLoopAgent.

Shared options such as WithTools, WithInstructions, WithStopWhen, and callbacks apply to both Stream and Generate. Stream-only options such as OnChunk apply only to Stream calls.

func WithToolLoopAgentRuntimeContext

func WithToolLoopAgentRuntimeContext(value any) ToolLoopAgentOption

WithToolLoopAgentRuntimeContext sets reusable Agent runtime context.

The context is delivered to tools through the existing PrepareStepResult.Context and ToolExecutionOptions.Context path. A per-call Agent runtime context overrides this value only when supplied.

type ToolNeedsApprovalFunc

type ToolNeedsApprovalFunc func(input json.RawMessage, opts ToolExecutionOptions) (bool, error)

ToolNeedsApprovalFunc decides whether a tool invocation requires user approval.

type ToolNotExecutableError

type ToolNotExecutableError struct {
	ToolName string
}

ToolNotExecutableError is returned when an approved tool call cannot be executed locally because the tool is unknown to the current invocation or has no `Execute` function configured.

func (*ToolNotExecutableError) Error

func (e *ToolNotExecutableError) Error() string

type ToolOption

type ToolOption interface {
	Option
	ConvertOption
}

ToolOption configures tools for text generation and UI message conversion.

func WithTools

func WithTools(tools ToolSet) ToolOption

WithTools sets the available tools.

type ToolOutputContext

type ToolOutputContext struct {
	ToolCallID string
	Input      json.RawMessage
	Output     json.RawMessage
}

ToolOutputContext is passed to Tool.ToModelOutput for converting tool output to the provider's expected format.

type ToolResult

type ToolResult struct {
	ToolCallID       string                     `json:"toolCallId"`
	ToolName         string                     `json:"toolName"`
	Input            json.RawMessage            `json:"input"`
	Output           json.RawMessage            `json:"output"`
	ModelOutput      *provider.ToolResultOutput `json:"-"`
	IsError          bool                       `json:"isError,omitempty"`
	ProviderExecuted bool                       `json:"providerExecuted,omitempty"`
	ProviderMetadata provider.ProviderMetadata  `json:"providerMetadata,omitempty"`
	Dynamic          *bool                      `json:"dynamic,omitempty"`
	Preliminary      bool                       `json:"preliminary,omitempty"`
	Title            string                     `json:"title,omitempty"`
}

ToolResult represents a tool execution result.

type ToolResultContent

type ToolResultContent struct {
	ToolCallID       string                    `json:"toolCallId"`
	ToolName         string                    `json:"toolName"`
	Input            json.RawMessage           `json:"input"`
	Output           json.RawMessage           `json:"output"`
	ProviderExecuted bool                      `json:"providerExecuted,omitempty"`
	Dynamic          *bool                     `json:"dynamic,omitempty"`
	Preliminary      bool                      `json:"preliminary,omitempty"`
	Title            string                    `json:"title,omitempty"`
	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
}

ToolResultContent represents a tool execution result in the generated content.

type ToolSet

type ToolSet map[string]Tool

ToolSet is a named collection of tools. Tools are keyed by name.

type TypedToolDef

type TypedToolDef[I, O any] struct {
	Name        string
	Description string
	Title       string
	Execute     func(ctx context.Context, input I, opts ToolExecutionOptions) (O, error)

	OutputSchema    schema.Schema
	InputExamples   []I
	Strict          *bool
	ProviderOptions provider.ProviderOptions
	ValidateInput   func(input I) error
	ToModelOutput   func(toolCallID string, input I, output O) (*provider.ToolResultOutput, error)

	OnInputStart     func(ToolExecutionOptions)
	OnInputDelta     func(inputTextDelta string, opts ToolExecutionOptions)
	OnInputAvailable func(input I, err error, opts ToolExecutionOptions)
}

TypedToolDef defines a tool using Go types instead of raw JSON. The input schema is derived from I via schema.SchemaFor[I]() and marshal/unmarshal is handled internally, so the Execute function works with typed input and output directly.

type UIMessage

type UIMessage struct {
	ID       string          `json:"id"`
	Role     Role            `json:"role"`
	Parts    []Part          `json:"parts"`
	Metadata json.RawMessage `json:"metadata,omitempty"`
}

UIMessage is the canonical message type for the AI SDK UI protocol. It is JSON-serializable for persistence and wire transfer.

func AssembleUIMessage

func AssembleUIMessage(stream <-chan UIMessageChunk, opts ...UIMessageReaderOption) (UIMessage, error)

AssembleUIMessage reads a UIMessageChunk stream to completion and returns the final assembled assistant message.

func (UIMessage) MarshalJSON

func (m UIMessage) MarshalJSON() ([]byte, error)

func (*UIMessage) UnmarshalJSON

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

type UIMessageChunk

type UIMessageChunk struct {
	Type ChunkType `json:"type"`

	// Lifecycle fields
	MessageID       string          `json:"messageId,omitempty"`
	MessageMetadata json.RawMessage `json:"messageMetadata,omitempty"`
	FinishReason    string          `json:"finishReason,omitempty"`
	Reason          string          `json:"reason,omitempty"`

	// Content fields
	ID             string `json:"id,omitempty"`
	Delta          string `json:"delta,omitempty"`
	InputTextDelta string `json:"inputTextDelta,omitempty"`

	// Tool fields
	ToolCallID string          `json:"toolCallId,omitempty"`
	ToolName   string          `json:"toolName,omitempty"`
	ApprovalID string          `json:"approvalId,omitempty"`
	Signature  string          `json:"signature,omitempty"`
	Input      json.RawMessage `json:"input,omitempty"`
	Output     json.RawMessage `json:"output,omitempty"`
	ErrorText  string          `json:"errorText,omitempty"`
	// Approved is intentionally not tagged with omitempty: a denial response
	// (approved=false) MUST appear on the wire. The custom MarshalJSON for
	// ChunkToolApprovalResponse always writes this field, but exposing the
	// raw bool would otherwise silently drop denials.
	Approved         bool  `json:"approved"`
	ProviderExecuted bool  `json:"providerExecuted,omitempty"`
	Dynamic          *bool `json:"dynamic,omitempty"`
	Preliminary      bool  `json:"preliminary,omitempty"`
	IsAutomatic      bool  `json:"isAutomatic,omitempty"`

	// Source fields
	SourceID string `json:"sourceId,omitempty"`
	URL      string `json:"url,omitempty"`
	Title    string `json:"title,omitempty"`

	// File fields
	MediaType string `json:"mediaType,omitempty"`
	Filename  string `json:"filename,omitempty"`

	// Data chunk fields (type is "data-{name}")
	DataName  string          `json:"-"`
	Data      json.RawMessage `json:"data,omitempty"`
	Transient bool            `json:"transient,omitempty"`

	ProviderMetadata provider.ProviderMetadata `json:"providerMetadata,omitempty"`
	ToolMetadata     json.RawMessage           `json:"toolMetadata,omitempty"`
}

UIMessageChunk is a single SSE event in the UI message stream protocol. Use the constructor functions (TextDeltaChunk, DataChunk, etc.) to create valid chunks with the correct Type set.

func DataChunk

func DataChunk(name string, data json.RawMessage, transient bool) UIMessageChunk

DataChunk creates a data chunk. Set transient to true for ephemeral data that should not be persisted in message history.

func ErrorChunk

func ErrorChunk(errorText string) UIMessageChunk

ErrorChunk creates an error chunk.

func FileChunk

func FileChunk(url, mediaType string) UIMessageChunk

FileChunk creates a file chunk.

func FinishChunk

func FinishChunk(finishReason string) UIMessageChunk

FinishChunk creates a finish chunk.

func ReasoningDeltaChunk

func ReasoningDeltaChunk(id, delta string) UIMessageChunk

ReasoningDeltaChunk creates a reasoning-delta chunk.

func ReasoningEndChunk

func ReasoningEndChunk(id string) UIMessageChunk

ReasoningEndChunk creates a reasoning-end chunk.

func ReasoningFileChunk

func ReasoningFileChunk(url, mediaType string) UIMessageChunk

ReasoningFileChunk creates a reasoning-file chunk.

func ReasoningStartChunk

func ReasoningStartChunk(id string) UIMessageChunk

ReasoningStartChunk creates a reasoning-start chunk.

func SourceDocumentChunk

func SourceDocumentChunk(sourceID, mediaType, title string) UIMessageChunk

SourceDocumentChunk creates a source-document chunk.

func SourceURLChunk

func SourceURLChunk(sourceID, url string) UIMessageChunk

SourceURLChunk creates a source-url chunk.

func StartChunk

func StartChunk(messageID string) UIMessageChunk

StartChunk creates a start chunk.

func TextDeltaChunk

func TextDeltaChunk(id, delta string) UIMessageChunk

TextDeltaChunk creates a text-delta chunk.

func TextEndChunk

func TextEndChunk(id string) UIMessageChunk

TextEndChunk creates a text-end chunk.

func TextStartChunk

func TextStartChunk(id string) UIMessageChunk

TextStartChunk creates a text-start chunk.

func ToolInputAvailableChunk

func ToolInputAvailableChunk(toolCallID, toolName string, input json.RawMessage) UIMessageChunk

ToolInputAvailableChunk creates a tool-input-available chunk.

func ToolInputDeltaChunk

func ToolInputDeltaChunk(toolCallID, inputTextDelta string) UIMessageChunk

ToolInputDeltaChunk creates a tool-input-delta chunk.

func ToolInputErrorChunk

func ToolInputErrorChunk(toolCallID, toolName string, input json.RawMessage, errorText string) UIMessageChunk

ToolInputErrorChunk creates a tool-input-error chunk.

func ToolInputStartChunk

func ToolInputStartChunk(toolCallID, toolName string) UIMessageChunk

ToolInputStartChunk creates a tool-input-start chunk.

func ToolOutputAvailableChunk

func ToolOutputAvailableChunk(toolCallID string, output json.RawMessage) UIMessageChunk

ToolOutputAvailableChunk creates a tool-output-available chunk.

func ToolOutputErrorChunk

func ToolOutputErrorChunk(toolCallID, errorText string) UIMessageChunk

ToolOutputErrorChunk creates a tool-output-error chunk.

func (UIMessageChunk) MarshalJSON

func (c UIMessageChunk) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Each chunk type serializes only the fields defined by the UIMessageChunk schema.

func (*UIMessageChunk) UnmarshalJSON

func (c *UIMessageChunk) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler and rejects unknown chunk discriminators.

type UIMessageReaderOption

type UIMessageReaderOption interface {
	// contains filtered or unexported methods
}

UIMessageReaderOption configures UI message stream readers.

func WithUIMessageReaderGenerateID

func WithUIMessageReaderGenerateID(fn func() string) UIMessageReaderOption

WithUIMessageReaderGenerateID configures the fallback message ID generator used when a chunk stream does not provide an ID before one is needed.

type UIMessageStreamOnFinishState

type UIMessageStreamOnFinishState struct {
	Messages        []UIMessage
	IsContinuation  bool
	IsAborted       bool
	ResponseMessage UIMessage
	FinishReason    provider.FinishReason
}

UIMessageStreamOnFinishState is passed to the OnFinish callback.

type UIMessageStreamOption

type UIMessageStreamOption interface {
	// contains filtered or unexported methods
}

UIMessageStreamOption configures UI message stream helpers.

func OnUIMessageStreamError

func OnUIMessageStreamError(fn func(error) string) UIMessageStreamOption

OnUIMessageStreamError configures error-to-text conversion for UI streams.

func OnUIMessageStreamFinish

func OnUIMessageStreamFinish(fn func(UIMessageStreamOnFinishState)) UIMessageStreamOption

OnUIMessageStreamFinish configures a callback invoked after stream finish.

func WithUIMessageStreamFinish

func WithUIMessageStreamFinish(send bool) UIMessageStreamOption

WithUIMessageStreamFinish configures whether finish chunks are sent.

func WithUIMessageStreamGenerateID

func WithUIMessageStreamGenerateID(fn func() string) UIMessageStreamOption

WithUIMessageStreamGenerateID configures generated UI message IDs.

func WithUIMessageStreamMessageMetadata

func WithUIMessageStreamMessageMetadata(fn func(part TextStreamPart) json.RawMessage) UIMessageStreamOption

WithUIMessageStreamMessageMetadata configures message metadata extraction.

func WithUIMessageStreamOriginalMessages

func WithUIMessageStreamOriginalMessages(messages ...UIMessage) UIMessageStreamOption

WithUIMessageStreamOriginalMessages configures the original messages used for continuation IDs and finish callbacks. Calling this option, even with no messages, marks original messages as explicitly present.

func WithUIMessageStreamReasoning

func WithUIMessageStreamReasoning(send bool) UIMessageStreamOption

WithUIMessageStreamReasoning configures whether reasoning chunks are sent.

func WithUIMessageStreamSources

func WithUIMessageStreamSources(send bool) UIMessageStreamOption

WithUIMessageStreamSources configures whether source chunks are sent.

func WithUIMessageStreamStart

func WithUIMessageStreamStart(send bool) UIMessageStreamOption

WithUIMessageStreamStart configures whether start chunks are sent.

type UIMessageStreamWriter

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

UIMessageStreamWriter writes UIMessageChunk events to a stream. It is safe for concurrent use.

func (*UIMessageStreamWriter) Close

func (w *UIMessageStreamWriter) Close() error

Close closes the writer. No further writes are allowed. Returns ErrAlreadyClosed if called more than once.

func (*UIMessageStreamWriter) Merge

func (w *UIMessageStreamWriter) Merge(stream <-chan UIMessageChunk) error

Merge reads all chunks from the given channel and writes them to this stream.

func (*UIMessageStreamWriter) Write

func (w *UIMessageStreamWriter) Write(chunk UIMessageChunk) (retErr error)

Write sends a chunk to the stream. Returns ErrWriterClosed if the writer has been closed.

type UIPartType

type UIPartType string

UIPartType identifies the wire type of a UIMessage part.

const (
	UIPartText           UIPartType = "text"
	UIPartReasoning      UIPartType = "reasoning"
	UIPartToolInvocation UIPartType = "tool-invocation"
	UIPartDynamicTool    UIPartType = "dynamic-tool"
	UIPartFile           UIPartType = "file"
	UIPartReasoningFile  UIPartType = "reasoning-file"
	UIPartSourceURL      UIPartType = "source-url"
	UIPartSourceDocument UIPartType = "source-document"
	UIPartData           UIPartType = "data"
	UIPartStepStart      UIPartType = "step-start"
)

type UserToolType

type UserToolType string

UserToolType identifies the kind of user-facing tool.

const (
	UserToolFunction UserToolType = "function"
	UserToolProvider UserToolType = "provider"
	UserToolDynamic  UserToolType = "dynamic"
)

Directories

Path Synopsis
gateway
catalog
Package catalog provides a finite public model namespace for hosted gateways.
Package catalog provides a finite public model namespace for hosted gateways.
providerwire
Package providerwire implements the complete JSON over HTTP and SSE transport for remote provider.LanguageModel calls.
Package providerwire implements the complete JSON over HTTP and SSE transport for remote provider.LanguageModel calls.
internal
Package middleware provides a model-agnostic interception layer for provider.LanguageModel.
Package middleware provides a model-agnostic interception layer for provider.LanguageModel.
logger module
prometheus module
Package output provides structured output capabilities for the AI SDK.
Package output provides structured output capabilities for the AI SDK.
Package provider defines the interface and types that LLM provider implementations depend on.
Package provider defines the interface and types that LLM provider implementations depend on.
providers
anthropic module
bedrock module
grafana module
openai module
Package registry provides provider management for the AI SDK.
Package registry provides provider management for the AI SDK.
Package schema provides JSON Schema generation, compilation, and validation for the AI SDK.
Package schema provides JSON Schema generation, compilation, and validation for the AI SDK.

Jump to

Keyboard shortcuts

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