diff --git a/packages/core/src/core/geminiChat.test.ts b/packages/core/src/core/geminiChat.test.ts index 1f60565f0d..bfce4b51dd 100644 --- a/packages/core/src/core/geminiChat.test.ts +++ b/packages/core/src/core/geminiChat.test.ts @@ -1279,13 +1279,10 @@ describe('GeminiChat', () => { expect(mockLogContentRetry).toHaveBeenCalledTimes(1); expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1); - // History should still contain the user message. + // History should NOT contain the failed user message, + // to prevent invalid content from breaking subsequent requests. const history = chat.getHistory(); - expect(history.length).toBe(1); - expect(history[0]).toEqual({ - role: 'user', - parts: [{ text: 'test' }], - }); + expect(history.length).toBe(0); }); describe('API error retry behavior', () => { diff --git a/packages/core/src/core/geminiChat.ts b/packages/core/src/core/geminiChat.ts index 25f32e09a7..1670f99508 100644 --- a/packages/core/src/core/geminiChat.ts +++ b/packages/core/src/core/geminiChat.ts @@ -380,6 +380,8 @@ export class GeminiChat { } if (isConnectionPhase) { + // Remove failed user content so it doesn't break subsequent requests + this.history.pop(); throw error; } lastError = error; @@ -429,6 +431,8 @@ export class GeminiChat { new ContentRetryFailureEvent(maxAttempts, lastError.type, model), ); } + // Remove failed user content so it doesn't break subsequent requests + this.history.pop(); throw lastError; } } finally {