Skip to content

fix(auth): improve Vertex AI 401 error message when using standard AP… - #28679

Open
SHAI-nikhil-chaudhary wants to merge 5 commits into
google-gemini:mainfrom
SHAI-nikhil-chaudhary:fix/vertex-ai-401-error
Open

fix(auth): improve Vertex AI 401 error message when using standard AP…#28679
SHAI-nikhil-chaudhary wants to merge 5 commits into
google-gemini:mainfrom
SHAI-nikhil-chaudhary:fix/vertex-ai-401-error

Conversation

@SHAI-nikhil-chaudhary

Copy link
Copy Markdown

This PR improves the error handling and developer experience when configuring authentication for Vertex AI.

Previously, if a user attempted to use the vertex-ai auth type but only provided a standard Gemini API key (without Google Cloud credentials), the CLI would attempt the request and fail with a cryptic 401 Unauthorized error from the Vertex endpoint.

This update adds proactive validation in createContentGeneratorConfig. It now checks if the user is in Vertex AI mode with only a standard key and throws a descriptive, actionable error message before the network request is ever made, guiding them to use Google Cloud credentials or switch their auth type.

Related Issue
Fixes #28622

What changed
Added validation logic in packages/core/src/core/contentGenerator.ts to catch the mismatched credential state.
Added a specific error message guiding the user to use gcloud auth application-default login or standard auth.
Added a corresponding unit test in packages/core/src/core/contentGenerator.test.ts to ensure this exact configuration rejects with the expected error.
Checklist
[x] I have read the contributing guidelines.

[x] I have run npm run build successfully.

[x] I have run npm test and all tests pass locally.

[x] I have added new unit tests to cover my changes.

@SHAI-nikhil-chaudhary
SHAI-nikhil-chaudhary requested a review from a team as a code owner August 4, 2026 07:16
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the developer experience for Vertex AI authentication by replacing cryptic 401 errors with clear, actionable feedback. By validating credentials before initiating network requests, the system now prevents misconfiguration issues and guides users toward the correct authentication methods.

Highlights

  • Proactive Authentication Validation: Introduced a validation check in createContentGeneratorConfig to detect when a user attempts to use Vertex AI with an incompatible standard Gemini API key.
  • Improved Error Messaging: Implemented a descriptive error message that provides actionable guidance on how to authenticate correctly for Vertex AI.
  • Test Coverage: Added a new unit test to verify that the configuration correctly rejects invalid authentication setups.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/s A small PR label Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 25
  • Additions: +18
  • Deletions: -7
  • Files changed: 1

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a proactive validation check to throw an error when attempting to use Vertex AI with only a standard Gemini API key, along with a corresponding unit test. The reviewer points out that this check introduces false positives for users relying on ambient Google Cloud credentials (ADC) who also have a GEMINI_API_KEY set. The reviewer suggests removing this validation and instead temporarily stripping GEMINI_API_KEY from process.env during client initialization to prevent the SDK from falling back to it, which would also require removing the newly added test.

Comment on lines +190 to 204
if (authType === AuthType.USE_VERTEX_AI) {
if (geminiApiKey && !googleApiKey && !(googleCloudProject && googleCloudLocation)) {
throw new Error(
'Authentication failed: You are attempting to use the Vertex AI endpoint with a standard Gemini API key. ' +
'Vertex AI requires Google Cloud credentials. Please authenticate using standard Google Cloud methods ' +
'(e.g., `gcloud auth application-default login`) or switch your auth type to standard.'
);
}
if (googleApiKey || (googleCloudProject && googleCloudLocation)) {
contentGeneratorConfig.apiKey = googleApiKey;
contentGeneratorConfig.vertexai = true;

return contentGeneratorConfig;
return contentGeneratorConfig;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This proactive validation check introduces a false-positive error for users who want to authenticate with Vertex AI using ambient Google Cloud credentials (such as those configured via gcloud auth application-default login or available ambiently on GCP environments like Cloud Run, GKE, or GCE) but also happen to have a standard GEMINI_API_KEY set in their environment or keychain.

In these valid scenarios, googleCloudProject and googleCloudLocation environment variables might not be explicitly set, yet authentication would succeed via Application Default Credentials (ADC). This check will incorrectly block these users with a misleading error message.

To fix the underlying issue (where the SDK mistakenly falls back to GEMINI_API_KEY in Vertex AI mode and causes a cryptic 401 error), we should instead prevent the SDK from picking up GEMINI_API_KEY when using Vertex AI with ADC. This can be achieved by temporarily removing GEMINI_API_KEY from process.env during the GoogleGenAI client initialization in createContentGenerator (around line 391), rather than throwing a proactive error here.

If ADC is indeed missing, the Google Auth library will naturally throw a clear, standard, and actionable error: Could not load the default credentials....

  if (authType === AuthType.USE_VERTEX_AI) {
    if (googleApiKey || (googleCloudProject && googleCloudLocation)) {
      contentGeneratorConfig.apiKey = googleApiKey;
      contentGeneratorConfig.vertexai = true;

      return contentGeneratorConfig;
    }
  }

Comment thread packages/core/src/core/contentGenerator.test.ts Outdated
@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/security Issues related to security labels Aug 4, 2026
@SHAI-nikhil-chaudhary

SHAI-nikhil-chaudhary commented Aug 5, 2026

Copy link
Copy Markdown
Author

Hi @joneba-google @DavidAPierce , just wanted to follow up on this PR and check if you’ve had a chance to review it. I’d be happy to make any changes or address any feedback needed.
Thanks for your time!

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

Labels

area/security Issues related to security priority/p2 Important but can be addressed in a future release. size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini CLI returns 401 when using a Gemini API key with the Vertex AI endpoint

1 participant