Thumbnail

Fixed: Gemini Pro cURL Query Error: “Please use a valid role: user, model.”

· 1 min read

Fixed: Gemini Pro cURL Error: “Please use a valid role: user, model.”

What causes this error?
Omitting the required "role" field in the JSON payload or using an access token that doesn’t represent the intended service account.

Fix:

-> Replace your original cURL command with this version:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token --impersonate-service-account <service-account-email>)" \
  -H "Content-Type: application/json" \
  -d '{
        "contents": {
          "role": "user",
          "parts": [{
            "text": "shortest sentence with 4 colors and 5 shapes"
          }]
        },
        "generationConfig": {
          "temperature": 0.2,
          "maxOutputTokens": 512
        }
      }' \
  "https://us-west1-aiplatform.googleapis.com/v1/projects/<project-id>/locations/us-west1/publishers/google/models/gemini-pro:generateContent"

Quick Token Check

Verify that your access token is for the correct service account.

-> Run the following command:

accessToken=$(gcloud auth print-access-token --impersonate-service-account <service-account-email>)
curl -i "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=$accessToken"

This streamlined solution ensures that you’re including the necessary "role" field and using the proper service account token.

Still stuck with the issue?

Drop me an email at dhanuskkar@cybermindworks.com (response time within 3 hrs)

Dhanushkkar

About Dhanushkkar

Sales Executive

Man with headphones

CONTACT US

How can we at CMW help?

Want to build something like this for yourself? Reach out to us!

Link copied
Copyright © 2025 CyberMind Works. All rights reserved.