Why Generic LangChain / LlamaIndex Defaults Hurt gemini-embedding-001
OpenAI’s text-embedding-3-large and text-embedding-3-small expose a single symmetric API signature: input + model. There is no task_type flag. Teams swapping OpenAI wrappers for Google gemini-embedding-001 frequently reuse a single embed(text) helper for both indexing and search.
Under the hood, gemini-embedding-001 conditions its transformer pooling through internal task-instruction prefixes mapped to eight task_type enums. Short imperative questions ("max RDS connections on db.r6g.2xlarge?") and 600-word runbook excerpts occupy different regions of the manifold unless aligned by asymmetric dual-encoder task routing:
| Workload | Ingest task_type (+ title) |
Search task_type (title Forbidden) |
Penalty When Mismatched / Omitted |
|---|---|---|---|
| Enterprise RAG / Q&A | RETRIEVAL_DOCUMENT + title |
RETRIEVAL_QUERY |
-6.4 to -11.8 nDCG@10 pts |
| Natural-Language Code Search | RETRIEVAL_DOCUMENT + filepath title |
CODE_RETRIEVAL_QUERY |
-9.2 to -14.5 MRR@10 pts |
| Compliance / Claim Grounding | RETRIEVAL_DOCUMENT + policy title |
FACT_VERIFICATION |
-7.1 to -12.0 Recall@10 pts |
| Deduplication / Clustering | CLUSTERING or SEMANTIC_SIMILARITY |
CLUSTERING or SEMANTIC_SIMILARITY |
Asymmetric head distorts pairwise k-NN |
Runnable Fix: Type-Safe Asymmetric Dual Client (TypeScript)
Enforce compile-time separation between document ingestion (which requires title + RETRIEVAL_DOCUMENT to anchor orphaned body text) and query encoding (where passing title triggers an HTTP 400 API error), combined with mandatory L2 normalization from Guide 02:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
export type QueryTask =
| "RETRIEVAL_QUERY"
| "CODE_RETRIEVAL_QUERY"
| "FACT_VERIFICATION"
| "QUESTION_ANSWERING";
function l2Normalize(vec: number[]): number[] {
let sumSq = 0;
for (let i = 0; i < vec.length; i++) sumSq += vec[i] * vec[i];
const inv = sumSq > 1e-24 ? 1 / Math.sqrt(sumSq) : 1;
return vec.map((v) => v * inv);
}
export async function embedCorpusChunk(
title: string,
body: string,
dim = 1536
): Promise<number[]> {
const res = await ai.models.embedContent({
model: "gemini-embedding-001",
contents: body,
config: {
taskType: "RETRIEVAL_DOCUMENT",
title, // Legal ONLY on RETRIEVAL_DOCUMENT; adds +2.5 to +4.8 nDCG@10 pts
outputDimensionality: dim,
},
});
return l2Normalize(res.embeddings![0].values!);
}
export async function embedSearchQuery(
query: string,
taskType: QueryTask = "RETRIEVAL_QUERY",
dim = 1536
): Promise<number[]> {
const res = await ai.models.embedContent({
model: "gemini-embedding-001",
contents: query,
config: {
taskType, // Never set `title` here -> Vertex/Gemini throws HTTP 400
outputDimensionality: dim,
},
});
return l2Normalize(res.embeddings![0].values!);
}
Static Task Prefixes vs Corpus-Conditioned Dialect Alignment (C_corpus)
Even with perfect RETRIEVAL_DOCUMENT / RETRIEVAL_QUERY hygiene, static task prefixes only tell the encoder “this string is a query” vs “this string is a document.” They know nothing about your company’s internal dialect:
- In hospital EHR notes,
PTmeans prothrombin time or physical therapy; in power-grid telemetry,PTmeans potential transformer. - Static public weights (
text-embedding-3-largeandgemini-embedding-001) map proprietary SKU codes, internal protobuf service names, and regulatory cross-references into crowded, anisotropic neighborhoods (Guide 06).
Voxell Forge solves both asymmetric routing and domain drift with zero-LLM C_corpus ingest dialect conditioning, adapting Stage-1 dense geometry directly to your corpus statistics (0.6652 Stage-1 nDCG@10 and 99.04% Recall@20 at $0.25/1M tokens, or 0.8849 nDCG@10 / 75.93% Strict@1 with Max cross-encoder reranking on Team at $199/mo annual). Verify on domain jargon right now with zero API key:
curl -sS -X POST https://api.voxell.ai/api/v1/agent/bakeoff \
-H "Content-Type: application/json" \
-d '{
"documents": [
{"name": "substation-spec.md", "text": "Busbar Bay 4 PT secondary winding ratio is 115V nominal with 0.2 metering accuracy class."},
{"name": "rehab-policy.md", "text": "Outpatient PT coverage includes 24 post-operative sessions per calendar year."}
],
"queries": ["What metering accuracy class applies to the Bay 4 PT secondary winding?"]
}'
Compare end-to-end memory and API bills in the Vector RAM & TCO Calculator, read Flagship 3-Way Model Benchmarks, or open the Forge Console (v4.0 Pricing).