Skip to content

The count tokens function generates an google.api_core.exceptions.PermissionDenied error when using an uploaded file in the prompt using the files api  #365

Closed
@haruiz

Description

@haruiz

Description of the bug:

After uploading a file using the file API and calling the count_tokens functions, I get the following error.

google.api_core.exceptions.PermissionDenied: 403 You do not have permission to access the File 8lqhztqcd6g1 or it may not exist

Here is my code:

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)

model_name = "models/gemini-1.5-pro-latest"
model = genai.GenerativeModel(model_name)

audio_file_path = "./../data/audio_example.mp3"
audio_sample = genai.upload_file(audio_file_path)
model.count_tokens(audio_sample)

Additional info: For some reason, if I comment the model.count_tokens call line, and run the following code, it works:

response = model.generate_content([
    "Describe the following audio:",
    audio_sample
])
print(response)

Actual vs expected behavior:

Return the number of tokens as shown in the notebook: https://github.com/google-gemini/cookbook/blob/main/quickstarts/Counting_Tokens.ipynb

Any other information you'd like to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions