Practical technical guides

Embedding Models Trained With Task Prefixes Need Them on Both Queries and Documents

A small measured comparison with nomic-embed-text, and why a half-migrated index ranks by migration state rather than relevance.

By Sandeep Belgavi · Published 2026-12-05

A prefix is part of the index contract

Some embedding models are trained to embed queries and documents differently and expect the caller to say which is which. The model card for nomic-embed-text states that the text prompt must include a task instruction prefix, and gives search_document: for texts stored in a retrieval index and search_query: for user questions. Other model families use their own strings, such as query: and passage: for E5. The vector you get back depends on that prefix, so the prefix is as much a part of the index as the model name and the dimension.

Teams discover this in two ways. Some never add the prefixes, because the model returns reasonable vectors without them and a quick demo works. Others add them during a migration and re-embed documents in batches over days, so for a while the index holds both kinds of vectors. The second case is more dangerous than the first, and the measurement below shows why.

A small measurement, reported as small

I embedded a synthetic help-desk corpus for Belgavi's Book Store with nomic-embed-text on a local Ollama 0.40.18 server. The corpus has 12 policy passages: returns, damaged items, gift cards, preorders, signed editions, refunds, shipping speed, address changes, ebook downloads, bulk orders, lost parcels and store credit. Twelve questions are phrased the way customers write them, such as "my novel came with a ripped cover what do I do", each with one correct passage. A second run adds 12 distractor passages on neighbouring topics, such as shipping fees and paperback shelf wear, to make ranking harder. Scores are cosine similarities; hit at 1 counts questions whose correct passage ranked first.

Configuration12 passages: hit at 112 passages: MRR24 passages: hit at 124 passages: MRR
Prefixes on queries and documents11 of 120.95810 of 120.903
No prefixes anywhere11 of 120.95810 of 120.917
Prefix on documents only11 of 120.95810 of 120.903
Prefix on queries only11 of 120.93810 of 120.892

On this corpus the prefixes did not improve retrieval. That is a real result and it should not be stretched in either direction: twelve questions cannot show that prefixes are unnecessary for this model, and they cannot show that they help. The model card's instruction comes from how the model was trained; a small, easy synthetic set is not a test of that. The useful findings are elsewhere.

Mixed indexes rank by migration state

The more revealing run simulates a half-finished migration in the 24-passage index. Queries carry the search_query: prefix. In one variant the 12 correct passages are still unprefixed and the 12 distractors have been re-embedded with search_document:; in the other variant it is the reverse.

Half-migrated indexHit at 1MRRQuestions whose top result was an unprefixed passage
Correct passages unprefixed, distractors prefixed11 of 120.93812
Correct passages prefixed, distractors unprefixed9 of 120.8473

The same questions, the same passages and the same model produce either the best or the worst score in the experiment, depending only on which half of the index was re-embedded first. To see why, I embedded all 24 passages both ways and compared each prefixed query's score against the prefixed and unprefixed version of the same passage. Across 288 query and passage pairs, the prefix changed the score by between minus 0.102 and plus 0.056, with a mean of minus 0.025. In the 12-passage run, the mean gap between the correct passage and the best wrong one was about 0.10. A shift as large as the margin between right and wrong answers is enough to reorder results, and in a mixed index it is applied to some documents and not others.

None of this is a benchmark of the model. It is one machine, one runtime and a tiny synthetic corpus. It is enough to show that mixing prefix conventions in one index produces rankings that reflect the migration, not relevance.

Record the embedding recipe with every vector

Treat the full input recipe as metadata stored beside each vector:

EMBED_RECIPE = {
    "model": "nomic-embed-text",
    "model_digest": "0a109f422b47",
    "document_prefix": "search_document: ",
    "query_prefix": "search_query: ",
    "max_input_tokens": 2048,
    "normalised": False,
}

def embed_document(text, embed):
    vector = embed(EMBED_RECIPE["document_prefix"] + text)
    return {"vector": vector, "recipe": EMBED_RECIPE}

def embed_query(text, embed, index_recipe):
    if index_recipe != EMBED_RECIPE:
        raise RuntimeError("query recipe does not match the index recipe")
    return embed(EMBED_RECIPE["query_prefix"] + text)

A query path that refuses to run against an index built with a different recipe turns a silent ranking problem into a deploy-time error. The model digest matters too: two downloads with the same name can be different weights.

Migrating an existing index safely

If an index was built without prefixes and the model card asks for them, do not re-embed in place:

  1. Build a second index with the new recipe, embedding every document, while the old index keeps serving.
  2. Run the evaluation questions against both and compare hit rates and the overlap of the top results. Expect small differences in either direction on small sets, as above.
  3. Switch reads to the new index in one step, with the query recipe switched in the same deploy.
  4. Keep the old index until the new one has served real traffic without regressions, then delete it.

The same procedure applies when changing the prefix string, the model, the model version or normalisation. Any change that alters how a document becomes a vector creates a new index.

Where prefixes come from

Prefix strings are not standardised. Take them from the model card of the exact model, not from a blog post or another model's documentation. Some runtimes add them for you: a library may expose a prompt_name or task_type setting that inserts the string, and a hosted API may take a task type parameter instead of text. Check which layer adds it, because adding it twice, once in your code and once in the library, is a third recipe and a third kind of vector.

Prefixes also count against the input limit. A long prefix plus a chunk sized to the limit pushes the end of the chunk past what the model embeds; see the companion guide on embedding input truncation.

Failure modes

Mixed recipes in one index. Rankings depend on which documents were re-embedded, as measured above.

One side prefixed, the other not. Prefixed queries against unprefixed documents gave the lowest MRR in both runs here; the drop was small, but there is no reason to accept an unmeasured mismatch.

Library and application both adding the prefix. The vector becomes something neither the model card nor the evaluation described.

Recipe drift across services. An ingestion service and a search service maintained by different teams diverge. Store the recipe in one place and have both read it.

Normalisation assumed. Cosine similarity hides vector length differences; a dot-product index does not. Record whether vectors were normalised.

When this does not matter

If the model does not use task prefixes, there is nothing to add; adding text the model was not trained with is another unmeasured change. If the index is rebuilt from scratch on every deploy, a mixed state cannot persist. And if retrieval is lexical or hybrid with a strong lexical component, prefix effects on the dense side matter less, though they still change fused rankings.

Verification

  • A recipe check at query time that compares the query recipe with the index recipe.
  • An evaluation set run against every index build, reporting hit at 1 and top-k overlap with the previous build.
  • A canary that embeds one known passage with and without the prefix and fails if the stored vector matches the wrong one.
  • A count of vectors per recipe in each index, which must be one recipe.

The companion example contains the synthetic corpus, the questions and the evaluation script, with the recorded scores from this run and a mode that reruns them against a local Ollama server.

Primary references and authorship

Sources read December 5, 2026. Original explanations, measurements and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/embedding_task_prefixes. Views are my own and do not represent any employer, past or present.