Skip to content

feat: Add explicit proxy configuration support to genai.configure() #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions google/generativeai/client.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
from collections.abc import Sequence
import httplib2
from io import IOBase
import sys

import google.ai.generativelanguage as glm
import google.generativeai.protos as protos
@@ -153,6 +154,10 @@ def configure(
client_options: client_options_lib.ClientOptions | dict[str, Any] | None = None,
client_info: gapic_v1.client_info.ClientInfo | None = None,
default_metadata: Sequence[tuple[str, str]] = (),
http_proxy: str | None = None,
https_proxy: str | None = None,
no_proxy: str | None = None,
grpc_proxy: str | None = None
) -> None:
"""Initializes default client configurations using specified parameters or environment variables.
@@ -171,6 +176,14 @@ def configure(
are set, they will be used in this order of priority.
default_metadata: Default (key, value) metadata pairs to send with every request.
when using `transport="rest"` these are sent as HTTP headers.
http_proxy: HTTP proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'http_proxy' environment variable.
https_proxy: HTTPS proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'https_proxy' environment variable.
no_proxy: Comma-separated list of hosts to exclude from proxy.
If provided, sets the 'no_proxy' environment variable.
grpc_proxy: gRPC proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'grpc_proxy' environment variable.
"""
if isinstance(client_options, dict):
client_options = client_options_lib.from_dict(client_options)
@@ -221,6 +234,33 @@ def configure(

self.clients = {}

if http_proxy:
os.environ['http_proxy'] = http_proxy
os.environ['HTTP_PROXY'] = http_proxy

if https_proxy:
os.environ['https_proxy'] = https_proxy
os.environ['HTTPS_PROXY'] = https_proxy

if no_proxy:
os.environ['no_proxy'] = no_proxy
os.environ['NO_PROXY'] = no_proxy

if grpc_proxy:
os.environ['grpc_proxy'] = grpc_proxy

if 'requests' in sys.modules:
import requests
# Create a new session and configure proxies
session = requests.Session()
if http_proxy or https_proxy:
session.proxies.update({
'http': http_proxy,
'https': https_proxy
})
# Patch the Session class to return our configured session
requests.Session = lambda: session

def make_client(self, name):
if name == "file":
cls = FileServiceClient
@@ -312,6 +352,10 @@ def configure(
client_options: client_options_lib.ClientOptions | dict | None = None,
client_info: gapic_v1.client_info.ClientInfo | None = None,
default_metadata: Sequence[tuple[str, str]] = (),
http_proxy: str | None = None,
https_proxy: str | None = None,
no_proxy: str | None = None,
grpc_proxy: str | None = None
):
"""Captures default client configuration.
@@ -329,14 +373,49 @@ def configure(
used.
default_metadata: Default (key, value) metadata pairs to send with every request.
when using `transport="rest"` these are sent as HTTP headers.
http_proxy: HTTP proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'http_proxy' environment variable.
https_proxy: HTTPS proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'https_proxy' environment variable.
no_proxy: Comma-separated list of hosts to exclude from proxy.
If provided, sets the 'no_proxy' environment variable.
grpc_proxy: gRPC proxy URL (e.g., 'http://127.0.0.1:8080').
If provided, sets the 'grpc_proxy' environment variable.
"""
if http_proxy:
os.environ['http_proxy'] = http_proxy
os.environ['HTTP_PROXY'] = http_proxy
if https_proxy:
os.environ['https_proxy'] = https_proxy
os.environ['HTTPS_PROXY'] = https_proxy
if no_proxy:
os.environ['no_proxy'] = no_proxy
os.environ['NO_PROXY'] = no_proxy

if grpc_proxy:
os.environ['grpc_proxy'] = grpc_proxy

if 'requests' in sys.modules:
import requests
session = requests.Session()
if http_proxy or https_proxy:
session.proxies.update({
'http': http_proxy,
'https': https_proxy
})
requests.Session = lambda: session

return _client_manager.configure(
api_key=api_key,
credentials=credentials,
transport=transport,
client_options=client_options,
client_info=client_info,
default_metadata=default_metadata,
http_proxy=http_proxy,
https_proxy=https_proxy,
no_proxy=no_proxy,
grpc_proxy=grpc_proxy
)