{"record":{"id":"f6927e250dbd021c","repo":"vercel/ai","slug":"xai-batch-options-batchid-is-not-complete","errorCode":null,"errorMessage":"xAI batch \"${options.batchId}\" is not complete.","messagePattern":"xAI batch \"(.+?)\" is not complete\\.","errorType":"validation","errorClass":"InvalidArgumentError","httpStatus":null,"severity":"error","filePath":"packages/xai/src/responses/xai-responses-batch.ts","lineNumber":221,"sourceCode":"    return {\n      batchId: batch.batch_id,\n      ...convertXaiBatchStatus(batch),\n      warnings,\n    };\n  }\n\n  async getBatchStatus(\n    options: BatchV4OperationOptions,\n  ): Promise<BatchV4Status> {\n    return convertXaiBatchStatus(await this.retrieveBatch(options));\n  }\n\n  async getBatchResults(\n    options: BatchV4OperationOptions,\n  ): Promise<ReadableStream<BatchV4ItemResult<LanguageModelV4GenerateResult>>> {\n    const batch = await this.retrieveBatch(options);\n    if (convertXaiBatchStatus(batch).status === 'pending') {\n      throw new InvalidArgumentError({\n        argument: 'batchId',\n        message: `xAI batch \"${options.batchId}\" is not complete.`,\n      });\n    }\n\n    return convertAsyncIteratorToReadableStream(\n      this.iterateBatchResults(options),\n    );\n  }\n\n  private async retrieveBatch(\n    options: BatchV4OperationOptions,\n  ): Promise<XaiBatchResponse> {\n    const { value: batch } = await getFromApi({\n      url: this.getUrl(`/batches/${encodeURIComponent(options.batchId)}`),\n      headers: combineHeaders(this.options.config.headers?.(), options.headers),\n      failedResponseHandler: xaiFailedResponseHandler,\n      successfulResponseHandler: createJsonResponseHandler(","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/xai/src/responses/xai-responses-batch.ts#L203-L239","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Poll getBatch (or the batch status) until status is 'completed' before calling getBatchResults","Add retry/backoff around getBatchResults that catches InvalidArgumentError and re-checks later","Verify the batchId is correct — a typo'd id may resolve to an unexpected state"],"exampleFix":"// before\nconst results = await batch.getBatchResults({ batchId });\n// after\nconst b = await batch.getBatch({ batchId });\nif (convertXaiBatchStatus(b).status === 'completed') {\n  const results = await batch.getBatchResults({ batchId });\n}","handlingStrategy":"retry","validationCode":"const batch = await batchClient.getBatch({ batchId });\nif (batch.status !== 'completed' && batch.status !== 'finished') {\n  throw new Error(`batch ${batchId} not ready: ${batch.status}`);\n}","typeGuard":"null","tryCatchPattern":"import { InvalidArgumentError } from 'ai';\ntry {\n  const results = await batch.getBatchResults({ batchId });\n} catch (e) {\n  if (InvalidArgumentError.isInstance(e) && e.message.includes('is not complete')) {\n    await sleep(30_000); // poll again later\n  }\n}","preventionTips":["Poll batch status until complete before requesting results","Use exponential backoff rather than fixed short intervals","Never assume batch duration — large batches can take hours"],"tags":["xai","batch","not-complete","timing"],"backgroundTag":"batch-not-complete","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}