Skip to content

fix(gemini): forward requestOptions.headers to GoogleGenAI httpOptions#12009

Open
serhiizghama wants to merge 1 commit intocontinuedev:mainfrom
serhiizghama:fix/issue-12008-gemini-custom-headers
Open

fix(gemini): forward requestOptions.headers to GoogleGenAI httpOptions#12009
serhiizghama wants to merge 1 commit intocontinuedev:mainfrom
serhiizghama:fix/issue-12008-gemini-custom-headers

Conversation

@serhiizghama
Copy link
Copy Markdown

@serhiizghama serhiizghama commented Apr 2, 2026

Problem

The GeminiApi constructor initializes GoogleGenAI without forwarding requestOptions.headers to the SDK. Custom headers configured via requestOptions.headers in config.yaml (e.g. x-api-key for enterprise Gemini endpoints) are silently dropped for all chat/streaming requests, causing authentication errors like:

{"error": {"code": 400, "message": "API key not valid.", "status": "INVALID_ARGUMENT"}}

Root cause

In packages/openai-adapters/src/apis/Gemini.ts, the GoogleGenAI instance is created without httpOptions:

this.genAI = withNativeFetch(() =>
  new GoogleGenAI({
    apiKey: this.config.apiKey,
    // requestOptions.headers not forwarded here
  }),
);

Custom headers work in embed() because it uses customFetch(this.config.requestOptions), which does merge requestOptions.headers. But chat goes through this.genAI, which never sees them.

What changed

Pass requestOptions.headers to GoogleGenAI via the httpOptions.headers field, which the @google/genai SDK fully supports (HttpOptions.headers: Record<string, string>):

const customHeaders = this.config.requestOptions?.headers;
this.genAI = withNativeFetch(() =>
  new GoogleGenAI({
    apiKey: this.config.apiKey,
    ...(customHeaders && { httpOptions: { headers: customHeaders } }),
  }),
);

How to test

Configure a Gemini model with a custom header that replaces the API key auth:

name: Gemini
models:
  - name: Gemini Flash
    provider: gemini
    model: gemini-2.5-flash
    apiBase: https://your-enterprise-endpoint/v1/
    requestOptions:
      headers:
        x-api-key: your-key

With this fix, the header is forwarded and chat requests succeed instead of returning 401/400.

Fixes #12008


Summary by cubic

Forward custom headers from requestOptions.headers to @google/genai via httpOptions.headers. This makes Gemini chat and streaming requests include enterprise auth (e.g., x-api-key) and fixes invalid API key errors.

  • Bug Fixes
    • Pass requestOptions.headers when constructing GoogleGenAI (inside withNativeFetch).

Written for commit 3cf3798. Summary will update on new commits.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@serhiizghama serhiizghama marked this pull request as ready for review April 3, 2026 13:19
@serhiizghama serhiizghama requested a review from a team as a code owner April 3, 2026 13:19
@serhiizghama serhiizghama requested review from sestinj and removed request for a team April 3, 2026 13:19
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 3, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Provider gemini does not pass custom headers to gemini API

1 participant