New Tool Radar

AI Concepts

How to Get Embeddings from Ollama with /api/embed

Send a POST request to /api/embed with an embedding model and your text to get back one vector per text, ready to compare with cosine similarity.

Schematic diagram with three steps: pull the embeddinggemma model, send your texts to POST /api/embed, then read one vector per input from the embeddings field.

Search by meaning and retrieval-augmented generation both start with embeddings: text turned into numeric vectors that you can store in a vector database, search with cosine similarity, or use in RAG pipelines. Ollama can make them on the same server that runs your chat model, through the POST /api/embed endpoint. The request needs a model and an input, and the input can be a single string or an array of strings; with an array, the response holds one vector per string in the embeddings field, in the same order, so a batch of passages needs only one request. This is the API route to embeddings — the command-line route, running an embedding model with ollama run, is a different task.

The request and the response

Ollama’s API reference for /api/embed lists the request fields and what the response holds.

Field Required What the API reference says
model Yes The model name; use an embedding model such as embeddinggemma
input Yes Text or an array of texts to generate embeddings for
truncate No, default true If true, truncate inputs that exceed the context window; if false, returns an error
dimensions No Number of dimensions to generate embeddings for
keep_alive No How long the model stays loaded
options No Runtime options that control text generation, such as num_ctx

The response holds model, the model that produced the embeddings; embeddings, an array of vector embeddings, one per input; and total_duration, load_duration, and prompt_eval_count, the number of input tokens processed. The documentation says /api/embed returns L2-normalized, unit-length vectors, and recommends cosine similarity for most semantic search uses; for unit-length vectors the cosine similarity and the dot product are the same number.

Try it: embed two practice sentences

Pull the model the documentation uses, then send two short fictional sentences in one request:

ollama pull embeddinggemma

curl http://localhost:11434/api/embed -d '{ "model": "embeddinggemma", "input": ["The lighthouse keeper checked the lamp at noon.", "The supply boat arrives on Tuesdays."] }'

Check What you should see What it means
One vector per input The embeddings array holds two vectors, in the same order as the inputs Each sentence got its own vector
Vector shape Each vector is a list of numbers The length depends on the model; the documentation gives typically 384 to 1024 dimensions
Token count A prompt_eval_count number How many input tokens were processed

Expected result. We checked Ollama’s API reference for /api/embed, which lists the request and response fields above. We did not send a request to /api/embed on this machine, so we have no vectors of our own; the two sentences are fictional, and the two-vector response is the expected result.

Do not use the old endpoint

Ollama has three routes that return embeddings, and they do not behave the same. /api/embed is the current one: field input, answer embeddings in the plural, normalized vectors, string or list. The old /api/embeddings endpoint is marked superseded by /api/embed: it takes a prompt field, answers with a singular embedding, handles one text only, and returns the vector without normalizing — in one third-party test with nomic-embed-text its length was 21.63 instead of 1. Vectors built half with one endpoint and half with the other are not comparable by dot product, so use /api/embed for new work.

Limits

You need an embedding model: a chat model will not do it. The library table in the embeddings guide lists all-minilm at 46 MB with a 512-token context window, nomic-embed-text at 274 MB with 2K, and embeddinggemma at 622 MB with 2K. Use the same embedding model for indexing and for querying, because the vector length depends on the model; if you change the model or the dimensions setting, embed the stored documents again. Long input is cut off without an error by default, because truncate defaults to true, so a long document sent as one input can lose its end — split long documents into smaller chunks, or send truncate set to false so Ollama returns an error instead. The old-endpoint comparison in this guide comes from a third-party test on Ollama 0.40.17. We did not test any of this ourselves.

Sources

See all