I’m trying to make a chatbot using Google Gemini AI in NodeJS v.20. I got this error when I try to send a prompt (question) to Gemini AI. I don’t know why it happens.
@google_generative-ai.js?v=3edcef02:553 Uncaught (in promise) TypeError: request is not iterable
at formatNewContent (@google_generative-ai.js?v=3edcef02:553:32)
at ChatSession.sendMessage (@google_generative-ai.js?v=3edcef02:724:24)
at async run (gemini.js:61:18)
at async onSent (context.jsx:18:5)
So, I was actually trying to make a function where after I type something in input tag and then click the send_icon img, the AI will create a new response. Here’s how the code is.
<input
onChange={(e) => setInput(e.target.value)}
value={input}
type="text"
placeholder="Enter prompt here"
/>
<div>
<img src={assets.gallery_icon} alt="" />
<img src={assets.mic_icon} alt="" />
<img onClick={() => onSent()} src={assets.send_icon} alt="" />
</div>
And I also have made the ContextProvider for the AI to generate a response:
import { createContext, useState } from "react";
import runChat from "../config/gemini";
export const Context = createContext();
const ContextProvider = (props) => {
const [input, setInput] = useState("");
const [recentPrompt, setRecentPrompt] = useState("");
const [previousPrompts, setPreviousPrompts] = useState([]);
const [showResult, setShowResult] = useState(false);
const [loading, setLoading] = useState(false);
const [resultData, setResultData] = useState("");
const onSent = async (prompt) => {
setResultData("");
setLoading(true);
setShowResult(true);
await runChat(input);
const response = await runChat(input);
setResultData(response);
setLoading(false);
setInput("");
};
const contextValue = {
previousPrompts,
setPreviousPrompts,
onSent,
setRecentPrompt,
recentPrompt,
showResult,
loading,
resultData,
input,
setInput,
};
return (
<Context.Provider value={contextValue}>{props.children}</Context.Provider>
);
};
export default ContextProvider;
Here’s the code for the chat session in my gemini API config
async function run() {
const chatSession = model.startChat({
generationConfig,
safetySettings,
history: [],
// See https://ai.google.dev/gemini-api/docs/safety-settings
});
const result = await chatSession.sendMessage(prompt);
console.log(result.response.text());
return result.response.text();
}
export default run;
So, I was expecting that when I run the web. Type something in the input box and click the send_icon image. The AI will generate a new response but instead it responded with such error. Is there anyone who can help me fix this issue?
Ryuu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Another possible problem is that prompt
may be of the wrong type. It should be a string, per this related Q&A:
/a/78104652/27165024
4