call api use gemini 2.0 flash exp model, response 400 bad request.
I can call gemini-2.0-flash-exp with this code snippet within node.js
import { GoogleGenerativeAI } from '@google/generative-ai'
...
const gemini = genAI.getGenerativeModel({
model: 'gemini-2.0-flash-exp',
})
What url are you using to call 2.0 Flash?
Could you give us some snippets on what you’re working with? Would be great to know context.
Happy coding!
Hat
Below is the C# code, maybe I should try it with python
When using the “gemini-exp-1121” model, the results can be returned correctly, but when using “gemini-2.0-flash-exp” a 400 error status code is returned
private static readonly string _apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{modelName}:generateContent";
private static readonly string _modelName = "gemini-exp-1121";
public void CallGemini()
{
string finalApiUrl = _apiUrl.Replace("{modelName}", _modelName);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string payload = GeneratePayload("hello");
var request = new HttpRequestMessage(HttpMethod.Post, $"{finalApiUrl}?key={_apiKey}");
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");;
try
{
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
SaveInfoToJson(w.Key, w.Value, responseContent);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
private static string GeneratePayload(string prompt)
{
var payload = new
{
contents = new[]
{
new
{
parts = new
{
text = prompt,
}
},
},
generation_config = new
{
temperature = 0.4,
top_p = 0.95,
top_k = 64,
max_output_tokens = 2500
},
safety_settings = new[]
{
new {
category= "HARM_CATEGORY_HARASSMENT",
threshold= "BLOCK_NONE",
},
new {
category= "HARM_CATEGORY_HATE_SPEECH",
threshold= "BLOCK_NONE",
},
new {
category= "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold= "BLOCK_NONE",
},
new {
category= "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold= "BLOCK_NONE",
}
}
};
return JsonConvert.SerializeObject(payload);
}
The problem is with GeneratePayload
. The Content
type (Caching | Gemini API | Google AI for Developers) needs two things: parts
and role
which you can hardcode to the string user
in your simple example. But that role
is missing. I think the experimental model 1121 was less picky and let you get away with it. It let me get away with an empty tools
section and gemini-2.0-flash-exp
didn’t like that either.
@OrangiaNebula @Ruediger_Seiffert @HatKid thank you very much
after add role information in payload and remove config and settings, the call was successful. Thank you.
Hi Andrew,
In case that you would like to avoid all the painful request creation and response handling, I recommend to have a look at the available NuGet package for .NET
It’s three lines of code.
using Mscc.GenerativeAI;
var genAi = new GoogleAI(apiKey);
var model = genAi.GenerativeModel(Model.Gemini20FlashExperimental);
var response = await model.GenerateContent("your prompt");
Console.WriteLine(response.Text);
PS: The way you use the HttpClient
might lead to port depletion depending on the use case. Because garbage collection happens any time later.