-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
Checked other resources
- This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
- I added a very descriptive title to this issue.
- I searched the LangChain.js documentation with the integrated search.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain.js rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
// ❌ Current: No way to customize fetch or headers
const model = new ChatGoogleGenerativeAI({
model: "gemini-2.0-flash",
apiKey: "my-api-key",
baseUrl: "https://my-proxy.example.com/api",
// These options are NOT supported currently:
// customFetch: myCustomFetch,
// customHeaders: { "Authorization": "Bearer my-token" },
});
// ✅ Expected: Should support customFetch and customHeaders
const modelWithCustomFetch = new ChatGoogleGenerativeAI({
model: "gemini-2.0-flash",
apiKey: "my-api-key",
baseUrl: "https://my-proxy.example.com/api",
customFetch: async (url, init) => {
// Custom logic here
return fetch(url, init);
},
customHeaders: new Headers({
"Authorization": "Bearer my-proxy-token",
}),
});
Error Message and Stack Trace (if applicable)
N/A - This is a feature request, not a bug.
Description
Problem:
The @google/generative-ai SDK (version 0.24.0+) supports customFetch and customHeaders options in RequestOptions, which allow users to:
- Route requests through custom proxies with different authentication schemes
- Add custom HTTP headers for enterprise/internal API gateways
- Intercept and modify requests for debugging or logging
However, ChatGoogleGenerativeAI in @langchain/google-genai does not expose these options. Currently, only baseUrl and apiVersion are passed to the SDK:
// libs/providers/langchain-google-genai/src/chat_models.ts, constructor
{
apiVersion: fields.apiVersion,
baseUrl: fields.baseUrl,
}Use Case:
Many enterprise users need to access Google Gemini models through internal proxy gateways that:
- Require
Authorization: Bearer <token>header instead ofx-goog-api-key - Need custom headers for authentication, tracing, or rate limiting
- Require request/response logging via custom fetch
Current Workaround:
Users must patch globalThis.fetch, which is intrusive and affects all HTTP requests in the application.
Proposed Solution:
Add customFetch and customHeaders to GoogleGenerativeAIChatInput interface and pass them to the SDK:
// In GoogleGenerativeAIChatInput interface
customFetch?: typeof fetch;
customHeaders?: HeadersInit;
// In constructor, when creating client
{
apiVersion: fields.apiVersion,
baseUrl: fields.baseUrl,
customFetch: fields.customFetch,
customHeaders: fields.customHeaders,
}This is a minimal, non-breaking change that unlocks significant flexibility for enterprise users.
System Info
Package: @langchain/google-genai
Version: 1.0.3
Underlying SDK: @google/generative-ai ^0.24.0
Platform: macOS / Linux / Windows (all affected)
Node version: 20+
Related source file:
https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-google-genai/src/chat_models.ts