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.4k stars 274 forks source link

Allow specification of `timeout` variable for `GenerativeModel.generate_content` and `GenerativeModel.generate_content_async` #182

Closed spencermyoung513 closed 7 months ago

spencermyoung513 commented 7 months ago

Description of the feature request:

Currently, when making a request to the Gemini Pro Vision API via GenerativeModel.generate_content or GenerativeModel.generate_content_async, there is no easy way to change the default timeout value on the request. It would be great to have this as a parameter (perhaps just as a non-documented kwarg) in the generate_content and other sibling methods.

What problem are you trying to solve with this feature?

Sometimes, API requests with multiple images or lots of tokens take longer than 60 seconds to get a response. But with a hard-enforced timeout setting of 60 seconds, these requests fail no matter what.

Any other information you'd like to share?

For the GenerativeServiceAsyncClient, at least, timeout is a parameter of many methods like generate_content. It is not a parameter of the GenerativeModel.generate_content method (or any of its siblings), however.

This could be amended currently by creating a custom client class that inherits from, say, GenerativeServiceAsyncClient and overriding the internal _client or _async_client attributes of a GenerativeModel. Such a workaround is far from elegant though, and creates a lot of pain on the developer side.

jun0wanan commented 7 months ago

Description of the feature request:

Currently, when making a request to the Gemini Pro Vision API via GenerativeModel.generate_content or GenerativeModel.generate_content_async, there is no easy way to change the default timeout value on the request. It would be great to have this as a parameter (perhaps just as a non-documented kwarg) in the generate_content and other sibling methods.

What problem are you trying to solve with this feature?

Sometimes, API requests with multiple images or lots of tokens take longer than 60 seconds to get a response. But with a hard-enforced timeout setting of 60 seconds, these requests fail no matter what.

Any other information you'd like to share?

For the GenerativeServiceAsyncClient, at least, timeout is a parameter of many methods like generate_content. It is not a parameter of the GenerativeModel.generate_content method (or any of its siblings), however.

This could be amended currently by creating a custom client class that inherits from, say, GenerativeServiceAsyncClient and overriding the internal _client or _async_client attributes of a GenerativeModel. Such a workaround is far from elegant though, and creates a lot of pain on the developer side.

Now, How can we use it ?

response = model.generate_content(contents=[],generation_config=dict( candidate_count=1, max_output_tokens=4096, temperature=0, ) )