vercel/ai · error · APICallError
Invalid JSON response
Error message
Invalid JSON response
What it means
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).
Source
Thrown at packages/xai/src/xai-chat-language-model.ts:412
text: responseBody,
schema: xaiStreamErrorSchema,
});
if (parsedError.success) {
throw new APICallError({
message: parsedError.value.error,
url,
requestBodyValues: body,
statusCode: 200,
responseHeaders,
responseBody,
isRetryable:
parsedError.value.code ===
'The service is currently unavailable',
});
}
throw new APICallError({
message: 'Invalid JSON response',
url,
requestBodyValues: body,
statusCode: 200,
responseHeaders,
responseBody,
});
}
return createEventSourceResponseHandler(xaiChatChunkSchema)({
response,
url,
requestBodyValues: body,
});
},
abortSignal: options.abortSignal,
fetch: this.config.fetch,
});View on GitHub (pinned to 69428b1f8b)
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
Defensive patterns
Strategy: fallback
Validate before calling
null
Type guard
null
Try / catch
import { APICallError } from 'ai';
try {
for await (const part of stream.fullStream) { /* ... */ }
} catch (e) {
if (APICallError.isInstance(e) && e.message === 'Invalid JSON response') {
// inspect e.responseBody; optionally retry once or surface diagnostics
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: Corporate proxies/WAFs injecting HTML into responses; xAI introducing a new error schema before the SDK supports it; gateway timeouts returning malformed bodies.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- ${parsedError.value.error}
- ${error.message}
- ${response.error}
- 'element streams in no-schema mode' functionality not suppor
- 'element streams in object mode' functionality not supported
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/3300b1e0a4afd68c.
Report an issue: GitHub.