diff --git a/custom_nodes/packages/gemini/src/gemini_model.ts b/custom_nodes/packages/gemini/src/gemini_model.ts index 904f33a..89ec46c 100644 --- a/custom_nodes/packages/gemini/src/gemini_model.ts +++ b/custom_nodes/packages/gemini/src/gemini_model.ts @@ -91,6 +91,11 @@ type NestedArray = T extends Array ? never : Array>; export class GeminiModel extends PureFunctionNode { private genAI?: GoogleGenAI; private model?: string; + // The API key the current genAI client was built with. The client pins the + // key at construction time, so we need to remember it separately: by the + // time run() fires, PureFunctionNode has already overwritten lastInputs + // with the new inputs, so it can't be used to detect a key change here. + private genAIKey?: string; constructor() { super(); @@ -111,18 +116,20 @@ export class GeminiModel extends PureFunctionNode { throw new Error('Please set your API key'); } - // Rebuild if api key is different. - if (inputs.apiKey !== this.lastInputs?.apiKey || !this.genAI) { + // Rebuild the client when the api key changes. GoogleGenAI keeps using + // the key it was constructed with, so a new key needs a new client. + if (inputs.apiKey !== this.genAIKey || !this.genAI) { this.genAI = new GoogleGenAI({ apiKey: inputs.apiKey, }); + this.genAIKey = inputs.apiKey; } // Get the selected model. if (!inputs.modelId) { throw new Error('Please select a model'); } - if (inputs.modelId !== this.lastInputs?.modelId || !this.model) { + if (inputs.modelId !== this.model) { this.model = inputs.modelId; }