VertexAIEmbedding with Output dimension = 768

I wish to use gemini-embedding-001 for Vertex AI embedding during creation of vectorstore and I wish to set it output dimension to 768. Where can I set the embedding config?

Hi @charlieckh

For the v1 versions!

Python:

  • The Vertex provider forwards a per-call dimensions argument to the API’s output_dimensionality:
# Tests demonstrate using dimensions=768 via the embed(...) method

@pytest.mark.parametrize(
    ("model_name", "embeddings_dim"),
    [
        (_DEFAULT_MODEL, 768),
        ("text-multilingual-embedding-002", 768),
        (_DEFAULT_MODEL, 3072),
    ],
)
def test_langchain_google_vertexai_embedding_with_output_dimensionality(
    model_name: str, embeddings_dim: int
) -> None:
    model = VertexAIEmbeddings(model=model_name)
    output = model.embed(
        texts=["foo bar"],
        dimensions=embeddings_dim,
    )
from langchain_google_vertexai import VertexAIEmbeddings

emb = VertexAIEmbeddings(model="gemini-embedding-001", project="YOUR_PROJECT", location="us-central1")
vecs = emb.embed(texts=["..."], dimensions=768)

Note: The class doesn’t expose a constructor-level default for dimensions. Vector stores that call embed_documents/embed_query won’t pass dimensions, so if you need 768 inside a vectorstore flow you’ll either:

  • Call emb.embed(…, dimensions=768) yourself before upserting, or
  • Subclass and override embed_documents/embed_query to call embed(…, dimensions=768).

For the Generative AI (non-Vertex) provider, the per-call parameter is output_dimensionality on embed_query/embed_documents:

def _build_embed_request(
    self,
    text: str,
    task_type: Optional[str] = None,
    title: Optional[str] = None,
    output_dimensionality: Optional[int] = None,
) -> EmbedContentRequest:
...
        output_dimensionality=output_dimensionality,

JavaScript/TypeScript

  • The base embeddings interface supports a constructor-level dimensions (alias outputDimensionality), which is carried through to the request:
export interface BaseGoogleEmbeddingsParams<AuthOptions>
  extends EmbeddingsParams,
    GoogleConnectionParams<AuthOptions> {
  model: string;

  /**
   * Used to specify output embedding size.
   * If set, output embeddings will be truncated to the size specified.
   */
  dimensions?: number;

  /**
   * An alias for "dimensions"
   */
  outputDimensionality?: number;
}
  buildParameters(): VertexEmbeddingsParameters {
    const ret: VertexEmbeddingsParameters = {
      outputDimensionality: this.dimensions,
    };
  • In the Vertex-specific wrapper, you just pass those fields through the constructor:
  constructor(fields: GoogleVertexAIEmbeddingsInput) {
    super({
      ...fields,
      platformType: "gcp",
    });
  }
  • Usage:
import { VertexAIEmbeddings } from "@langchain/google-vertexai";

const embeddings = new VertexAIEmbeddings({
  model: "gemini-embedding-001",
  dimensions: 768, // or outputDimensionality: 768
  location: "us-central1",
  // project / auth via gcloud or explicit options as needed
});
  • This works for both document and query embeddings because the base class uses the stored dimensions when building request parameters, so vectorstores that call embedDocuments/embedQuery will inherit 768 automatically.
  • FYI default for gemini-embedding-001 is 3072 (confirmed in tests).

Summary:

  • Python Vertex: set per-call with dimensions=768 on embed(…). No constructor default; vectorstores won’t pass it automatically.

  • JS Vertex: set at construction with dimensions: 768 (or outputDimensionality). It’s forwarded to all embedding calls, including vectorstore usage.

1 Like

Do I still need to perform extra normalisation from 3072 to 768 for this?. Thanks for your answer!

have you already created your embeddings? Or will you generate them from scratch?

I have already created my embedding by using embedding.embed. And now i am wondering whether extra normalisation for the embedding is needed