llamaIndex - ✅(Solved) Fix [Bug]: gemini-embedding-2 task instructions not implemented (task_type deprecated) [1 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
run-llama/llama_index#21535Fetched 2026-05-02 05:27:32
View on GitHub
Comments
1
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
labeled ×2commented ×1cross-referenced ×1

With the recent official release of the gemini-embedding-2 model, Google updated their API requirements for how embedding task types are handled. During the gemini-embedding-2-preview phase, these requirements were not strictly documented or enforced, but they are now a mandatory part of the official Gemini API specification.

Currently, LlamaIndex's GoogleGenAIEmbedding passes the task_type parameter inside the EmbedContentConfig payload. While this is correct for gemini-embedding-001, for gemini-embedding-2 the task_type field is deprecated and the backend ignores this parameter, resulting in the model embedding raw text without task-specific optimization.

Instead, Google now strongly recommend to add the task instruction in your prompt.

  • Queries: task: search result | query: {text}
  • Documents: title: {title} | text: {text} (or title: none | text: {text})
  • Other tasks: task: fact_verification | {text}, task: clustering | {text}, etc.

Error Message

Because the Gemini API silently ignores the task_type parameter for gemini-embedding-2 (rather than throwing a 400 error), users experience a silent failure where they believe their embeddings are task-optimized, but they actually aren't.

Root Cause

Impact on Users

Because LlamaIndex's BaseEmbedding implicitly routes standard operations through RETRIEVAL_QUERY and RETRIEVAL_DOCUMENT, the current integration handles gemini-embedding-2 incorrectly:

Fix Action

Fixed

PR fix notes

PR #21536: Fix/gemini embedding 2 task types

Description (problem / solution / changelog)

Description

Google recently released gemini-embedding-2 which introduces a breaking change to how task_type is handled. The task_type parameter is no longer supported in the request payload EmbedContentConfig.

As verified in #21535 , the API silently ignores the config parameter for v2 models instead of throwing an error. This results in LlamaIndex users receiving generic, unoptimized embeddings even when explicitly requesting RETRIEVAL_DOCUMENT or CODE_RETRIEVAL_QUERY.

To fix this, the Gemini API now requires the task instruction to be prefixed directly to the text strings (e.g., task: search result | query: {text} vs title: none | text: {text}).

Fixes #21535

New Package?

Did I fill in the tool.llamahub section in the pyproject.toml and provide a detailed README.md for my new integration or package?

  • Yes
  • No

Version Bump?

Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)

  • Yes
  • No

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • I added new unit tests to cover this change
  • I believe this change is already covered by existing unit tests

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran uv run make format; uv run make lint to appease the lint gods

Changed files

  • llama-index-integrations/embeddings/llama-index-embeddings-google-genai/llama_index/embeddings/google_genai/base.py (modified, +77/-5)
  • llama-index-integrations/embeddings/llama-index-embeddings-google-genai/pyproject.toml (modified, +1/-1)
  • llama-index-integrations/embeddings/llama-index-embeddings-google-genai/tests/test_embeddings_gemini.py (modified, +56/-2)

Code Example

from llama_index.embeddings.google_genai import GoogleGenAIEmbedding
from google.genai import types

# Use a dummy key for reproduction
API_KEY = "YOUR_API_KEY"
BASE_TEXT = "from llama_index.embeddings.google_genai import GoogleGenAIEmbedding"

# 1. Current behavior (Incorrect/No change in embedding)
model_retrieval = GoogleGenAIEmbedding(
    model_name="gemini-embedding-2", 
    api_key=API_KEY, 
    embedding_config=types.EmbedContentConfig(task_type="RETRIEVAL_QUERY")
)
model_code = GoogleGenAIEmbedding(
    model_name="gemini-embedding-2", 
    api_key=API_KEY, 
    embedding_config=types.EmbedContentConfig(task_type="CODE_RETRIEVAL_QUERY")
)

emb1 = model_retrieval.get_query_embedding(BASE_TEXT)
emb2 = model_code.get_query_embedding(BASE_TEXT)

print(f"Standard task_type comparison: {emb1 == emb2}") 
# Returns True (Incorrect: The vectors should be different)

# 2. Manual prefixing (The 'Gemini 2' way)
query_1 = f"task: retrieval_query | {BASE_TEXT}"
query_2 = f"task: code_retrieval_query | {BASE_TEXT}"

emb_manual_1 = model_retrieval.get_query_embedding(query_1)
emb_manual_2 = model_code.get_query_embedding(query_2)

print(f"Manual prefix comparison: {emb_manual_1 == emb_manual_2}") 
# Returns False (Correct: These are distinct task-specific vectors)

---

Standard task_type comparison: True
Manual prefix comparison: False
RAW_BUFFERClick to expand / collapse

Bug Description

Description

With the recent official release of the gemini-embedding-2 model, Google updated their API requirements for how embedding task types are handled. During the gemini-embedding-2-preview phase, these requirements were not strictly documented or enforced, but they are now a mandatory part of the official Gemini API specification.

Currently, LlamaIndex's GoogleGenAIEmbedding passes the task_type parameter inside the EmbedContentConfig payload. While this is correct for gemini-embedding-001, for gemini-embedding-2 the task_type field is deprecated and the backend ignores this parameter, resulting in the model embedding raw text without task-specific optimization.

Instead, Google now strongly recommend to add the task instruction in your prompt.

  • Queries: task: search result | query: {text}
  • Documents: title: {title} | text: {text} (or title: none | text: {text})
  • Other tasks: task: fact_verification | {text}, task: clustering | {text}, etc.

Impact on Users

Because LlamaIndex's BaseEmbedding implicitly routes standard operations through RETRIEVAL_QUERY and RETRIEVAL_DOCUMENT, the current integration handles gemini-embedding-2 incorrectly:

  • Silent Accuracy Loss: Users believe they are using optimized retrieval task types, but the model is actually falling back to a default embedding state because the task_type parameter is ignored.
  • Performance Degradation: The "massive accuracy improvements" promised by Gemini 2's asymmetric retrieval routing are lost without the string prefixes.

Proposed Solution

Modify _embed_texts and _aembed_texts in llama-index-embeddings-google-genai to conditionally handle "embedding-2" models:

  1. Map LlamaIndex's core task types (RETRIEVAL_QUERY, RETRIEVAL_DOCUMENT, FACT_VERIFICATION, etc.) to the exact string prefixes mandated by the new Gemini API docs.
  2. Format the texts list dynamically before making the API call.
  3. Safely strip the task_type key from the EmbedContentConfig for v2 models to prevent API rejection.
  4. Leave the logic completely untouched for gemini-embedding-001 for backward compatibility.

I have already implemented this fix with full test coverage mapping to the new API specifications. I will open a PR linking to this issue shortly.

References

https://ai.google.dev/gemini-api/docs/embeddings https://ai.google.dev/gemini-api/docs/embeddings#task-types-embeddings-2

Version

0.5.0

Steps to Reproduce

Because the Gemini API silently ignores the task_type parameter for gemini-embedding-2 (rather than throwing a 400 error), users experience a silent failure where they believe their embeddings are task-optimized, but they actually aren't.

You can prove this by requesting two different task types and seeing that the API returns the exact same vector:

from llama_index.embeddings.google_genai import GoogleGenAIEmbedding
from google.genai import types

# Use a dummy key for reproduction
API_KEY = "YOUR_API_KEY"
BASE_TEXT = "from llama_index.embeddings.google_genai import GoogleGenAIEmbedding"

# 1. Current behavior (Incorrect/No change in embedding)
model_retrieval = GoogleGenAIEmbedding(
    model_name="gemini-embedding-2", 
    api_key=API_KEY, 
    embedding_config=types.EmbedContentConfig(task_type="RETRIEVAL_QUERY")
)
model_code = GoogleGenAIEmbedding(
    model_name="gemini-embedding-2", 
    api_key=API_KEY, 
    embedding_config=types.EmbedContentConfig(task_type="CODE_RETRIEVAL_QUERY")
)

emb1 = model_retrieval.get_query_embedding(BASE_TEXT)
emb2 = model_code.get_query_embedding(BASE_TEXT)

print(f"Standard task_type comparison: {emb1 == emb2}") 
# Returns True (Incorrect: The vectors should be different)

# 2. Manual prefixing (The 'Gemini 2' way)
query_1 = f"task: retrieval_query | {BASE_TEXT}"
query_2 = f"task: code_retrieval_query | {BASE_TEXT}"

emb_manual_1 = model_retrieval.get_query_embedding(query_1)
emb_manual_2 = model_code.get_query_embedding(query_2)

print(f"Manual prefix comparison: {emb_manual_1 == emb_manual_2}") 
# Returns False (Correct: These are distinct task-specific vectors)

Relevant Logs/Tracebacks

Standard task_type comparison: True
Manual prefix comparison: False

extent analysis

TL;DR

Modify the _embed_texts and _aembed_texts methods in llama-index-embeddings-google-genai to conditionally handle "embedding-2" models by mapping LlamaIndex's core task types to the exact string prefixes mandated by the new Gemini API docs.

Guidance

  • Update the GoogleGenAIEmbedding class to dynamically format the texts list with the required task prefixes before making the API call.
  • Remove the task_type key from the EmbedContentConfig for gemini-embedding-2 models to prevent API rejection.
  • Ensure backward compatibility by leaving the logic untouched for gemini-embedding-001 models.
  • Verify the fix by comparing the embeddings generated with and without the task prefixes.

Example

# Example of formatting the text with the required task prefix
query_text = f"task: retrieval_query | {BASE_TEXT}"
emb = model_retrieval.get_query_embedding(query_text)

Notes

The proposed solution requires updating the llama-index-embeddings-google-genai module to handle the changes in the Gemini API. The fix should be applied to the _embed_texts and _aembed_texts methods to ensure correct handling of "embedding-2" models.

Recommendation

Apply the workaround by modifying the _embed_texts and _aembed_texts methods to conditionally handle "embedding-2" models, as this will ensure correct handling of task types and prevent silent accuracy loss.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING