Skip to content

Conversation

Kartikayy007
Copy link

@Kartikayy007 Kartikayy007 commented Mar 10, 2025

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
Loading

Motivation

Type of change

Feature request

Checklist

  • I have performed a self-review of my code
  • I have added detailed comments to my code where applicable
  • I have verified that my change does not break existing code
  • My PR is based on the latest changes of the main branch
  • I am familiar with the Google Style Guide
  • 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.

@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:python sdk Issue/PR related to Python SDK labels Mar 10, 2025
@Kartikayy007
Copy link
Author

@evansenter please review

@Annhiluc Annhiluc added type:feature request New feature request/enhancement status:triaged Issue/PR triaged to the corresponding sub-team p3 labels Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK p3 status:awaiting review PR awaiting review from a maintainer status:triaged Issue/PR triaged to the corresponding sub-team type:feature request New feature request/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants