-
Notifications
You must be signed in to change notification settings - Fork 317
fix: After an API error, all subsequent calls fail with the same error #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/methods/chat-session.ts
Outdated
| finalResult = result; | ||
| }); | ||
| await this._sendPromise; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we apply logic similar to https://github.com/google-gemini/generative-ai-js/pull/420/files#diff-250ed4bb03c096e3830327714961f4dbfde48df60b19c2918a2cdb4df06b6df1R203-R213?
As in, apply a .catch instead of a separate try/catch block here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean something like this?
await this._sendPromise.catch((error) => {
this._sendPromise = Promise.resolve();
throw error;
});There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes something like that. Could you check to see if this also works?
.then((result) => {
// ...
finalResult = result;
})
.catch((e) => {
// Resets _sendPromise to avoid subsequent calls failing and throw error.
this._sendPromise = Promise.resolve();
throw e;
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it works! I've already made the change.
…catch() to simplify the code.
src/methods/chat-session.ts
Outdated
| finalResult = result; | ||
| }); | ||
| await this._sendPromise; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes something like that. Could you check to see if this also works?
.then((result) => {
// ...
finalResult = result;
})
.catch((e) => {
// Resets _sendPromise to avoid subsequent calls failing and throw error.
this._sendPromise = Promise.resolve();
throw e;
});
Annhiluc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for making the changes! Could you also help add a unit test for this case in src/methods/chat-session.test.ts?
Something like:
it("generateContent errors should not persist", async () => {
const generateContentStub = stub(
generateContentMethods,
"generateContent",
).rejects("generateContent failed");
const chatSession = new ChatSession("MY_API_KEY", "a-model");
await expect(chatSession.sendMessage("hello")).to.be.rejected;
expect(generateContentStub).to.be.calledWith(
"MY_API_KEY",
"a-model",
match.any,
);
// Subsequent call should succeed.
const UpdatedGenerateContentStub = stub(
generateContentMethods,
"generateContent",
);
await expect(chatSession.sendMessage("hello")).to.not.be.rejected;
expect(UpdatedGenerateContentStub).to.be.calledWith(
"MY_API_KEY",
"a-model",
match.any,
);
});
|
Sure! I've added a unit test for this case in |
Fix #417