vercel/ai · error · APICallError

${response.error}

Error message

${response.error}

What it means

When the xAI chat completions endpoint returns HTTP 200 but the JSON body contains an error field, doGenerate surfaces it as an APICallError with statusCode 200. xAI occasionally reports errors inside a 200 response, so the SDK explicitly checks response.error after parsing.

Source

Thrown at packages/xai/src/xai-chat-language-model.ts:277

    const {
      responseHeaders,
      value: response,
      rawValue: rawResponse,
    } = await postJsonToApi({
      url,
      headers: combineHeaders(this.config.headers?.(), options.headers),
      body,
      failedResponseHandler: xaiFailedResponseHandler,
      successfulResponseHandler: createJsonResponseHandler(
        xaiChatResponseSchema,
      ),
      abortSignal: options.abortSignal,
      fetch: this.config.fetch,
    });

    if (response.error != null) {
      throw new APICallError({
        message: response.error,
        url,
        requestBodyValues: body,
        statusCode: 200,
        responseHeaders,
        responseBody: JSON.stringify(rawResponse),
        isRetryable: response.code === 'The service is currently unavailable',
      });
    }

    const choice = response.choices![0];
    const content: Array<LanguageModelV4Content> = [];

    // extract text content
    if (choice.message.content != null && choice.message.content.length > 0) {
      let text = choice.message.content;

      // skip if this content duplicates the last assistant message

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Read the APICallError message (the raw response.error) to identify the xAI-side cause and adjust the prompt/parameters
  2. Inspect responseBody in the error for the full payload; remove or rephrase content if it is a policy error
  3. Check whether a proxy/gateway is rewriting error statuses to 200 and fix it so normal error handling applies
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

import { APICallError } from 'ai';
try {
  const { text } = await generateText({ model, prompt });
} catch (e) {
  if (APICallError.isInstance(e) && e.statusCode === 200) {
    console.error('xAI in-band error:', e.message, e.responseBody);
  }
}

Prevention

When it happens

Trigger: A successful-looking 200 response from api.x.ai/v1/chat/completions whose body is { error: ... } — e.g. moderation blocks, content filtered at generation time, or partial gateway failures passing through 200.

Common situations: Prompt content triggering xAI safety errors returned in-band; proxy/gateway layers that convert failures into 200-wrapped errors; streaming-unaware intermediary returning error JSON.

Related errors


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