EmbeddingModel

public interface EmbeddingModel


Provides an interface for generating embeddings for text and multimodal Content.

Obtain instances via EmbeddingGeneration.getEmbeddingModels, and call close when the model is no longer needed to release underlying resources.

Typical usage:

val models = EmbeddingGeneration.getEmbeddingModels()
val model = models.firstOrNull() ?: return
models
.drop(1).forEach { it.close() }
try {
if (model.checkStatus() == FeatureStatus.DOWNLOADABLE) {
model.download().collect()
}
val request = EmbeddingRequest(
Content(listOf(TextPart("What is machine learning?"))),
TaskConfig.RetrievalQuery(),
)
val result = model.generateEmbedding(request)
val vector = result.embedding.vector
} finally {
model.close()
}

Summary

Public methods

abstract @FeatureStatus int

Checks the current availability status of the embedding feature.

abstract void

Releases resources associated with the underlying embedding engine.

abstract @NonNull Flow<@NonNull DownloadStatus>

Downloads the required model assets for the embedding feature if they are not already available.

abstract @NonNull EmbeddingResult

Performs asynchronous content embedding on the provided input request.

abstract EmbeddingMetadata

Returns the EmbeddingMetadata of the embedding model, or null if metadata retrieval fails or is unavailable.

abstract @NonNull String

Returns the model signature uniquely identifying the underlying embedding model and version.

abstract int

Returns the maximum number of input tokens supported by this model in a single request.

abstract boolean

Returns true if the model is currently deprecated.

abstract void

Warms up the inference engine for use by loading necessary models and initializing runtime components, reducing latency for the first generateEmbedding call.

Public methods

checkStatus

abstract @FeatureStatus int checkStatus()

Checks the current availability status of the embedding feature.

Returns
@FeatureStatus int

a FeatureStatus constant indicating the feature's readiness.

close

abstract void close()

Releases resources associated with the underlying embedding engine.

Callers should invoke this method when the model is no longer needed, including on any unused EmbeddingModel instances returned by EmbeddingGeneration.getEmbeddingModels.

download

abstract @NonNull Flow<@NonNull DownloadStatus> download()

Downloads the required model assets for the embedding feature if they are not already available.

The download starts when the returned cold Flow is collected. If the download fails, the flow emits DownloadStatus.DownloadFailed and terminates with the corresponding com.google.mlkit.genai.common.GenAiException.

Returns
@NonNull Flow<@NonNull DownloadStatus>

a cold Flow that emits DownloadStatus updates for download progress.

generateEmbedding

abstract @NonNull EmbeddingResult generateEmbedding(@NonNull EmbeddingRequest request)

Performs asynchronous content embedding on the provided input request.

Parameters
@NonNull EmbeddingRequest request

the EmbeddingRequest containing input Content and optional TaskConfig.

Returns
@NonNull EmbeddingResult

an EmbeddingResult containing the generated embedding vector and model signature.

Throws
com.google.mlkit.genai.common.GenAiException

if embedding generation fails or the request content is invalid.

getMetadata

abstract EmbeddingMetadata getMetadata()

Returns the EmbeddingMetadata of the embedding model, or null if metadata retrieval fails or is unavailable.

getModelSignature

abstract @NonNull String getModelSignature()

Returns the model signature uniquely identifying the underlying embedding model and version.

Embeddings generated by models with different signatures are incompatible and should not be directly compared. Callers that persist embeddings should store this signature alongside the vectors to detect model updates and re-index when the signature changes.

Returns
@NonNull String

the model signature string.

Throws
com.google.mlkit.genai.common.GenAiException

if retrieving the model signature fails.

getTokenLimit

abstract int getTokenLimit()

Returns the maximum number of input tokens supported by this model in a single request.

Returns
int

the maximum input token capacity, -1 if the token limit is unknown, or Int.MAX_VALUE if there is no token limit restriction.

Throws
com.google.mlkit.genai.common.GenAiException

if retrieving the token limit fails.

isDeprecated

abstract boolean isDeprecated()

Returns true if the model is currently deprecated.

A model is considered deprecated if the current UTC date is on or after EmbeddingMetadata.deprecationDate. Returns false if model metadata or the deprecation date is unavailable.

warmup

abstract void warmup()

Warms up the inference engine for use by loading necessary models and initializing runtime components, reducing latency for the first generateEmbedding call.

Throws
com.google.mlkit.genai.common.GenAiException

if initializing the inference engine fails.