Skip to main content

Batch API

Batch functions submit many requests to a provider for asynchronous processing, then poll for status and read the results. The functions are experimental and carry the Experimental prefix.

A Provider field accepts a provider.BatchV4 value, a provider that exposes ExperimentalBatch(), or nil. When it is nil, the batch runs through the AI Gateway.

Functions​

FunctionReturns
ai.ExperimentalStartBatch(ctx, StartBatchOptions)*StartBatchResult. Submits the batch.
ai.ExperimentalGetBatchStatus(ctx, GetBatchStatusOptions)*Batch. The batch with its latest status.
ai.ExperimentalGetBatchResults(ctx, GetBatchResultsOptions)*BatchResultsStream. Streams the terminal results.
ai.ExperimentalCancelBatch(ctx, CancelBatchOptions)*CancelBatchResult. Requests cancellation.
ai.ExperimentalListBatches(ctx, ListBatchesOptions)*ListBatchesResult. One page of batches.

BatchReference and Batch​

ai.BatchReference is the value to persist. It holds Version, ID and Provider, which is enough to check status, read results or cancel from another process. ai.Batch embeds BatchReference and provider.BatchV4Status.

StartBatchOptions​

FieldTypeDescription
Providerinterface{}Provider is a provider.BatchV4 instance, a provider.BatchProvider (a Provider exposing ExperimentalBatch()), or nil. When nil, batches are processed through the AI Gateway (mirrors TypeScript: globalThis.AI_SDK_DEFAULT_PROVIDER ?? gateway).
Requests[]BatchRequest
ProviderOptionsmap[string]interface{}
WebhookURLstringWebhookURL, when set, is the URL the provider notifies when the batch reaches a terminal state. Providers that do not support completion webhooks return an unsupported-functionality warning instead of erroring.
MaxRetries*int
Headersmap[string]string
Timeout*time.Duration

StartBatchResult​

FieldTypeDescription
(embedded)BatchEmbedded.
Warnings[]provider.BatchV4Warning

BatchImageRequest​

An image request inside a batch.

FieldTypeDescription
IDstring
Modelstring
Promptstring
N*int
Sizestring
AspectRatiostring
Seed*int
Files[]provider.ImageFile
Mask*provider.ImageFile
ProviderOptionsmap[string]interface{}

GetBatchStatusOptions​

FieldTypeDescription
Providerinterface{}
BatchBatchReference
ProviderOptionsmap[string]interface{}
MaxRetries*int
Headersmap[string]string
Timeout*time.Duration

GetBatchResultsOptions​

FieldTypeDescription
Providerinterface{}
BatchBatchReference
ProviderOptionsmap[string]interface{}
MaxRetries*int
Headersmap[string]string
Timeout*time.Duration

BatchResultsStream​

Next() returns the next *ai.BatchItemResult and io.EOF when the stream is complete. Err() returns the first error. Close() releases the stream. The shape matches provider.TextStream.

FieldTypeDescription
Typeprovider.BatchRequestType
IDstring
Statusprovider.BatchItemStatus
Text*types.GenerateResultText is populated when Type is text and Status is succeeded.
Image*types.ImageResultImage is populated when Type is image and Status is succeeded.
Error*provider.BatchError
ProviderMetadatamap[string]interface{}

CancelBatchOptions​

FieldTypeDescription
Providerinterface{}
BatchBatchReference
ProviderOptionsmap[string]interface{}
Headersmap[string]string
Timeout*time.Duration
FieldTypeDescription
ProviderMetadatamap[string]interface{}

ListBatchesOptions​

FieldTypeDescription
Providerinterface{}
ProviderOptionsmap[string]interface{}
Limit*intLimit is optional (nil means unset), mirroring TypeScript's limit?: number. An explicit 0 is forwarded to the provider rather than treated as "not set".
Cursorstring
MaxRetries*int
Headersmap[string]string
Timeout*time.Duration

ListBatchesResult​

FieldTypeDescription
Batches[]Batch
NextCursorstring
ProviderMetadatamap[string]interface{}