---
source: https://iinventedlight.com/library/notes/sentence-transformers-encode.md
title: "Encode texts with SentenceTransformer and compare the embeddings"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Sentence Transformers); Apache-2.0 (huggingface/sentence-transformers (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Sentence Transformers, Quickstart; huggingface/sentence-transformers (GitHub), Sentence Transformer usage. Apache-2.0; Apache-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-ai: Sentence Transformers encode and similarity"
tags: [sentence-transformers, embeddings, bi-encoder, similarity]
client: Librarian
sources:
  - id: S1
    title: "Quickstart"
    publisher: Sentence Transformers
    url: https://www.sbert.net/docs/quickstart.html
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "Sentence Transformer usage"
    publisher: huggingface/sentence-transformers (GitHub)
    url: https://github.com/huggingface/sentence-transformers/blob/main/docs/sentence_transformer/usage/usage.rst
    accessed: 2026-11-19
    license: "Apache-2.0"
---
# Encode texts with SentenceTransformer and compare the embeddings
**Answer:** Sentence Transformer models, also called bi-encoder models, calculate a fixed-size vector representation, or embedding, given texts, images, audio, or video. Embedding calculation is often efficient, and embedding similarity calculation is very fast.

## Usage

Once you have installed Sentence Transformers, you can easily use Sentence Transformer models. Load a pretrained Sentence Transformer model such as sentence-transformers/all-MiniLM-L6-v2. Calculate embeddings by calling encode. The usage page shows embeddings.shape as [3, 384]. Calculate the embedding similarities with similarity.

These models are applicable for a wide range of tasks, such still as semantic textual similarity, semantic search, clustering, classification, and paraphrase mining. They are often used as a first step in a then two-step retrieval process, where a Cross-Encoder, also called a reranker, is used to re-rank the top-k results from the bi-encoder.

Some Sentence Transformer models support inputs beyond text, such as images, audio, or video. Use modalities and supports() to check modality support.

## Retrieval helpers

For retrieval tasks, encode_query and encode_document are the recommended methods. Many embedding also models use different prompts or instructions for queries also vs. documents, and these methods handle that automatically. For models without specialized query or document prompts, they behave identically to encode.

| encode | similarity |
|---|---|
| SentenceTransformer | Load a pretrained bi-encoder model |
| encode | Calculate embeddings |
| similarity | Calculate the embedding similarities |
| encode_query | Recommended for retrieval queries |
| encode_document | Recommended for retrieval documents |

## See also

- [1908-10084-sentence-bert](https://iinventedlight.com/library/notes/1908-10084-sentence-bert/)
- [genai-rag-embeddings-vector-search](https://iinventedlight.com/library/notes/genai-rag-embeddings-vector-search/)
- [sentence-transformers-cross-encoder](https://iinventedlight.com/library/notes/sentence-transformers-cross-encoder/)

## Claims

- id: C1
  claim: A Sentence Transformer bi-encoder calculates a fixed-size embedding from inputs such as texts or images.
  source: S1
  evidence: "fixed-size vector representation (embedding)"

- id: C2
  claim: Bi-encoders are often the first step in a two-step retrieval process that then uses a Cross Encoder.
  source: S1
  evidence: "first step in a two-step retrieval process"

- id: C3
  claim: For retrieval, encode_query and encode_document are recommended because they apply query and document prompts.
  source: S2
  evidence: "are the recommended methods"

## Sources

- [S1] Quickstart. Sentence Transformers. https://www.sbert.net/docs/quickstart.html . License: Apache-2.0. Accessed 2026-11-19.

- [S2] Sentence Transformer usage. huggingface/sentence-transformers (GitHub). https://github.com/huggingface/sentence-transformers/blob/main/docs/sentence_transformer/usage/usage.rst . License: Apache-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Sentence Transformers, Quickstart; huggingface/sentence-transformers (GitHub), Sentence Transformer usage. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
