Genai.list_tuned_models() returning ACCESS_TOKEN_SCOPE_INSUFFICIENT

Hello,

I have being able to successfully use my credentials to use a tuned model for instance, but when trying to call genai.list_tuned_models() it outputs an error: 403 Request had insufficient authentication scopes. [reason: “ACCESS_TOKEN_SCOPE_INSUFFICIENT”

import pprint
import google.generativeai as genai
from load_creds import load_creds

creds = load_creds()

genai.configure(credentials=creds)
generation_config = {
    "temperature": 0.9, 
    "top_p": 0.95,
    "top_k": 64,
    "max_output_tokens": 1024,
    "response_mime_type": "text/plain",
}

# Convert the generator to a list
list1 = list(genai.list_tuned_models())

# Use pprint to print the list in a readable format
pprint.pprint(list1)

what could be the problem ?

Regards

Hi @Sandro_Jakoska

Unlike the other endpoints which use API keys, model tuning uses OAuth.

Here’s a tutorial to get you started.

The correct setup will look like:

from google.colab import userdata
import pathlib
pathlib.Path('client_secret.json').write_text(userdata.get('CLIENT_SECRET'))
# Use `--no-browser` in colab

!gcloud auth application-default login --no-browser --client-id-file client_secret.json --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning'
     

!pip install -q -U google-generativeai
import google.generativeai as genai

You can check your existing tuned models with the genai.list_tuned_model method.

for m in genai.list_tuned_models():
  print(m.name)

Hello @sps thank you for your reply. What it feels weird is that the code bellow uses OAth and prompts the tuned model without a problem…

import pprint
import google.generativeai as genai
from load_creds import load_creds

creds = load_creds()

genai.configure(credentials=creds)
generation_config = {
    "temperature": 0.9, 
    "top_p": 0.95,
    "top_k": 64,
    "max_output_tokens": 1024,
    "response_mime_type": "text/plain",
}

# Create a GenerativeModel instance
model = genai.GenerativeModel(
    model_name="tunedModels/custom-interact-sentiment-analysis-zn037",
    generation_config=generation_config,
)

# Start a chat session
chat_session = model.start_chat(enable_automatic_function_calling=True)

# Send a message to the chat session including the file URI
response = chat_session.send_message(f"'Estou feliz'. Apenas classifique positivo, neutro, ou negativo, nenhuma explicação é necessária.")

# Print the response
print(response.text)

But when I call the method to list the tuned models

import pprint
import google.generativeai as genai
from load_creds import load_creds

creds = load_creds()

genai.configure(credentials=creds)

for m in genai.list_tuned_models():
  print(m.name)

It gives me PermissionDenied: 403 Request had insufficient authentication scopes. [reason: “ACCESS_TOKEN_SCOPE_INSUFFICIENT”

You need to do OAuth with the scopes:

"https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning"

Hi, tried adding manually the scopes you mentioned on the OAth consent screen.

but still get the same error.

Looking at the token.json generated , it contain this line “scopes”: [“https://www.googleapis.com/auth/generative-language.retriever”],

Do I need to include the scopes manually in the token ?