google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.55k stars 309 forks source link

Failed to upload a file - FailedPrecondition: 400 Unauthorized: Missing authentication token #542

Closed tsuga closed 1 month ago

tsuga commented 1 month ago

Description of the bug:

When I try to upload an image to Gemini, it gives me an error. I am using v0.7.2.

import google.generativeai as genai
genai.configure(api_key=GEMINI_API_KEY)

genai.upload_file("absolutepathto/image.png")

Actual vs expected behavior:

---------------------------------------------------------------------------
_InactiveRpcError                         Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/google/api_core/grpc_helpers.py:76, in _wrap_unary_errors.<locals>.error_remapped_callable(*args, **kwargs)
     75 try:
---> 76     return callable_(*args, **kwargs)
     77 except grpc.RpcError as exc:

File /opt/conda/lib/python3.10/site-packages/grpc/_channel.py:1181, in _UnaryUnaryMultiCallable.__call__(self, request, timeout, metadata, credentials, wait_for_ready, compression)
   1175 (
   1176     state,
   1177     call,
   1178 ) = self._blocking(
   1179     request, timeout, metadata, credentials, wait_for_ready, compression
   1180 )
-> 1181 return _end_unary_response_blocking(state, call, False, None)

File /opt/conda/lib/python3.10/site-packages/grpc/_channel.py:1006, in _end_unary_response_blocking(state, call, with_call, deadline)
   1005 else:
-> 1006     raise _InactiveRpcError(state)

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.FAILED_PRECONDITION
    details = "Unauthorized: Missing authentication token"
    debug_error_string = "UNKNOWN:Error received from peer ipv4:34.128.171.17:443 {created_time:"2024-09-09T13:27:28.392766886+00:00", grpc_status:9, grpc_message:"Unauthorized: Missing authentication token"}"
>

The above exception was the direct cause of the following exception:

FailedPrecondition                        Traceback (most recent call last)
Cell In[42], line 1
----> 1 genai.upload_file(image_path)

File /opt/conda/lib/python3.10/site-packages/google/generativeai/files.py:71, in upload_file(path, mime_type, name, display_name, resumable)
     68 if display_name is None:
     69     display_name = path.name
---> 71 response = client.create_file(
     72     path=path, mime_type=mime_type, name=name, display_name=display_name, resumable=resumable
     73 )
     74 return file_types.File(response)

File /opt/conda/lib/python3.10/site-packages/google/generativeai/client.py:97, in FileServiceClient.create_file(self, path, mime_type, name, display_name, resumable)
     94 request = self._discovery_api.media().upload(body={"file": file}, media_body=media)
     95 result = request.execute()
---> 97 return self.get_file({"name": result["file"]["name"]})

File /opt/conda/lib/python3.10/site-packages/google/ai/generativelanguage_v1beta/services/file_service/client.py:921, in FileServiceClient.get_file(self, request, name, retry, timeout, metadata)
    918 self._validate_universe_domain()
    920 # Send the request.
--> 921 response = rpc(
    922     request,
    923     retry=retry,
    924     timeout=timeout,
    925     metadata=metadata,
    926 )
    928 # Done; return the response.
    929 return response

File /opt/conda/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py:131, in _GapicCallable.__call__(self, timeout, retry, compression, *args, **kwargs)
    128 if self._compression is not None:
    129     kwargs["compression"] = compression
--> 131 return wrapped_func(*args, **kwargs)

File /opt/conda/lib/python3.10/site-packages/google/api_core/grpc_helpers.py:78, in _wrap_unary_errors.<locals>.error_remapped_callable(*args, **kwargs)
     76     return callable_(*args, **kwargs)
     77 except grpc.RpcError as exc:
---> 78     raise exceptions.from_grpc_error(exc) from exc

FailedPrecondition: 400 Unauthorized: Missing authentication token

Any other information you'd like to share?

I've confirmed that the API key is correct by running another text-only prompt.

Gunand3043 commented 1 month ago

Have you tried uploading other files, such as PDFs or audio files?

tsuga commented 1 month ago

I tried with a PDF file. Same error for PDF.

MarkDaoust commented 1 month ago

Are you sure your key is good? This Files notebook works:

https://colab.sandbox.google.com/github/google-gemini/cookbook/blob/main/quickstarts/File_API.ipynb

Is it possible it's picking up on some application-default credentials and getting confused?

tsuga commented 1 month ago

@MarkDaoust Thank you for checking. I've confirm that the key is good. I am on Kaggle environment.

image
MarkDaoust commented 1 month ago

Oh, kaggle! I was just talking to them.

This might be the fix you need: https://github.com/google-gemini/generative-ai-python/pull/548 It's in v0.8.1.

Try updating to 0.8.1 and see if that works. It if doesn't it may be a matter of waiting until Kaggle updates it to 0.8.1, which should be soon, since they were asking me to fix this.

tsuga commented 1 month ago

Thanks. I've updated the library to v0.8.1 and it works without error!