Skip to content

Batch Models

The data types of a provider’s batch API, for batch pricing in exchange for latency.

The BatchModelRequests capability lets an agent use a batch API without changing how it runs, and model_request_batch sends a list of requests as one batch. The batch_* methods on Model are the provider-level primitives both are built on.

BatchRequest

One model request to send as part of a batch.

Every aspect of the request can differ between the requests of a batch, as long as the provider accepts them in one job: Model.batch_encode reports which ones it can combine.

Attributes

custom_id

The ID that matches the request to its result. It must be unique within a batch.

Type: str

messages

The messages to send to the model.

Type: list[ModelMessage]

model_settings

Settings for this request.

Type: ModelSettings | None Default: None

model_request_parameters

Tools, output schema and other request parameters for this request.

Type: ModelRequestParameters Default: field(default_factory=ModelRequestParameters)

EncodedBatchRequest

A BatchRequest encoded for one provider’s batch API.

Attributes

custom_id

The ID that matches the request to its result within the batch.

Type: str

group

Everything requests must share to be submitted in one batch, such as the endpoint, credentials and model.

It is stored on the Batch handle, so it never contains secrets.

Type: str

body

The provider-native request, as JSON.

Type: str

Batch

A handle to a submitted batch job.

It holds only plain data, so you can store it, for example with a Pydantic TypeAdapter, and use it from another process to check on the job, retrieve its results or cancel it.

Attributes

id

The provider’s ID for the batch job.

Type: str

model_name

The name of the model the batch was submitted to.

Type: str

provider_name

The name of the provider the batch was submitted to.

Type: str

group

The group the batch’s requests share.

Used to check that a later call uses a compatible client configuration.

Type: str

status

The job’s processing state when this handle was last updated.

Type: BatchStatus

created_at

When the provider created the job.

Type: datetime

request_count

The number of requests in the batch.

Type: int

provider_details

Provider-specific job details, such as the files a provider writes results to.

Type: dict[str, Any] | None Default: None

BatchResult

The provider’s result for one request of a batch.

Turn it into a ModelResponse with Model.batch_decode.

Attributes

custom_id

The ID of the request this result belongs to.

Type: str

status

How the provider finished the request.

Type: BatchResultStatus

body

The provider-native result or error, as JSON, when the request 'succeeded' or 'errored'.

Type: str | None Default: None

BatchStatus

The processing state of a batch job. An ended job can hold a mix of per-request outcomes.

Type: TypeAlias Default: Literal['in_progress', 'canceling', 'ended']

BatchResultStatus

How the provider finished one request of a batch.

Type: TypeAlias Default: Literal['succeeded', 'errored', 'expired', 'canceled']