{"record":{"id":"3300b1e0a4afd68c","repo":"vercel/ai","slug":"invalid-json-response","errorCode":null,"errorMessage":"Invalid JSON response","messagePattern":"Invalid JSON response","errorType":"exception","errorClass":"APICallError","httpStatus":200,"severity":"error","filePath":"packages/xai/src/xai-chat-language-model.ts","lineNumber":412,"sourceCode":"            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,\n            responseBody,\n          });\n        }\n\n        return createEventSourceResponseHandler(xaiChatChunkSchema)({\n          response,\n          url,\n          requestBodyValues: body,\n        });\n      },\n      abortSignal: options.abortSignal,\n      fetch: this.config.fetch,\n    });","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/xai/src/xai-chat-language-model.ts#L394-L430","documentation":"In doStream, when the response body signals an error but cannot be parsed against xaiStreamErrorSchema, the SDK throws a generic APICallError 'Invalid JSON response' with the raw body attached. It indicates the error body had an unexpected shape (or the body was otherwise not the JSON the SDK expects).","triggerScenarios":"xAI (or an intermediary) returns HTTP 200 with a non-schema-conforming error body during a chat completion stream — e.g. HTML error pages from proxies, new/changed xAI error formats, truncated bodies.","commonSituations":"Corporate proxies/WAFs injecting HTML into responses; xAI introducing a new error schema before the SDK supports it; gateway timeouts returning malformed bodies.","solutions":["Inspect responseBody on the thrown APICallError to see the actual body and identify the shape mismatch","Bypass or fix proxies/WAFs that rewrite the response body","Update @ai-sdk/xai to the latest version in case xAI's error schema changed"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"null","typeGuard":"null","tryCatchPattern":"import { APICallError } from 'ai';\ntry {\n  for await (const part of stream.fullStream) { /* ... */ }\n} catch (e) {\n  if (APICallError.isInstance(e) && e.message === 'Invalid JSON response') {\n    // inspect e.responseBody; optionally retry once or surface diagnostics\n  }\n}","preventionTips":["Keep @ai-sdk/xai updated for xAI error-schema changes","Ensure no proxies/WAFs rewrite or truncate response bodies","Log responseBody on 'Invalid JSON response' for diagnosis"],"tags":["xai","streaming","invalid-json","api-call-error"],"backgroundTag":"invalid-json-response","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}