# How to append a new key-value to the header produced by ChatGoogleGenerativeAI in Langchain?

**URL:** <https://forum.langchain.com/t/how-to-append-a-new-key-value-to-the-header-produced-by-chatgooglegenerativeai-in-langchain/499>\
**Category:** LangSmith Product Help\
**Tags:** js-help\
**Created:** [July 16, 2025, 2:26pm UTC](https://forum.langchain.com/t/how-to-append-a-new-key-value-to-the-header-produced-by-chatgooglegenerativeai-in-langchain/499 "2025-07-16T14:26:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![agn-7](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/agn-7/32/222_2.png) [@agn-7](https://forum.langchain.com/u/agn-7)\
**Post date:** [July 16, 2025, 2:26pm UTC](https://forum.langchain.com/t/how-to-append-a-new-key-value-to-the-header-produced-by-chatgooglegenerativeai-in-langchain/499/1 "2025-07-16T14:26:23Z")

</div>

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:

```js
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:

```js
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');
  }

```

---

<div class="post-metadata">

**Author:** ![Yuchaocheng](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/yuchaocheng/32/1906_2.png) [@Yuchaocheng](https://forum.langchain.com/u/Yuchaocheng)\
**Post date:** [December 14, 2025, 4:32am UTC](https://forum.langchain.com/t/how-to-append-a-new-key-value-to-the-header-produced-by-chatgooglegenerativeai-in-langchain/499/2 "2025-12-14T04:32:04Z")

</div>

Hi! I ran into the exact same issue and did a deep dive into the source code. Here’s what I found:

### Root Cause

You’re right that `@google/generative-ai` SDK supports `customHeaders` via `RequestOptions`. However, `ChatGoogleGenerativeAI` in `@langchain/google-genai` \*\*does not expose this option\*\*.

Looking at the source code ([chat\_models.ts, lines 727-730]( [langchainjs/libs/providers/langchain-google-genai/src/chat\_models.ts at main · langchain-ai/langchainjs · GitHub](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-google-genai/src/chat_models.ts#L727-L730) )):

```typescript

this.client = new GenerativeAI(this.apiKey).getGenerativeModel(

{ /\* modelParams \*/ },

{

```
apiVersion: fields.apiVersion,

baseUrl: fields.baseUrl,

// ❌ customHeaders is NOT passed here

// ❌ customFetch is NOT passed here

```

}

);

```

Only `apiVersion` and `baseUrl` are forwarded to the SDK. The `requestOptions` parameter you’re trying to use is simply ignored.

### Workaround: Global Fetch Interception

Until LangChain adds native support, you can intercept `globalThis.fetch`:

```typescript

import { ChatGoogleGenerativeAI } from ‘@langchain/google-genai’;

const PROXY\_HOST = ‘[your-proxy-host.com](http://your-proxy-host.com)’; // Replace with your proxy hostname

const PROXY\_AUTH\_TOKEN = ‘your-token’;

// Intercept fetch before creating the model

const originalFetch = globalThis.fetch;

if (!(globalThis.fetch as any).\_\_proxyPatched) {

globalThis.fetch = async (input, init) =\> {

```
const url = typeof input === 'string' 

  ? input 

  : input instanceof URL 

    ? input.href 

    : (input as Request).url;

// Only intercept requests to your proxy

if (url.includes(PROXY_HOST)) {

  const headers = new Headers(init?.headers);

  // Remove SDK's default auth header, add your Bearer token

  headers.delete('x-goog-api-key');

  headers.set('Authorization', \`Bearer ${PROXY_AUTH_TOKEN}\`);

  return originalFetch(input, { ...init, headers });

}

return originalFetch(input, init);

```

};

(globalThis.fetch as any).\_\_proxyPatched = true;

}

// Now create the model - requests will be intercepted

const model = new ChatGoogleGenerativeAI({

apiKey: ‘your-api-key’, // Still required by SDK, but will be replaced

model: ‘gemini-2.0-flash’,

baseUrl: ‘[https://your-proxy-host.com/api/path](https://your-proxy-host.com/api/path)’,

// … other options

});

```

### Feature Request

This is a missing feature, not a bug. I’ve prepared an issue/PR request for LangChain to add `customFetch` and `customHeaders` support. The fix is straightforward - just pass these options through to the underlying SDK.

If you’d like to help push this forward, consider:

1. 👍 Upvoting any existing feature request

2. Opening a new feature request if none exists

3. Submitting a PR - the change is minimal (just add 2 fields to the interface and pass them to the SDK)

### Alternative: Use the SDK Directly

If you don’t need LangChain’s Agent/Chain features, using `@google/generative-ai` directly (as you’ve already discovered) is the cleanest solution for now.

Hope this helps! 🙌
