Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ export class Chat {
this.recordHistory(inputContent, modelOutput);
return;
})();
await this.sendPromise;
await this.sendPromise.catch(() => {
// Resets sendPromise to avoid subsequent calls failing
this.sendPromise = Promise.resolve();
});
return responsePromise;
}

Expand Down
35 changes: 35 additions & 0 deletions test/unit/chats_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,41 @@ describe('sendMessage valid response', () => {
});
});

describe('sendMessage subsequent calls', () => {
it('sendMessage errors should not persist', async () => {
const client = new GoogleGenAI({vertexai: false, apiKey: 'fake-api-key'});
const chat = client.chats.create({model: 'gemini-1.5-flash'});

const modelsModule = client.models;
spyOn(modelsModule, 'generateContent').and.returnValue(
Promise.reject('test error'),
);

const message1 = chat.sendMessage({message: 'send message 1'});
await expectAsync(message1).toBeRejectedWith('test error');
Comment thread
josejefferson marked this conversation as resolved.
Resolved

const successResponse = new GenerateContentResponse();
successResponse.candidates = [
{
content: {
role: 'model',
parts: [
{
text: 'valid response',
},
],
},
},
];
modelsModule.generateContent = jasmine
.createSpy('generateContent')
.and.returnValue(Promise.resolve(successResponse));

const message2 = chat.sendMessage({message: 'send message 2'});
await expectAsync(message2).toBeResolvedTo(successResponse);
});
});

describe('GenerateContent response schema', () => {
it('smoke test GenerateContent response schema with Array', async () => {
const client = new GoogleGenAI({vertexai: false, apiKey: 'fake-api-key'});
Expand Down