Skip to main content

Utilities

Smaller exported types and functions in pkg/ai that the other reference pages do not cover.

Message pruning​

Prune a conversation to fit a token budget.

func PruneMessages(ctx context.Context, messages []types.Message, opts PruneOptions) ([]types.Message, error)
func PruneToFitContext(ctx context.Context, messages []types.Message, contextWindow int, reserveTokens int) ([]types.Message, error)
func DefaultMessagePrune(ctx context.Context, messages []types.Message, maxTokens int) ([]types.Message, error)
func DefaultPruneOptions(maxTokens int) PruneOptions

ai.MessagePruneFunc is the function type of DefaultMessagePrune: func(ctx context.Context, messages []types.Message, maxTokens int) ([]types.Message, error). DefaultMessagePrune keeps the system message and the last messages and drops older ones.

FieldTypeDescription
MaxTokensintMaxTokens is the maximum number of tokens allowed
PreserveSystemMessageboolPreserveSystemMessage keeps the system message even if pruning is needed
PreserveLastNintPreserveLastN keeps the last N messages
PruneFuncMessagePruneFuncPruneFunc is the custom pruning function

Timeouts​

ai.TimeoutConfig sets total, per-step and per-chunk timeouts on GenerateTextOptions and StreamTextOptions. ai.NewTimeoutConfig(total, perStep, perChunk *time.Duration) builds one. Pass nil for a timeout you do not want.

ai.TimeoutReason names the boundary that aborted an operation.

ConstantValueDescription
TimeoutReasonTotal"total"
TimeoutReasonStep"step"
TimeoutReasonChunk"chunk"
TimeoutReasonFirstChunk"firstChunk"
TimeoutReasonTool"tool"

Stream status​

ai.StreamStatus reports the state of a stream for UI code.

ConstantValueDescription
StreamStatusSubmitted"submitted"StreamStatusSubmitted indicates the request has been submitted and the stream is actively receiving data from the model.
StreamStatusStreaming"streaming"StreamStatusStreaming indicates at least one chunk has been received.
StreamStatusDone"done"StreamStatusDone indicates the stream has completed successfully.

Building stream results​

NewStreamTextResultFromParts​

func NewStreamTextResultFromParts(ctx context.Context, src provider.TextStream, opts ExternalStreamOptions) *StreamTextResult

Builds a *StreamTextResult from a stream you already have, instead of calling a model. The stream carries every step's chunks in order. Use it to put the SDK's result accessors and UI helpers over a custom source.

FieldTypeDescription
CallIDstringCallID correlates this result with the caller's own callback/telemetry events. A new one is generated when empty.
ProviderstringProvider and ModelID label steps and the response metadata when a chunk does not carry its own provider.ResponseMetadata.
ModelIDstring
OnChunkfunc(chunk provider.StreamChunk)OnChunk is invoked for every chunk as it is consumed from src, mirroring StreamTextOptions.OnChunk.
OnFinishfunc(result *StreamTextResult)OnFinish/OnEnd are invoked once, after src is fully consumed (whether it ended in success or with an error), mirroring StreamTextOptions' callbacks of the same name. OnEnd takes precedence when both are set.
OnEndfunc(result *StreamTextResult)
Outputinterface{}Output, when it satisfies the internal outputProcessor interface (any value returned by TextOutput/ObjectOutput/ArrayOutput/ChoiceOutput/ JSONOutput), enables structured-output parsing on the returned result: Output()/OutputErr() resolve once src is fully consumed by parsing the final step's accumulated text, and PartialOutput() updates after every text chunk of that step, deduplicated exactly like StreamTextOptions. Output. A value that does not satisfy outputProcessor is ignored (mirrors StreamTextOptions.Output's interface{} + type-assertion contract; see stream.go's own opts.Output handling for the pattern this mirrors).

SimulateReadableStream​

func SimulateReadableStream(chunks []provider.StreamChunk, perChunkDelay time.Duration) provider.TextStream

Creates a stream from in-memory chunks, with a delay between chunks. Use it in tests.

Stream transform emitters​

A StreamTransformFunc can emit chunks before it returns, which keeps output flowing for transforms that wait.

NameDescription
ai.StreamTransformEmitterfunc(provider.StreamChunk). Emits one chunk immediately.
ai.WithStreamTransformEmitter(ctx, emit)Returns a context that carries an emitter. The stream loop installs one around every transform call.
ai.StreamTransformEmitterFromContext(ctx)Returns the emitter and true inside a transform call. Outside one it returns false, and the transform should return its chunks as a batch.

ChunkDetector​

ai.ChunkDetector is one of the values ai.SmoothStreamOptions.Chunking accepts, next to "word", "line" and a *regexp.Regexp. It takes the buffered text and returns the first complete chunk and true, or an empty string and false when the buffer holds no complete chunk. It mirrors the TypeScript ChunkDetector.

Text stream response init​

ai.CreateTextStreamResponseWithInit(ctx, result, init *TextStreamResponseInit) is CreateTextStreamResponse with a custom status, status text and headers.

ai.PipeTextStreamToWriter(ctx, stream provider.TextStream, w io.Writer) error writes text deltas from a provider stream to w as UTF-8. If a write fails or ctx ends first, it closes the stream before it returns, so upstream resources are released.

Parsing partial output​

