{"record":{"id":"4974456747d27175","repo":"vercel/ai","slug":"parsederror-value-error","errorCode":null,"errorMessage":"${parsedError.value.error}","messagePattern":"\\$\\{parsedError\\.value\\.error\\}","errorType":"exception","errorClass":"APICallError","httpStatus":200,"severity":"error","filePath":"packages/xai/src/xai-chat-language-model.ts","lineNumber":399,"sourceCode":"\n    const { responseHeaders, value: response } = await postJsonToApi({\n      url,\n      headers: combineHeaders(this.config.headers?.(), options.headers),\n      body,\n      failedResponseHandler: xaiFailedResponseHandler,\n      successfulResponseHandler: async ({ response }) => {\n        const responseHeaders = extractResponseHeaders(response);\n        const contentType = response.headers.get('content-type');\n\n        if (contentType?.includes('application/json')) {\n          const responseBody = await response.text();\n          const parsedError = await safeParseJSON({\n            text: responseBody,\n            schema: xaiStreamErrorSchema,\n          });\n\n          if (parsedError.success) {\n            throw new APICallError({\n              message: parsedError.value.error,\n              url,\n              requestBodyValues: body,\n              statusCode: 200,\n              responseHeaders,\n              responseBody,\n              isRetryable:\n                parsedError.value.code ===\n                'The service is currently unavailable',\n            });\n          }\n\n          throw new APICallError({\n            message: 'Invalid JSON response',\n            url,\n            requestBodyValues: body,\n            statusCode: 200,\n            responseHeaders,","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/xai/src/xai-chat-language-model.ts#L381-L417","documentation":"In doStream, if the initial response stream yields an error payload, the SDK reads the body and safeParseJSON-validates it against xaiStreamErrorSchema. When parsing succeeds, the parsed error string becomes an APICallError (statusCode 200, since the SSE/HTTP layer succeeded). This is xAI reporting an error inside an otherwise successful response stream.","triggerScenarios":"Starting a chat completion stream that xAI accepts (HTTP 200) but then emits an error event/body — rate limits applied mid-stream, model overloaded messages, content policy violations detected during generation.","commonSituations":"High-concurrency streaming workloads hitting mid-stream overload ('The service is currently unavailable'); policy-triggered stream aborts; upstream xAI incidents.","solutions":["Catch the APICallError and retry with backoff, especially if the code is 'The service is currently unavailable' (the SDK marks that retryable)","Reduce request concurrency/rate to avoid mid-stream throttling","Inspect responseBody in the error for xAI's full error detail and adjust the prompt if policy-related"],"exampleFix":"// before\nconst result = await streamText({ model: xai('grok-4'), prompt });\n// after\nconst result = streamText({ model: xai('grok-4'), prompt, maxRetries: 3 });\ntry {\n  for await (const part of result.fullStream) { /* ... */ }\n} catch (e) {\n  if (APICallError.isInstance(e)) console.error(e.message, e.responseBody);\n}","handlingStrategy":"retry","validationCode":"null","typeGuard":"null","tryCatchPattern":"import { APICallError } from 'ai';\ntry {\n  const result = streamText({ model, prompt, maxRetries: 3 });\n  for await (const part of result.fullStream) { /* ... */ }\n} catch (e) {\n  if (APICallError.isInstance(e) && e.isRetryable) {\n    // SDK already retries for 'The service is currently unavailable'; add outer backoff otherwise\n  }\n}","preventionTips":["Set maxRetries >= 2 for streaming xAI workloads","Throttle concurrency to avoid mid-stream overload errors","Monitor responseBody to distinguish policy vs capacity errors"],"tags":["xai","streaming","api-call-error","retryable"],"backgroundTag":"api-error-in-200-response","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}