docs/docs/integrations/embedding-models/google-genai.md
https://github.com/googleapis/java-genai
This integration uses the official Google Gen AI SDK for Java (com.google.genai:google-genai). It is marked
Experimental: the API and implementation may change in future releases.
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-google-genai</artifactId>
<version>1.19.0-beta29</version>
</dependency>
Get an API key for free here: https://ai.google.dev/gemini-api/docs/api-key .
See the available embedding models, for example:
gemini-embedding-001 — text-only; supports task types and output dimensionality (128–3072).gemini-embedding-2 — natively multimodal; does not use the task type parameter (see
Google AI Gemini Embeddings for how task instructions work
with Gemini Embedding 2).EmbeddingModel embeddingModel = GoogleGenAiEmbeddingModel.builder()
.apiKey(System.getenv("GOOGLE_AI_GEMINI_API_KEY"))
.modelName("gemini-embedding-001")
.build();
Response<Embedding> response = embeddingModel.embed("Hello, world!");
Embedding embedding = response.content();
EmbeddingModel embeddingModel = GoogleGenAiEmbeddingModel.builder()
.apiKey(System.getenv("GOOGLE_AI_GEMINI_API_KEY"))
.modelName("gemini-embedding-001")
.taskType(GoogleGenAiEmbeddingModel.TaskTypeEnum.RETRIEVAL_DOCUMENT) // default task type
.outputDimensionality(768) // reduce the embedding size (for models that support it)
.titleMetadataKey("title") // metadata key used as the document title for RETRIEVAL_DOCUMENT
.maxRetries(3)
.timeout(Duration.ofSeconds(30))
.build();
Besides the convenience methods and the builder-level taskType(...), GoogleGenAiEmbeddingModel supports the
request/response API with per-call parameters:
EmbeddingInputType.QUERY / DOCUMENT is mapped to the SDK's RETRIEVAL_QUERY /
RETRIEVAL_DOCUMENT task type, so you can embed queries and documents differently without configuring two
model instances. (This applies to models that support task types, such as gemini-embedding-001.)dimensions(...) overrides the builder's outputDimensionality, for models that
support reducing the output size.gemini-embedding-2): natively embeds interleaved text + image into a single embedding.
Earlier models (e.g. gemini-embedding-001) are text-only. Images must be provided as base64 (ImageContent).GoogleGenAiEmbeddingModel.builder().listeners(...) to observe requests,
responses, and errors.EmbeddingResponse response = embeddingModel.embed(EmbeddingRequest.builder()
.input("What is the capital of France?")
.inputType(EmbeddingInputType.QUERY) // embed as a query
.dimensions(256) // reduce output dimensionality
.build());
List<Embedding> embeddings = response.embeddings();
Multimodal example (Gemini Embedding 2 — text and image fused into one embedding):
EmbeddingModel embeddingModel = GoogleGenAiEmbeddingModel.builder()
.apiKey(System.getenv("GOOGLE_AI_GEMINI_API_KEY"))
.modelName("gemini-embedding-2")
.build();
EmbeddingResponse response = embeddingModel.embed(EmbeddingRequest.builder()
.input(TextContent.from("a photo of a cat"), ImageContent.from(base64Image, "image/png"))
.build());
Embedding embedding = response.embeddings().get(0);
See Embedding Model for the request/response API, and Observability for listeners.
For more details on the Gemini embedding models, see the documentation.