GeminiEmbeddingRAGAPI

Give Gemini Embedding 2 queries and documents their own formats

For text retrieval, Gemini Embedding 2 uses task instructions in the input rather than task_type. Pair query prefixes with document formatting and rebuild incompatible older vectors.

Give Gemini Embedding 2 queries and documents their own formats

For text retrieval with Gemini Embedding 2, format the query for its task and the document for its role. The older task_type setting does not carry over. Getting vectors back from both requests is only the first check: the query and index must also follow a compatible retrieval policy.

This guide checks Google's current embedding documentation on November 26, 2026. It covers text-only retrieval with gemini-embedding-2. The multimodal library guide covers media units and aggregation; this article focuses on the strings you prepare before embedding.

What replaces task_type?

gemini-embedding-001 selects tasks through configuration. gemini-embedding-2 does not support that field and instead recommends task instructions in text-only inputs. The two models also use incompatible embedding spaces, so changing the query request alone does not migrate an existing index.

RoleEarlier modelEmbedding 2 text format
Search queryA matching task such as RETRIEVAL_QUERYtask: search result | query: {question}
Searchable documentA matching task such as RETRIEVAL_DOCUMENTtitle: {title} | text: {content}
Question-answering queryAn appropriate supported tasktask: question answering | query: {question}

The vertical bar is literal input text, not JSON syntax. With no document title, the current guide specifies title: none. Choose a task that matches the application and keep it consistent; a question mark alone is not a reason to switch retrieval formats from request to request.

A task instruction prepares the query while title and text prepare the document

Should both sides use the same prefix?

Document retrieval has different roles on its two sides. Use the corresponding query and document formats rather than assuming identical strings are required for compatibility.

This complete Python example prepares inputs locally. It requires no SDK and makes no model calls:

records = [
    {"id": "demo-return", "title": "Public demonstration return policy",
     "text": "Standard goods may be returned within seven days of delivery; custom goods require manual review."},
    {"id": "demo-delivery", "title": "Public demonstration shipping notes",
     "text": "After dispatch, shipment status is available on the order page."},
]
question = "Can custom goods be returned automatically?"
query_input = f"task: search result | query: {question}"
document_inputs = [
    {"id": row["id"],
     "input": f'title: {row["title"] or "none"} | text: {row["text"]}'}
    for row in records
]
print(query_input)
for row in document_inputs:
    print(row["id"], row["input"])

These are application records. Their id and input fields are not Google request fields. Build contents according to the endpoint or SDK you actually use, and preserve separate embeddings for separate documents. Aggregating unrelated records into one representation loses their independent retrieval identity.

We ran the input preparation locally and checked that both documents, the question, and their IDs survive. We did not call Gemini or measure a retrieval improvement.

Does a format change require rebuilding the index?

Moving from gemini-embedding-001 to gemini-embedding-2 requires re-embedding existing data according to Google. Equal dimensions do not make their spaces compatible. A template change within one model should first be tested in a temporary collection, with the old index retained while you compare policies.

Retain the old index while building and checking a separate collection

Store the effective model version, dimensions, document-template version, and source revision with index records. Record the query-template version too. These are application metadata, not lifecycle controls that the embedding provider manages for you.

If a rebuild still reuses old vectors, continue with the cache and index checks. Renaming a model in the client does not demonstrate that cache identity, outbound formatting, and indexed content all changed.

Does this apply to mixed text and media?

The text-only recommendation does not transfer unchanged to multimodal aggregation. Google generally advises against automatically prefixing the text portion of mixed media inputs because the effect varies by use case. That is a scope limit, not a claim that media inputs can never include useful context.

Likewise, Nbility's Embeddings API documentation describes an OpenAI-compatible text interface. It does not promise every option of Google's native embedContent API. Confirm the available model and endpoint contract before adapting a native example.

What would count as a successful migration?

Check retrieved evidence before judging the generated answer. For the demonstration corpus, ask about standard returns, the custom-goods exception, and shipment status. Add a question about a guaranteed review deadline, which the corpus does not provide.

Check formatted inputs, retrieved source IDs, and supported answers in that order

Hold chunking, dimensions, filters, and candidate count constant while changing the input policy. Keep the source IDs returned by each run. A useful result retrieves the right evidence, retains the exception, and recognizes missing information. HTTP success and fluent prose do not establish those properties. The unanswerable-question guide gives a way to organize that last check.

If the new policy performs worse, inspect the exact outbound strings, duplicate prefixes, and the collection queried before changing another component. The recommendation is documented; its benefit on your own corpus still needs to be measured.

Related posts

Run your Agent workflow through Nbility

Get an API key and connect OpenAI-compatible models and developer tools from one place.

Get an API key