NameDescription
ai.ParsePartialJSON(text string) jsonparser.ParseResultParses JSON that may be incomplete.
ai.ParsePartialOutputOptionsInput for an output specification's partial parse: Text.
ai.ParseCompleteOutputOptionsInput for the final parse: Text, Response, Usage and FinishReason.
ai.GetTextFromDataURL(dataURL string) (string, error)Decodes a base64 text/* data URL, using its declared charset (UTF-8 by default).

Object output modes​

ConstantValueDescription
ObjectModeObject"object"ObjectModeObject returns a single object (default)
ObjectModeArray"array"ObjectModeArray returns an array of objects (streaming)
ObjectModeEnum"enum"ObjectModeEnum forces selection from enum values
ObjectModeNoSchema"no-schema"ObjectModeNoSchema returns raw JSON without validation

ElementStreamOptions​

Options for streaming array elements as they complete.

FieldTypeDescription
ElementSchemaschema.SchemaElementSchema defines the structure of each array element
OnElementfunc(element ElementStreamResult[ELEMENT])OnElement is called when a new element is parsed
OnErrorfunc(err error)OnError is called when an error occurs during parsing
OnCompletefunc()OnComplete is called when the stream completes
FieldTypeDescription
ElementELEMENTThe parsed element
IndexintIndex of this element in the array
IsFinalboolWhether this is the final element in the array

Evaluation​

ai.ExperimentalEvaluate asks an evaluation model typed questions and returns one typed answer per question. ai.NewEvaluationLanguageModel returns an *ai.EvaluationLanguageModel, which adapts any language model into an evaluation model by prompting it with a JSON-schema-constrained request. Reasoning defaults to none unless you override it with provider options.

FieldTypeDescription
Modelprovider.LanguageModel
ProviderstringProvider overrides the reported provider ID. Defaults to "<model.Provider()>.evaluation".

EvaluateResult​

FieldTypeDescription
Answersmap[string]provider.EvaluationAnswerAnswers has exactly one typed answer per question, keyed by question ID.
UsageEvaluateUsage
Warnings[]types.Warning
Rounding*provider.EvaluationRounding
ProviderMetadatamap[string]interface{}
ResponseEvaluateResponse
FieldTypeDescription
InputTokens*int
OutputTokens*int
TotalTokens*int
FieldTypeDescription
IDstring
Timestamptime.Time
ModelIDstring
Headersmap[string]string
Bodyinterface{}

Per-call diagnostics​

GenerateImageResult.Calls holds one ai.GenerateImageCall per provider call. Each step result from text generation carries an ai.GenerateStepRequest and an ai.GenerateStepResponse.

FieldTypeDescription
Images[]types.GeneratedFile
ProviderMetadatamap[string]interface{}
Response*types.ResponseMetadata
Warnings[]types.Warning
Usagetypes.ImageUsage
FieldTypeDescription
Bodyinterface{}Body is the raw request body sent to the provider API (for debugging).
FieldTypeDescription
IDstringID is the provider-assigned response identifier when available. For streaming paths, this may be a generated fallback value.
Timestamptime.TimeTimestamp is when the provider started generating the response. Zero value means it was not available.
ModelIDstringModelID is the model that handled the request, when available.
Headersmap[string]stringHeaders are the raw HTTP response headers from the provider.
Messages[]types.MessageMessages are the response messages generated in this step (assistant message + any tool messages).
Bodyinterface{}Body is the raw response body from the provider (for debugging).

Realtime sessions​

ai.ConnectRealtime(ctx, model, opts RealtimeSessionOptions) opens a *ai.RealtimeSession on an experimental realtime model. Send(ctx, event) writes a client event. Read(ctx) returns the next batch of server events. Close() ends the session.

NameDescription
ai.RealtimeDialerOpens a connection: Dial(ctx, provider.WebSocketConfig) (RealtimeWebSocketConn, error).
ai.WebSocketRealtimeDialerThe default dialer, built on golang.org/x/net/websocket. The Origin field is unused.
ai.RealtimeWebSocketConnA connection with Send, Receive and Close.
ai.RealtimeBinaryConnOptional capability for a connection that can send and receive binary frames (SendBinary, ReceiveFrame). Without it, every frame is text.
ai.GetRealtimeToolDefinitions(tools) ([]provider.RealtimeToolDefinition, error)Converts tools to realtime tool definitions.
ai.GetRealtimeToolDefinitionsWithOptions(opts RealtimeToolDefinitionsOptions)Same, with a per-tool context.
FieldTypeDescription
Tools[]types.Tool
ToolsContextmap[string]interface{}

Sandboxes​

ai.ShellSandbox runs commands through the local shell with context cancellation. ai.NewShellSandbox(opts ...ShellSandboxOption) creates one, and ai.WithShellSandboxDescription(description) sets its description. The sandbox types ai.SandboxProcess, ai.SandboxProcessResult, ai.SandboxReadTextFileOptions and ai.SandboxWriteTextFileOptions re-export the types in pkg/providerutils.

Warnings and telemetry​

NameDescription
ai.LogWarningsFunctionfunc(options LogWarningsOptions). Install one with ai.SetLogWarnings to replace the default stderr logger.
ai.DisableLogWarnings()Turns warning logging off. The AI_SDK_LOG_WARNINGS=false environment variable does the same.
ai.TelemetryOptionsAlias for telemetry.Options.

URL support checks​

FunctionDescription
ai.SupportedURLCheckerForModel(model)Returns a func(mediaType, rawURL string) bool for a model that exposes SupportedURLs. A model without it supports no direct URLs.
ai.SupportedURLCheckerFromPatterns(patternsByMediaType)Compiles a SupportedURLs-style map of regular expressions, keyed by media type, into the same checker.