generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Request for Proxy Configuration Support in google-generativeai Library to Route Traffic Through Proxies

Open Alexmalab opened this issue 1 year ago • 8 comments

Description of the feature request:

I need to use the google-generativeai Python library with routing its api requests through a proxy. However, the library doesn't natively support passing proxy configurations directly when initializing the client.

Feature Request

Add support for passing proxy configurations (e.g., as part of client_options) directly to the google-generativeai client, ensuring all network requests—including file uploads—are routed through the proxy.

What problem are you trying to solve with this feature?

This would avoid the need for global proxy settings or reverse proxies, offering a cleaner and more robust solution in network-restricted environments.

Any other information you'd like to share?

I have tried the following approaches, each with limitations:

  1. Global Environment Variables (os.environ['HTTP_PROXY'], os.environ['HTTPS_PROXY)

    • This works but affects all network requests globally, which is problematic in my multi-threaded, multi-process environment of the project. Managing when to apply or remove these settings isn't practical.
  2. Reverse Proxy with Custom API Endpoint

    • Since genai.configure() supports api_endpoint parameter, I created a custom route on my project that routes requests as a reverse proxy. This works for model.generate_content function but fails with the genai.upload_file function, as it hardcodes Google's endpoint, bypassing the custom API endpoint.

Alexmalab avatar Oct 18 '24 13:10 Alexmalab

Also have similar requirements, and I hope the team can support this feature

ethanshancn avatar Oct 26 '24 13:10 ethanshancn

FYI, I've given up on this and created a separate utility function for file uploads. Use aiohttp instead of requests for async. It wraps the result in genai File object, so, can be natively used with genai code

toolittlecakes avatar Dec 15 '24 07:12 toolittlecakes

If someone is behinde proxy , all call simply return 403 , and there is no way to make it works unfortunately , SDK do not respect ENV variable , and no option to configure the proxy, WHY? we don't know!

mbaneshi avatar Dec 18 '24 23:12 mbaneshi

just set global environment variables grpc_proxy, see doc gRPC environment variables

AnoyiX avatar Dec 19 '24 10:12 AnoyiX

Thanks @AnoyiX setting grpc_proxy , resolved issue.

mbaneshi avatar Dec 19 '24 21:12 mbaneshi

just set global environment variables grpc_proxy, see doc gRPC environment variables

however, the genai.upload_file still doesnt work, right?

FlyingStarlight avatar Jan 02 '25 14:01 FlyingStarlight

The FileServiceClient class uses an httplib2.Http() instance without any proxy configuration

demoncoder-crypto avatar Mar 10 '25 03:03 demoncoder-crypto

I tried to solve it hopefully

demoncoder-crypto avatar Mar 10 '25 04:03 demoncoder-crypto