In my app, I’m making requests to gemini from the backend and returning the response to the frontend but for some reason the backend breaks and throws me the following error:
[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent: fetch failed
At a glance, that URL looks correct.
Without seeing more of your code or a more detailed error message, it is pretty difficult to diagnose what might be going on.
Here is some of my server side code:
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { genericChat } from "@/components/global/gemini_ai_endpoint"
import { jsonrepair } from 'jsonrepair'
type Data = {
ai_prompt:string | {type:string, content:string}[]
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>,
) {
if (req.method !== "POST") {
res.status(400).send({ ai_prompt: "Bad Request: Unsupported HTTP method" })
return
}
try {
const { prompt, user_information } = JSON.parse(req.body)
const prompt_modified = `${prompt}\n [APP_HINT]: User Information : ${JSON.stringify(user_information)}`
const response = await genericChat.sendMessage(prompt_modified)
const response_text = response.response.text()
res.status(200).send({ai_prompt: JSON.parse(jsonrepair(response_text))})
} catch (e) {
res.status(500).send({ai_prompt: [{type:"Paragraph", content: String(e)}]})
}
}
genericChat is just a variable that instances a model chat:
// components/global/gemini_ai_endpoint
const genericChat = model.startChat({})
1 Like
Where and how in your code are you showing that error?
I’m wondering if there is an HTTP error code associated with the reply. Because it feels like there isn’t enough info normally associated with an error.
Yeah, nevermind, I upgraded to node-20 from node-18 and now the bug is magically gone. Thanks though.