Skip to content

Supporting custom fetch in options for http proxy. #999

@WeiAnAn

Description

@WeiAnAn

Is your feature request related to a problem? Please describe.

The current @google/genai SDK does not offer a direct method to configure an HTTP/HTTPS proxy for its API requests. This makes it challenging to use the SDK's clients in common corporate or restricted network environments that require a proxy.


Describe the solution you'd like

I propose adding a fetchImplementation (or alternatively, fetch) property to the configuration object passed to the SDK's client constructors. This would allow developers to supply their own fetch implementation, making it easy to integrate libraries like http-proxy-agent to correctly route traffic.


Describe alternatives you've considered

  1. Direct Proxy Options: Adding explicit proxy fields (e.g., { proxy: { host: '...' } }) is an option, but it's less flexible than fetchImplementation for handling complex authentication, custom certificates, or different agent behaviors.

  2. Relying on Environment Variables: Using a global environment variable like HTTPS_PROXY is a significant drawback because it affects the entire Node.js process. This forces all outbound HTTPS requests from the application through the proxy, not just requests from this SDK. An explicit fetchImplementation option provides safe, instance-level control without causing unintended side effects.


Additional context

Here is a conceptual code example illustrating how the proposed solution would work.

import { GoogleGenAI } from '@google/genai';
import { fetch } from "undici";
import { ProxyAgent } from "proxy-agent";

const agent = new ProxyAgent();
const customFetchWithProxy = (url, options) => {
  return fetch(url, { ...options, dispatcher: agent });
};

// Pass the custom fetch function during client initialization
const ai = new GoogleGenAI({
  vertexai: true,
  project: 'your_project_id',
  location: 'us-central1',
  fetchImplementation: customFetchWithProxy,
});

Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions