Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google-vertexai model authentication error - throw new Error("APIs that require a project ID cannot use an API key") #5000

Closed
5 tasks done
mohan-ganesh opened this issue Apr 7, 2024 · 8 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:question A specific question about the codebase, product, project, or how to use a feature

Comments

@mohan-ganesh
Copy link

Checked other resources

  • 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

I'm using the following version of the libraries on node version V21.7.2
just trying to initialize the VertexAI and invoke the model, and getting the following error message.

`import { VertexAI } from "@langchain/google-vertexai";
const model = new VertexAI({
authOptions: {
credentials: {
type: "service_account",
project_id: "",
private_key_id: "",
private_key: "",
client_email: "",
client_id: "",
auth_uri: "",
token_uri: "",
auth_provider_x509_cert_url: "",
client_x509_cert_url: "",
universe_domain: "",
},
},
});

const res = await model.invoke(
"What does google gemini do?"
);
console.log({ res });
`

Up that i get the following error node_modules/@langchain/google-common/dist/auth.js:51
throw new Error("APIs that require a project ID cannot use an API key");

also, when i traced to implementation it seems like getProjectId() is implemented to throw this error

Question: I tried all the 4 ways to initilize the model to provide the auth details listed at https://js.langchain.com/docs/integrations/llms/google_vertex_ai but all of them resolved the same error

Please advice

export class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
constructor(apiKey) {
super();
Object.defineProperty(this, "apiKey", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.apiKey = apiKey;
}
get clientType() {
return "apiKey";
}
getProjectId() {
throw new Error("APIs that require a project ID cannot use an API key");
// Perhaps we could implement this if needed:
// https://cloud.google.com/docs/authentication/api-keys#get-info
}
request(opts) {
const authHeader = {
"X-Goog-Api-Key": this.apiKey,
};
return this._request(opts.url, opts, authHeader);
}
}

Error Message and Stack Trace (if applicable)

Error: APIs that require a project ID cannot use an API key
at ApiKeyGoogleAuth.getProjectId (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/auth.js:51:15)
at ChatConnection.buildUrlVertex (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/connection.js:166:45)
at ChatConnection.buildUrl (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/connection.js:176:29)
at ChatConnection._request (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/connection.js:48:32)
at ChatConnection.request (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/connection.js:181:37)
at ProxyChatGoogle._streamResponseChunks (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/chat_models.js:144:56)
at _streamResponseChunks.next ()
at ProxyChatGoogle._streamIterator (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/core/dist/language_models/chat_models.js:78:34)
at async VertexAI._streamIterator (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/llms.js:180:30)
at async VertexAI.invoke (file:///Users/mohanganesh/projects/ai/mono-ai-vertex/ping-vertex/node_modules/@langchain/google-common/dist/llms.js:220:26)

Description

  • i am trying to initilize the VertexAI model from langchain and leading the above error

System Info

I have tried on mac, also in a container and results were same.

Copy link

dosubot bot commented Apr 7, 2024

The error message you're encountering suggests that the authentication method being used is not correctly providing a project ID in a manner expected by Google Cloud services, specifically Vertex AI. The error message implies that an API key is being used or the system believes one is being used, which is not supported for the APIs requiring a project ID.

Given the context of the GoogleVertexAI class initialization in the provided TypeScript code, the authentication is handled through the GAuthClient with the use of Google Cloud's authentication mechanisms rather than an API key directly. The class expects authentication to be provided via Google Cloud's standard authentication methods, which include:

  1. Logging into an account with permissions to the Google Cloud project using Vertex AI.
  2. Running on a machine using a service account with permissions to the Google Cloud project.
  3. Setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of a service account credentials file.

To resolve the authentication error, ensure the following:

  1. Service Account Permissions: Verify that the service account being used has the necessary permissions for accessing Vertex AI services. It should have roles that grant access to Vertex AI APIs.

  2. Environment Variable: Ensure that the GOOGLE_APPLICATION_CREDENTIALS environment variable is correctly set to the path of a valid service account JSON credentials file. This file should contain the necessary information, including the project ID.

  3. Correct Use of Authentication Methods: Double-check that you are not inadvertently using an API key for authentication. The code and the error suggest a mismatch in the expected authentication method. Ensure that the authentication flow is correctly set up to use one of the supported methods mentioned in the code comments.

  4. Google Cloud SDK Authentication: If you are running this locally or in an environment where the Google Cloud SDK is available, make sure you are authenticated with gcloud auth login and have set the project with gcloud config set project [YOUR_PROJECT_ID], especially if you rely on the first method of authentication mentioned in the comments.

  5. Check for Overrides: Ensure that there are no parts of the code or configuration that override the intended authentication method with an API key. This includes checking environment variables, configuration files, or other parts of the code that might be setting or expecting an API key.

If after these steps the issue persists, it might be helpful to explicitly log or inspect the authentication configuration at runtime to ensure that the expected values are being used and that the GAuthClient is correctly configured with the necessary scopes and credentials.

Regarding the authOptions object passed to the VertexAI constructor in the LangChain JS framework, it is expected to be of the type GoogleAuthOptions. This is inferred from the constructor of the GAuthClient class, which accepts an optional parameter options of type GoogleAuthOptions and passes it to the GoogleAuth constructor. The GoogleAuthOptions type is imported from the google-auth-library, and while the specific content and format of GoogleAuthOptions are not detailed in the provided context, it typically includes configuration options for Google authentication, such as credentials, client options, and others that are used to initialize and configure the authentication process for Google APIs.

I hope this helps! If you have any further questions or if the issue persists, please provide more details about your setup and the steps you've taken so far.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:question A specific question about the codebase, product, project, or how to use a feature labels Apr 7, 2024
@sessycode
Copy link

How about comment out GOOGLE_API_KEY in your env file?
In my case, I found it makes trouble when GOOGLE_APPLICATION_CREDENTIALS and GOOGLE_API_KEY are togehter in the env file.

@afirstenberg
Copy link
Contributor

What @sessycode said - Are you using or setting GOOGLE_API_KEY anywhere? That shouldn't be compatible with Vertex AI.

(That said, it might make sense for the library to ignore API key completely if we're in Vertex AI mode. But I thought it did.)

@glorat
Copy link
Contributor

glorat commented May 31, 2024

Note of thanks to this thread... I had the same error because I had GOOGLE_API_KEY in my .env file for unrelated purposes

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Aug 30, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 6, 2024
@regevaz
Copy link

regevaz commented Dec 26, 2024

definitely it makes a huge sense for the library to ignore API key completely if we're in Vertex AI mode...

@afirstenberg
Copy link
Contributor

Coincidentally, I have a fix for this in progress.

@regevaz
Copy link

regevaz commented Dec 26, 2024

Great! I hope it'll merged soon

@afirstenberg
Copy link
Contributor

@regevaz - PR #7435 includes this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature auto:question A specific question about the codebase, product, project, or how to use a feature
Projects
None yet
Development

No branches or pull requests

5 participants