Skip to contents

Repeatedly calls openai_get_batch() until the batch reaches a terminal status (one of "completed", "failed", "cancelled", "expired"), a timeout is reached, or max_attempts is exceeded.

Usage

openai_poll_batch_until_complete(
  batch_id,
  interval_seconds = 5,
  timeout_seconds = 600,
  max_attempts = Inf,
  api_key = NULL,
  verbose = TRUE
)

Arguments

batch_id

The batch ID.

interval_seconds

Number of seconds to wait between polling attempts.

timeout_seconds

Maximum total time to wait in seconds before giving up.

max_attempts

Maximum number of logical status polls, excluding internal HTTP retries. This is mainly useful for testing; default is Inf.

api_key

Optional OpenAI API key.

verbose

Logical; if TRUE, prints status messages to the console.

Value

The final Batch object (a list) as returned by openai_get_batch().

Details

This is a synchronous helper – it will block until one of the conditions above is met.

Polling limits

HTTP retries occur within a logical status poll. Elapsed time includes retry waits, but timeout_seconds is checked between status requests; an in-flight GET and its retries can finish after that limit. Existing terminal-status and timeout return behavior is preserved.

Retrieval retries

Batch metadata and result-file GET requests retry HTTP 408, 429, all 5xx responses, and transport failures, with at most three total HTTP attempts per GET. Valid Retry-After seconds or HTTP dates take precedence; otherwise retries use exponential backoff starting at 0.5 seconds plus up to 0.25 seconds of jitter, capped at 30 seconds. Other HTTP errors fail immediately. Exhaustion raises the original error with the additional class pairwiseLLM_batch_retry_exhausted. These retries retrieve the same batch; they do not resubmit comparisons or create scientific failed-attempt rows.

Examples

if (FALSE) { # \dontrun{
# Requires OPENAI_API_KEY and a created batch that may still be running.

batch <- openai_create_batch("file_123", endpoint = "/v1/chat/completions")

final <- openai_poll_batch_until_complete(
  batch_id         = batch$id,
  interval_seconds = 10,
  timeout_seconds  = 3600
)

final$status
} # }