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

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

Open Kartikayy007 opened this issue 10 months ago • 1 comments

Description of the change

Added explicit proxy configuration support to genai.configure() with:

  • HTTP/HTTPS proxy configuration via http_proxy/https_proxy parameters
  • gRPC proxy support through grpc_proxy parameter
  • No-proxy list specification using no_proxy
  • Automatic Requests session configuration
  • Dual environment variable setup (lowercase + uppercase)
  • End-to-end proxy handling for all API calls
sequenceDiagram
    participant User
    participant SDK
    participant Proxy
    participant GeminiAPI
    
    User->>SDK: genai.configure(<br>http_proxy="proxy.example:8080",<br>https_proxy="proxy.example:8080")
    SDK->>Proxy: Set environment variables
    SDK->>Proxy: Configure requests.Session
    User->>SDK: generate_content()
    SDK->>Proxy: Route request through
    Proxy->>GeminiAPI: Forward API call
    GeminiAPI-->>Proxy: Response
    Proxy-->>SDK: Return result
    SDK-->>User: Final response

Motivation

  • Solves connection issues in proxy-restricted environments (#356)
  • Provides first-class proxy configuration instead of relying solely on env vars
  • Aligns with Python ecosystem proxy conventions
  • Enables enterprise network configurations
  • Supports both REST and gRPC transports

Type of change

Feature request

Checklist

  • [x] I have performed a self-review of my code
  • [x] I have added detailed comments to my code where applicable
  • [x] I have verified that my change does not break existing code
  • [x] My PR is based on the latest changes of the main branch
  • [x] I am familiar with the Google Style Guide
  • [x] I have read through the Contributing Guide

Usage Examples

Explicit configuration:

import google.generativeai as genai

genai.configure(
    api_key="YOUR_API_KEY",
    http_proxy="http://corp-proxy:8080",
    https_proxy="http://corp-proxy:8080",
    no_proxy="localhost,127.0.0.1",
    grpc_proxy="http://corp-proxy:8081"
)

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Explain quantum physics")

Environment variable fallback:

import os
import google.generativeai as genai

os.environ["HTTP_PROXY"] = "http://corp-proxy:8080"
os.environ["HTTPS_PROXY"] = "http://corp-proxy:8080"

genai.configure(api_key="YOUR_API_KEY")  # Auto-detects proxy config

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Explain quantum physics")

Mixed configuration:

import google.generativeai as genai

# Explicit config takes priority over env vars
genai.configure(
    https_proxy="http://alt-proxy:8080",
    api_key="YOUR_API_KEY"
)

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Explain quantum physics") 
# Uses https_proxy="http://alt-proxy:8080" regardless of env vars

Note: Test cases will be added in a follow-up PR after initial code review.

Kartikayy007 avatar Mar 10 '25 10:03 Kartikayy007

@evansenter please review

Kartikayy007 avatar Mar 10 '25 20:03 Kartikayy007