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.62k stars 322 forks source link

User location is not supported for the API use #587

Open lmyslinski opened 1 month ago

lmyslinski commented 1 month ago

Description of the bug:

My production just went down out of the blue, after 2 weeks since integrating Gemini.

Actual vs expected behavior:

No response

Any other information you'd like to share?

I'm currently on the Pay-as-you-go plan.

pyproject.toml:

google-generativeai = "^0.8.1"

I'm using the most basic config:

import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_AI_KEY"])

model = genai.GenerativeModel(
    model_name="gemini-1.5-flash",
    system_instruction="...",
)

file = genai.upload_file(path=file_path, display_name=file_name)

Stack trace:

ResumableUploadError: <HttpError 400 when requesting None returned "User location is not supported for the API use.". Details: "User location is not supported for the API use.">
  File "starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "starlette/middleware/base.py", line 108, in __call__
    response = await self.dispatch_func(request, call_next)
  File "main.py", line 60, in logging_middleware
    response: Response = await call_next(request)
  File "starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "starlette/middleware/cors.py", line 83, in __call__
    await self.app(scope, receive, send)
  File "starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "starlette/routing.py", line 66, in app
    response = await func(request)
  File "fastapi/routing.py", line 274, in app
    raw_response = await run_endpoint_function(
  File "fastapi/routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
    (app stack omitted)
        file = genai.upload_file(path=file_path, display_name=file_name)
  File "/app/.venv/lib/python3.11/site-packages/google/generativeai/files.py", line 71, in upload_file
    response = client.create_file(
  File "/app/.venv/lib/python3.11/site-packages/google/generativeai/client.py", line 114, in create_file
    result = request.execute()
  File "googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "googleapiclient/http.py", line 902, in execute
    _, body = self.next_chunk(http=http, num_retries=num_retries)
  File "googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "googleapiclient/http.py", line 1022, in next_chunk
    raise ResumableUploadError(resp, content)

My servers are located in Warsaw. I can successfully run from localhost also from Poland, but all of my prod requests are failing.

lmyslinski commented 1 month ago

I was able to workaround the problem by moving my servers to Sweden. Still, I cannot depend service that can just randomly start denying my requests. The issue has been ongoing for 6 hours (and still is) until I was able to get down to "fix" it (it's a weekend after all)

Alexmalab commented 1 month ago

If it supports proxy configuration, you can try routing it through the Sweden network.

vTuanpham commented 3 weeks ago

+1 having the same problem, hoping a fix is coming

vTuanpham commented 3 weeks ago

Current workaround seem to be VPN or just simply switch the VM location.

WoWnik commented 1 day ago

My server in Finland also started getting error 400. After moving to the Polish server everything was fine. But within 2 weeks I started getting error 400. After two weeks the error disappeared. And after another two weeks it appeared again.

It seems Google blocks requests from certain IP addresses, then unblocks them, and everything starts all over again. Why and for what reason?