Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(providers): report a truncated Gemini stream instead of a clean stop (#1288)

tashdroid1d70b75dff41
diff --git a/server/src/__tests__/providers/google.test.ts b/server/src/__tests__/providers/google.test.ts
index cdbc4308..0cfa318f 100644
--- a/server/src/__tests__/providers/google.test.ts
+++ b/server/src/__tests__/providers/google.test.ts
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GoogleProvider } from '../../providers/google.js';
+import { isStreamTruncatedError } from '../../lib/error-classify.js';
describe('GoogleProvider', () => {
let provider: GoogleProvider;
@@ -662,6 +663,40 @@ describe('GoogleProvider', () => {
expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('stop');
});
+ // An abrupt EOF — no `[DONE]`, no `finishReason` — is a truncated generation,
+ // not a completion (providers/base.ts:392-397). This adapter used to
+ // synthesize `finish_reason: 'stop'` for it, so the client was told a
+ // half-written answer was complete, the request was logged 'success', and the
+ // route was never benched. Throwing the shared message hands it to
+ // isStreamTruncatedError and the fallback loop's truncation-streak bench.
+ it('throws on an abrupt EOF instead of synthesizing a stop chunk', async () => {
+ vi.spyOn(global, 'fetch').mockResolvedValueOnce(sseResponse([
+ 'data: {"candidates":[{"content":{"parts":[{"text":"Hel"}]}}]}\n\n',
+ 'data: {"candidates":[{"content":{"parts":[{"text":"lo, this answer is cut"}]}}]}\n\n',
+ // body ends here: no [DONE], no finishReason
+ ]));
+
+ const chunks: any[] = [];
+ let thrown: any;
+ try {
+ for await (const c of provider.streamChatCompletion(
+ 'test-key',
+ [{ role: 'user', content: 'Hi' }],
+ 'gemini-2.5-pro',
+ )) chunks.push(c);
+ } catch (err) {
+ thrown = err;
+ }
+
+ expect(thrown).toBeInstanceOf(Error);
+ // The classifier the fallback loop consults must recognize it.
+ expect(isStreamTruncatedError(thrown)).toBe(true);
+ // The content already streamed is still delivered; what must NOT appear is
+ // a terminal chunk claiming the truncated answer finished normally.
+ expect(chunks.map(c => c.choices[0].delta.content ?? '').join('')).toBe('Hello, this answer is cut');
+ expect(chunks.some(c => c.choices[0].finish_reason)).toBe(false);
+ });
+
it('streams Gemini thought parts as reasoning_content, not visible content (#539)', async () => {
vi.spyOn(global, 'fetch').mockResolvedValueOnce(sseResponse([
'data: {"candidates":[{"content":{"parts":[{"text":"private","thought":true}]}}]}\n\n',
diff --git a/server/src/providers/google.ts b/server/src/providers/google.ts
index f8122354..0ef71b8b 100644
--- a/server/src/providers/google.ts
+++ b/server/src/providers/google.ts
@@ -798,18 +798,24 @@ export class GoogleProvider extends BaseProvider {
reader.cancel().catch(() => { /* upstream already gone */ });
}
+ // Reaching here means the body ended with neither `[DONE]` nor any
+ // `finishReason` — both legitimate terminators `return` from inside the
+ // loop above, so the only way out to this point is the `if (done) break`
+ // on an abrupt EOF (an h2 END_STREAM from an edge, or the backend cutting
+ // the generation mid-answer).
+ //
+ // This used to synthesize `finish_reason: 'stop'`, which told the client a
+ // half-written answer had completed normally: no failover, the request row
+ // logged 'success', and the route never benched. base.ts:392-397 states the
+ // opposite contract for every adapter that goes through readSseStream —
+ // "a stream that ends without [DONE] AND without any finish_reason is a
+ // truncated generation, not a completion" — and throws (base.ts:471). This
+ // adapter parses Gemini's own frame format and reads the body itself, so it
+ // never inherited that. Throw the same message: isStreamTruncatedError
+ // (lib/error-classify.ts:685) matches on it, and the fallback loop already
+ // fails over and bench-counts the streak (lib/fallback-loop.ts:485).
if (!emittedFinish) {
- yield {
- id,
- object: 'chat.completion.chunk',
- created: Math.floor(Date.now() / 1000),
- model: modelId,
- choices: [{
- index: 0,
- delta: {},
- finish_reason: sawToolCalls ? 'tool_calls' : 'stop',
- }],
- };
+ throw new Error(`${this.name} stream ended unexpectedly (no [DONE], no finish_reason) — connection reset or truncated upstream`);
}
}