GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent: [400 Bad Request] Invalid or unsupported file uri: https://storage.googleapis.com/bucket_for_fyp_images/1722401114016_test.webp
I get the same error and couldn’t find a solution about this. It was working fine last week but now it doesn’t work. Sometimes i get 404 sometimes 400.
Welcome to the forums!
If you are setting a fileData
part, the URL for it can only be from specific sources.
If you are using the AI Studio API, the source must be a URL that you get from the File API.
While the Vertex AI API can use files stored in Google Cloud, you need to use a gs:
URL, and the bucket needs to be in the same project that is used to call the Gemini API.
Hello,
I am currently integrating the Google Generative AI API (specifically, the Gemini 1.5 Pro model) into my React Native application and have encountered an issue related to the file URI format. Below is the relevant portion of my code:
App.js: (React-Native)
import React, { useState, useEffect } from ‘react’;
import { View, Text } from ‘react-native’;
import { GoogleGenerativeAI } from ‘@google/generative-ai’;
const GEMINI_API_KEY = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
const model = genAI.getGenerativeModel({
model: “gemini-1.5-pro”,
});
const App = () => {
const [generatedResponse, setGeneratedResponse] = useState(‘’);
const fileData = {
fileUri: ‘https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4’,
mimeType: ‘video/mp4’,
};
const prompt = “Describe this video clip”;
const videoPart = {
fileData: {
fileUri: fileData.fileUri,
mimeType: fileData.mimeType,
},
};
const generateContent = async () => {
try {
const result = await model.generateContent(prompt);
const responseText = await result.response.text();
setGeneratedResponse(responseText);
} catch (error) {
console.error(‘Error generating content’, error);
}
};
useEffect(() => {
generateContent();
}, );
return (
<View style={{ padding: 20 }}>
<Text style={{ fontSize: 24, fontWeight: ‘bold’ }}>Generated Response
<Text style={{ marginTop: 10 }}>{generatedResponse}
);
};
export default App;
Output:
Issue Description:
I am receiving a 400 error indicating an “Invalid or unsupported file uri.” It appears the file URI being used might not be supported by the API.
Could you please provide guidance on the correct format for the fileUri
or any additional steps needed to resolve this issue?
You can’t just use a GCS public URL for the fileData
part.
If you are using the AI Studio API (which is what you illustrate), you need to use the File API to store the file and use the URL it returns.
If you are using Vertex AI, you will store it in GCS and use the gs:
URI for that resource.