The MediaPipe Text Embedder task lets you create a numeric representation of text data to capture its semantic meaning. This functionality is frequently used to compare the semantic similarity of two pieces of text using mathematical comparison techniques such as Cosine Similarity. This task operates on text data with a machine learning (ML) model, and outputs a numeric representation of the text data as a list of high-dimensional feature vectors, also known as embedding vectors, in either floating-point or quantized form.
Quickstart
Install the SDK for your platform, initialize the task with a model, and run inference.
Web
import { FilesetResolver, TextEmbedder } from "@mediapipe/tasks-text"; const MODEL_URL = "https://storage.googleapis.com/mediapipe-models/text_embedder/universal_sentence_encoder/float32/latest/universal_sentence_encoder.tflite"; const textFiles = await FilesetResolver.forTextTasks( "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-text@latest/wasm" ); const textEmbedder = await TextEmbedder.createFromOptions(textFiles, { baseOptions: { modelAssetPath: MODEL_URL }, quantize: true, }); const firstEmbed = textEmbedder.embed("It's a charming and affecting journey.").embeddings[0]; const secondEmbed = textEmbedder.embed("What a great and fantastic trip!").embeddings[0]; const similarity = TextEmbedder.cosineSimilarity(firstEmbed, secondEmbed);
Android (Kotlin)
import com.google.mediapipe.tasks.core.BaseOptions import com.google.mediapipe.tasks.text.textembedder.TextEmbedder import com.google.mediapipe.tasks.text.textembedder.TextEmbedder.TextEmbedderOptions val baseOptions = BaseOptions.builder() .setModelAssetPath("universal_sentence_encoder.tflite") .build() val options = TextEmbedderOptions.builder() .setBaseOptions(baseOptions) .setQuantize(true) .build() val textEmbedder = TextEmbedder.createFromOptions(context, options) val firstEmbed = textEmbedder .embed("It's a charming and affecting journey.") .embeddingResult().embeddings().first() val secondEmbed = textEmbedder .embed("What a great and fantastic trip!") .embeddingResult().embeddings().first() val similarity = TextEmbedder.cosineSimilarity(firstEmbed, secondEmbed)
Android (Java)
import com.google.mediapipe.tasks.components.containers.Embedding; import com.google.mediapipe.tasks.core.BaseOptions; import com.google.mediapipe.tasks.text.textembedder.TextEmbedder; import com.google.mediapipe.tasks.text.textembedder.TextEmbedder.TextEmbedderOptions; BaseOptions baseOptions = BaseOptions.builder() .setModelAssetPath("universal_sentence_encoder.tflite") .build(); TextEmbedderOptions options = TextEmbedderOptions.builder() .setBaseOptions(baseOptions) .setQuantize(true) .build(); TextEmbedder textEmbedder = TextEmbedder.createFromOptions(context, options); Embedding firstEmbed = textEmbedder .embed("It's a charming and affecting journey.") .embeddingResult().embeddings().get(0); Embedding secondEmbed = textEmbedder .embed("What a great and fantastic trip!") .embeddingResult().embeddings().get(0); double similarity = TextEmbedder.cosineSimilarity(firstEmbed, secondEmbed);
iOS
import Foundation import MediaPipeTasksText guard let modelPath = Bundle.main.path( forResource: "universal_sentence_encoder", ofType: "tflite" ) else { return } let options = TextEmbedderOptions() options.baseOptions.modelAssetPath = modelPath options.quantize = true let textEmbedder = try TextEmbedder(options: options) let firstResult = try textEmbedder.embed(text: "It's a charming and affecting journey.") let secondResult = try textEmbedder.embed(text: "What a great and fantastic trip!") if let firstEmbed = firstResult.embeddingResult.embeddings.first, let secondEmbed = secondResult.embeddingResult.embeddings.first { let similarity = try TextEmbedder.cosineSimilarity(embedding1: firstEmbed, embedding2: secondEmbed) }
Python
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions TextEmbedder = mp.tasks.text.TextEmbedder TextEmbedderOptions = mp.tasks.text.TextEmbedderOptions model_path = "universal_sentence_encoder.tflite" options = TextEmbedderOptions(base_options=BaseOptions(model_asset_path=model_path), quantize=True) with TextEmbedder.create_from_options(options) as text_embedder: first_embed = text_embedder.embed("It's a charming and affecting journey.").embeddings[0] second_embed = text_embedder.embed("What a great and fantastic trip!").embeddings[0] similarity = TextEmbedder.cosine_similarity(first_embed, second_embed)
Task details
This section describes the capabilities, inputs, outputs, and configuration options of this task.
Features
- Input text processing - Supports out-of-graph tokenization for models without in-graph tokenization.
- Embedding similarity computation - Built-in utility function to compute the cosine similarity between two feature vectors.
- Quantization - Supports scalar quantization for the feature vectors.
- Text (
String) - Optional formatting context (
TextFormatOptionson Web,TextFormatContexton Android and iOS, orTextEmbeddingFormatContextin Python) for supported models like the EmbeddingGemma 300M model.
- Embedding: the feature vector itself, either in floating-point form or scalar-quantized.
- Head index: the index for the head that produced this embedding.
- Head name (optional): the name of the head that produced this embedding.
Configuration options
This task has the following configuration options:
Web
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
l2Normalize |
Whether to normalize the returned feature vector with L2 norm. Use only if the model does not already include a built-in L2_NORMALIZATION op. |
Boolean |
false |
quantize |
Whether to scalar-quantize the returned embedding to bytes (assumes unit-norm vectors in [-1.0, 1.0]; pair with l2Normalize if needed). |
Boolean |
false |
Android
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
l2Normalize |
Whether to normalize the returned feature vector with L2 norm. Use only if the model does not already include a built-in L2_NORMALIZATION op. |
Boolean |
false |
quantize |
Whether to scalar-quantize the returned embedding to bytes (assumes unit-norm vectors in [-1.0, 1.0]; pair with l2Normalize if needed). |
Boolean |
false |
iOS
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
l2Normalize |
Whether to normalize the returned feature vector with L2 norm. Use only if the model does not already include a built-in L2_NORMALIZATION op. |
Boolean |
false |
quantize |
Whether to scalar-quantize the returned embedding to bytes (assumes unit-norm vectors in [-1.0, 1.0]; pair with l2Normalize if needed). |
Boolean |
false |
Python
| Option Name | Description | Value Range | Default Value |
|---|---|---|---|
l2_normalize |
Whether to normalize the returned feature vector with L2 norm. Use only if the model does not already include a built-in L2_NORMALIZATION op. |
Boolean |
False |
quantize |
Whether to scalar-quantize the returned embedding to bytes (assumes unit-norm vectors in [-1.0, 1.0]; pair with l2_normalize if needed). |
Boolean |
False |
Task output
Each TextEmbedderResult returns an embeddings list, and cosineSimilarity (cosine_similarity) computes semantic similarity between two embeddings in [-1.0, 1.0]:
| Field | Type / Range | Description |
|---|---|---|
floatEmbedding |
Float32Array / float[] |
Dense float semantic vector (embedding in Python; L2-normalized when l2Normalize is enabled). |
quantizedEmbedding |
Uint8Array / byte[] |
Scalar-quantized 8-bit vector when quantize is enabled. |
headIndex |
int |
Zero-based index of the embedding head (head_index in Python). |
headName |
string |
Optional name of the embedding head (head_name in Python). |
Example TextEmbedderResult payload
The following example shows the dense float embedding vector returned for an input string:
{
embeddings: [
{
headIndex: 0,
floatEmbedding: [0.2345, 0.1234, -0.0891, 0.4102, 0.6789 /* ... */]
}
]
}
Models
The Text Embedder supports the smaller Universal Sentence Encoder model and the more capable EmbeddingGemma 300M model for advanced use cases.
EmbeddingGemma 300M model
The TextEmbedder SDK supports the embeddinggemma-300m. The provided task file uses a maximum sequence length of 512 tokens.
This model handles input differently based on the task type and context format
you provide. For the query formats, the template follows task: <task> | query:
<text>, and for document formats, it follows
title: <title> | text: <text>. The formats follow the official EmbeddingGemma
prompt
instruction.
| Task Type | Formatting |
|---|---|
RETRIEVAL_DOCUMENT |
Uses document format with the provided title. |
RETRIEVAL_QUERY |
Uses query format with <task> as "search result". |
SEMANTIC_SIMILARITY |
Uses query format with <task> as "sentence similarity". |
CLASSIFICATION |
Uses query format with <task> as "classification". |
CLUSTERING |
Uses query format with <task> as "clustering". |
QUESTION_ANSWERING /FACT_CHECKING /CODE_RETRIEVAL |
Depending on whether the role is a query or document, it uses the respective format. When acting as a query, the <task> string corresponds to "question answering", "fact checking", or "code retrieval" respectively. |
| Default | Any other task type falls back to the query format with <task> as "search result". |
| Model name | Input | Quantization type | Versions |
|---|---|---|---|
| EmbeddingGemma 300M | string | Mixed Precision (int4+int8) | Latest |
Universal Sentence Encoder model
This model uses a dual encoder architecture and was trained on various question-answer datasets.
Consider the following pairs of sentences:
- ("it's a charming and often affecting journey", "what a great and fantastic trip")
- ("I like my phone", "I hate my phone")
- ("This restaurant has a great gimmick", "We need to double-check the details of our plan")
The text embeddings in the first two pairs will have a higher cosine similarity than the embeddings in the third pair because the first two pairs of sentences share a common topic of "trip sentiment" and "phone opinion" respectively while the third pair of sentences don't share a common topic.
Note that although the two sentences in the second pair have opposing sentiments, they have a high similarity score because they share a common topic.
| Model name | Input | Quantization type | Versions |
|---|---|---|---|
| Universal Sentence Encoder | string, string, string | None (float32) | Latest |
Task benchmarks
Here are the task benchmarks for the whole pipeline based on the preceding pre-trained models. The latency result is the average latency on Samsung S26 Ultra CPU with 4 threads. For more detailed benchmarks, see the EmbeddingGemma model card.
| Model name | Device | CPU latency (ms) |
|---|---|---|
| Universal Sentence Encoder | Galaxy S26 Ultra (4 threads) | 10 |
| EmbeddingGemma 300M | Galaxy S26 Ultra (4 threads) | 200 |