I want to use my proxy server with ChatGoogleGenerativeAI method in langchain.
I changed the baseUrl by baseUrl parameter, however, I couldn’t find a parameter or a manner to append a new pair of key-value into the header for authorization to pass my proxy API authentication.
here is what I have done so far, but it doesn’t add the authorization attribute to the request header:
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
// Prepare configuration with custom headers if proxy auth is available
const modelConfig: any = {
apiKey: apiKey,
model: this.modelName,
maxOutputTokens: llmConfig.maxOutputTokens,
temperature: llmConfig.temperature,
topK: 1,
topP: 0.90,
safetySettings: safetySettings,
streaming: true,
baseUrl: llmConfig.baseUrl // Use configured baseURL for custom proxy support
};
// Add custom headers for proxy authentication if available
if (llmConfig.baseUrl && proxyAuthToken) {
modelConfig.requestOptions = {
customHeaders: {
'authorization': `Bearer ${proxyAuthToken}`
}
};
}
// Initialize the LangChain model
this.model = new ChatGoogleGenerativeAI(modelConfig);
But with @google/generativeai it is doable using requestOptions.customHeaders and works properly:
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
initialize(
apiKey: string,
modelName?: string,
enableLogging?: boolean,
baseUrl?: string,
proxyAuthToken?: string
): void {
if (!apiKey) {
throw new Error('Gemini API key is required');
}
this.loggingEnabled = enableLogging || false;
this.currentModel = modelName || 'gemini-2.0-flash';
this.baseUrl = baseUrl;
this.genAI = new GoogleGenerativeAI(apiKey);
// Prepare request options with baseUrl and proxy auth if provided
const requestOptions: any = {};
if (this.baseUrl) {
requestOptions.baseUrl = this.baseUrl;
}
if (proxyAuthToken) {
requestOptions.customHeaders = {
'authorization': `Bearer ${proxyAuthToken}`
};
}
this.model = this.genAI.getGenerativeModel({
model: this.currentModel,
generationConfig: {
responseMimeType: "application/json",
responseSchema: this.getWorkflowSchema()
}
}, Object.keys(requestOptions).length > 0 ? requestOptions : undefined);
logger.info('GeminiAnalyzerService initialized');
}