How can I retrieve data in JSON format from the Gemini 1.5 API using curl?
The code below works correctly:
curl -H 'Content-Type: application/json' -H "x-goog-api-key: ${API_KEY}" -d '{"contents":[
{"role": "user",
"parts":[{"text": "Give me five subcategories of jazz?"}]}]}' "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent"
But when I try to add:
,"generation_config": {"response_mime_type": "application/json"}}
Giving:
curl -H 'Content-Type: application/json' -H "x-goog-api-key: ${API_KEY}" -d '{"contents":[
{"role": "user",
"parts":[{"text": "Give me five subcategories of jazz?"}]},"generation_config": {"response_mime_type": "application/json"},]' "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent"
I receive this error response:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name "response_mime_type" at 'generation_config': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "generation_config",
"description": "Invalid JSON payload received. Unknown name "response_mime_type" at 'generation_config': Cannot find field."
}
]
}
]
}
I’ve tried with several different models (e.g. gemini-1.5-pro-latest
) always with the same result.
2
Answers
Try this.
Modification points:
In the current stage, in order to use
"response_mime_type": "application/json"
, it is required to usev1beta
instead ofv1
.Also, in your showing curl command, the request body is as follows. This request body is not enclosed by
}
. And,generation_config
is required to be moved fromcontents
property.The modified request body is as follows.
When these points are reflected in your curl command, they become as follows:
Modified curl command:
When this curl command is run, the following result is obtained.
Note:
https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}
.Reference: