skip to Main Content

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


  1. Try this.

    curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY 
        -H 'Content-Type: application/json' 
        -X POST 
        -d '{
            "contents": [{
                "parts":[
                    {"text": "Write a story about a magic backpack."}
                ]
            }],
            "safetySettings": [
                {
                    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                    "threshold": "BLOCK_ONLY_HIGH"
                }
            ],
            "generationConfig": {
                "stopSequences": [
                    "Title"
                ],
                "temperature": 1.0,
                "maxOutputTokens": 800,
                "topP": 0.8,
                "topK": 10,
                "responseMimeType": "application/json"
            }
        }'  2> /dev/null | grep "text"
    
    Login or Signup to reply.
  2. Modification points:

    • In the current stage, in order to use "response_mime_type": "application/json", it is required to use v1beta instead of v1.

    • 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 from contents property.

      {
         "contents":[
            {
               "role":"user",
               "parts":[{"text":"Give me five subcategories of jazz?"}]
            },
            "generation_config":{"response_mime_type":"application/json"}
         ]
      
      • The modified request body is as follows.

        {
           "contents":[
              {
                 "role":"user",
                 "parts":[{"text":"Give me five subcategories of jazz?"}]
              }
           ],
           "generation_config":{"response_mime_type":"application/json"}
        }
        

    When these points are reflected in your curl command, they become as follows:

    Modified curl command:

    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/v1beta/models/gemini-1.5-flash:generateContent"
    

    When this curl command is run, the following result is obtained.

    {
      "candidates": [
        {
          "content": {
            "parts": [
              {
                "text": "["Bebop", "Cool Jazz", "Modal Jazz", "Fusion Jazz", "Latin Jazz"]n"
              }
            ],
            "role": "model"
          },
          "finishReason": "STOP",
          "index": 0,
          "safetyRatings": [
            {
              "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HATE_SPEECH",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HARASSMENT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
              "probability": "NEGLIGIBLE"
            }
          ]
        }
      ],
      "usageMetadata": {
        "promptTokenCount": 9,
        "candidatesTokenCount": 20,
        "totalTokenCount": 29
      }
    }
    

    Note:

    • As additional information, the API key can also be used as the query parameter instead of the request header like https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}.

    Reference:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search