Krishh
Krishh

Reputation: 667

Gemini Python API Deadline Exceeded

I have been trying to use Google's Gemini API using python, and I am running into continuous 504 Deadline Exceeded. This is also not a one of thing as well, I have tried 20+ times using python SDK and it failed everytime, while curl returned in 5-6 seconds everytime.

I initially thought this was a network problem, but a simple curl calls works well.

I have a very basic starter script:

genai.configure(api_key=key)
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("What is the meaning of life")
print(response.text)

The response I get:

Traceback (most recent call last):
  File "../test.py", line 5, in <module>
    response = model.generate_content("What is the meaning of life.")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/generativeai/generative_models.py", line 232, in generate_content
    response = self._client.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 566, in generate_content
    response = rpc(
               ^^^^
  File "../env/lib/python3.12/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
           ^^^^^^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File ..env/lib/python3.12/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "../env/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
    result = target()
             ^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "../env/lib/python3.12/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.DeadlineExceeded: 504 Deadline Exceeded

The curl call though works as expected:

curl -H 'Content-Type: application/json' \
    -d '{"contents":[{"parts":[{"text":"What is the meaning of life"}]}]}' \
    -X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={key}'

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "In the life....


Can anyone help me figure out what is wrong here?

Upvotes: 3

Views: 4235

Answers (2)

Joseph Cardwell
Joseph Cardwell

Reputation: 339

This worked for me:

response = model.generate_content(
    contents=messages, 
    request_options={"timeout": 1000}
)

Upvotes: 2

Ashish Johnson
Ashish Johnson

Reputation: 467

I had this exact same issue while using Gemini embedding model via python SDK inside for loop, Although I haven't tried CURL call.

What I'm aware is that there is a 60 request/min rate limit on Gemini service & other GCP services as well. 504 Deadline Exceeded. To overcome this, you can add a try/except block and put a sleep of 120 sec using python time module.

Try the below code:

model = genai.GenerativeModel('gemini-pro')
try:
    response = model.generate_content("What is the meaning of life")
    
except Exception as e
    time.sleep(120)
    response = model.generate_content("What is the meaning of life")
    

If the above solution does not work then I think someone might have access to your API key and they using it. So simply refresh your key.

Upvotes: 0

Related Questions