LangChain4j integration for Google Vertex AI models including chat, language, embedding, image, and scoring capabilities
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
All Vertex AI models support multiple authentication methods via Google Cloud credentials.
Models automatically discover credentials from environment using Default Application Credentials:
VertexAiChatModel model = VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project("your-project-id")
.location("us-central1")
.publisher("google")
.modelName("chat-bison@001")
.build(); // No credentials specified - uses auto-discoveryDefault Application Credentials checks in this order:
gcloud auth application-default loginSet the path to your service account key file:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"Then build models normally (no credentials parameter needed):
VertexAiChatModel model = VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project("your-project-id")
.location("us-central1")
.publisher("google")
.modelName("chat-bison@001")
.build();For local development, authenticate using gcloud CLI:
gcloud auth application-default loginThis stores credentials locally that models will automatically discover.
For custom credential management, provide GoogleCredentials explicitly:
import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
GoogleCredentials credentials = GoogleCredentials.fromStream(
new FileInputStream("/path/to/service-account.json")
);
VertexAiChatModel model = VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project("your-project-id")
.location("us-central1")
.publisher("google")
.modelName("chat-bison@001")
.credentials(credentials) // Explicit credentials
.build();GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream);GoogleCredentials credentials = GoogleCredentials.fromStream(
new FileInputStream("/path/to/service-account.json")
).createScoped("https://www.googleapis.com/auth/cloud-platform");import java.io.ByteArrayInputStream;
String jsonKey = "{ ... service account JSON ... }";
GoogleCredentials credentials = GoogleCredentials.fromStream(
new ByteArrayInputStream(jsonKey.getBytes())
);Custom credentials via credentials() builder method are supported by:
For models without credentials() support, use environment variable or gcloud SDK methods.
Your service account needs these IAM roles:
roles/aiplatform.userOr these specific permissions:
aiplatform.endpoints.predict
aiplatform.models.predictError: "Application Default Credentials are not available"
Solutions:
GOOGLE_APPLICATION_CREDENTIALS environment variablegcloud auth application-default loginError: "Permission denied" or "403 Forbidden"
Solutions:
roles/aiplatform.user roleError: "Could not load credentials" or "Invalid key format"
Solutions:
gcloud auth application-default login for convenienceInstall with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-vertex-ai@1.11.0