Task: Develop error handler for AI chat
Develop error handler for AI chat
Judging by the statistics, it is not uncommon for a request to an AI agent to be processed incorrectly on the AI provider's side. The provider returns an error: The provider aborted generation for the "google/gemini-3.5-flash-lite" model: the response was truncated or corrupted. Please repeat the request.
Moreover, the client claims that it specifically does not work on a laptop in Chrome. That is, they send a request and get an error. Everything works from a mobile phone. As an option, http streamable might not be supported in the browser. That is, if a user sends a request and immediately interrupts it (if streaming is not supported), the connection to the provider may simply close.
Ворклоги
Here is what ChatGPT says:
Yes, a request abortion in the browser can lead to the cancellation of generation at the provider's end — if your backend links these two connections and passes the cancellation downstream. However, the lack of streaming response support in modern Chrome is an unlikely cause: fetch() supports reading responses via ReadableStream. MDN
Here we need to separate two segments: browser → your server and your server → provider. If the browser aborted the request, the server can cancel the request to the provider using AbortController. In that case, the error handler might indeed mistake this cancellation for "the provider interrupted the generation." The error text alone does not prove who aborted the connection first. Cancellation via AbortSignal can terminate an HTTP request with an AbortError. Node.js v26.10.0 Documentation
You can check API availability directly in the browser console:
const supportsResponseStreaming =
typeof fetch === 'function' &&
typeof ReadableStream === 'function' &&
typeof Response === 'function' &&
'body' in Response.prototype &&
typeof ReadableStream.prototype.getReader === 'function';
console.log({ supportsResponseStreaming });
This checks support for reading a streaming response. There is no separate browser function called "HTTP Streamable supported"; if we are talking about a regular request with JSON and a streaming agent response, this exact capability is needed. Streaming request body sending is a different capability with its own limitations. MDN
But true does not guarantee that the stream passes normally through the entire chain. For this, you need a test endpoint on your server that sends, for example, five messages at one-second intervals, and a check in the browser:
async function testStream(url) {
const started = performance.now();
const response = await fetch(url, { cache: 'no-store' });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
if (!response.body) throw new Error('No response body');
const reader = response.body.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log({
elapsedMs: Math.round(performance.now() - started),
bytes: value.byteLength,
});
}
console.log('Stream completed');
} catch (error) {
console.error('Error reading stream:', error);
} finally {
reader.releaseLock();
}
}
testStream('/api/stream-test');
The address here is conditional — the endpoint needs to be implemented. Reading via getReader() allows you to observe the arrival of response chunks. Network chunk boundaries do not have to match server message boundaries. MDN
Interpretation:
- Chunks arrive gradually — the stream is going through.
- Everything arrives in one chunk at the end — buffering is happening somewhere.
- Reading fails with an error — the connection or stream processing is broken; the cause requires correlation with the server log.
For your case, the decisive check is the order of events in the logs of a single request: client disconnection, calling request cancellation to the provider, provider response reading error. If the client disconnects first and then your code cancels the generation, the hypothesis is confirmed. If the stream from the provider is corrupted first, the cause lies in a different segment.
The "laptop / mobile" difference by itself only indicates a difference in conditions: network, VPN, extensions, frontend version, or its request cancellation logic. It is not possible to pinpoint a specific cause based on this sign alone.