vercel/ai · error · InvalidArgumentError

xAI batch "${options.batchId}" is not complete.

Error message

xAI batch "${options.batchId}" is not complete.

What it means

xai-responses-batch.getBatchResults fetches results for a batch, but only when the batch status maps to a completed state. If the batch is still pending (queued/running/in_progress), an InvalidArgumentError is thrown for the given batchId instead of returning partial results.

Source

Thrown at packages/xai/src/responses/xai-responses-batch.ts:221

    return {
      batchId: batch.batch_id,
      ...convertXaiBatchStatus(batch),
      warnings,
    };
  }

  async getBatchStatus(
    options: BatchV4OperationOptions,
  ): Promise<BatchV4Status> {
    return convertXaiBatchStatus(await this.retrieveBatch(options));
  }

  async getBatchResults(
    options: BatchV4OperationOptions,
  ): Promise<ReadableStream<BatchV4ItemResult<LanguageModelV4GenerateResult>>> {
    const batch = await this.retrieveBatch(options);
    if (convertXaiBatchStatus(batch).status === 'pending') {
      throw new InvalidArgumentError({
        argument: 'batchId',
        message: `xAI batch "${options.batchId}" is not complete.`,
      });
    }

    return convertAsyncIteratorToReadableStream(
      this.iterateBatchResults(options),
    );
  }

  private async retrieveBatch(
    options: BatchV4OperationOptions,
  ): Promise<XaiBatchResponse> {
    const { value: batch } = await getFromApi({
      url: this.getUrl(`/batches/${encodeURIComponent(options.batchId)}`),
      headers: combineHeaders(this.options.config.headers?.(), options.headers),
      failedResponseHandler: xaiFailedResponseHandler,
      successfulResponseHandler: createJsonResponseHandler(

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Poll getBatch (or the batch status) until status is 'completed' before calling getBatchResults
  2. Add retry/backoff around getBatchResults that catches InvalidArgumentError and re-checks later
  3. Verify the batchId is correct — a typo'd id may resolve to an unexpected state

Example fix

// before
const results = await batch.getBatchResults({ batchId });
// after
const b = await batch.getBatch({ batchId });
if (convertXaiBatchStatus(b).status === 'completed') {
  const results = await batch.getBatchResults({ batchId });
}
Defensive patterns

Strategy: retry

Validate before calling

const batch = await batchClient.getBatch({ batchId });
if (batch.status !== 'completed' && batch.status !== 'finished') {
  throw new Error(`batch ${batchId} not ready: ${batch.status}`);
}

Type guard

null

Try / catch

import { InvalidArgumentError } from 'ai';
try {
  const results = await batch.getBatchResults({ batchId });
} catch (e) {
  if (InvalidArgumentError.isInstance(e) && e.message.includes('is not complete')) {
    await sleep(30_000); // poll again later
  }
}

Prevention

When it happens

Trigger: Calling getBatchResults({ batchId }) right after batch creation or before the xAI batch finishes processing; polling too aggressively; checking results after a failed/cancelled batch that maps to a pending-ish status.

Common situations: Fire-and-forget scripts that submit a batch and immediately request results; long-running batches (minutes to hours) polled once; cron job intervals shorter than batch duration.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/f6927e250dbd021c. Report an issue: GitHub.