Skip to content

.Net: feat: Add support for dimensions in Vertex AI embedding services#13612

Open
abbottdev wants to merge 5 commits intomicrosoft:mainfrom
abbottdev:feature/vertex-dimensions
Open

.Net: feat: Add support for dimensions in Vertex AI embedding services#13612
abbottdev wants to merge 5 commits intomicrosoft:mainfrom
abbottdev:feature/vertex-dimensions

Conversation

@abbottdev
Copy link
Contributor

Motivation and Context

Why is this change required?
PR #10489 added support for configuring embedding [dimensions] (outputDimensionality) for the Google AI connector, but the equivalent Vertex AI connector was not updated. This means specifying [Dimensions] in [EmbeddingGenerationOptions] or via the constructor has no effect when using Vertex AI — the API always returns the model's default dimensionality.

What problem does it solve?
When using [VertexAIEmbeddingGenerator] or [VertexAITextEmbeddingGenerationService]with a [dimensions] value (e.g. 128), the output embedding length is the model default (e.g. 3072) instead of the requested size.

What scenario does it contribute to?
Users who need to control embedding dimensionality for storage optimization, performance, or compatibility with downstream systems when using the Vertex AI endpoint.

Fixes: #12988

Description

This PR adds outputDimensionality support to the Vertex AI embedding connector, mirroring the existing Google AI implementation from PR #10489.

The Google connector has two parallel embedding paths — Google AI (uses API key, calls generativelanguage.googleapis.com) and Vertex AI (uses bearer token, calls [{location}-aiplatform.googleapis.com]). PR #10489 only wired up dimensions for the Google AI path. This PR applies the same pattern to every layer of the Vertex AI path.

The key structural difference between the two APIs is where outputDimensionality goes in the request JSON:

Google AI puts it per-content-item: { "requests": [{ "content": {...}, "outputDimensionality": 128 }] }
Vertex AI puts it in the shared parameters block: { "instances": [...], "parameters": { "autoTruncate": false, "outputDimensionality": 128 } }
The implementation follows this difference. In [VertexAIEmbeddingRequest], outputDimensionality is added to the existing [RequestParameters] class (alongside autoTruncate), rather than on each instance item.

Dimensions flow through the same chain as Google AI:

  1. Extension methods accept int? dimensions = null and pass it to the generator/service constructor
  2. [VertexAIEmbeddingGenerator] passes it to [VertexAITextEmbeddingGenerationService]
  3. The service passes it to [VertexAIEmbeddingClient], which stores it as a default
  4. At request time, the client resolves the final value as [options?.Dimensions ?? this._dimensions] — runtime [EmbeddingGenerationOptions] take priority over the constructor default
  5. [VertexAIEmbeddingRequest.FromData()] sets it on the parameters block, and [JsonIgnore(WhenWritingNull)] ensures it's omitted when not specified

All new parameters default to null, preserving full backward compatibility.

Contribution Checklist

@abbottdev abbottdev requested a review from a team as a code owner March 1, 2026 11:35
@moonbox3 moonbox3 added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Mar 1, 2026
@github-actions github-actions bot changed the title feat: Add support for dimensions in Vertex AI embedding services .Net: feat: Add support for dimensions in Vertex AI embedding services Mar 1, 2026
@abbottdev
Copy link
Contributor Author

@moonbox3 - any chance we can get some reviews on this PR?

@westey-m westey-m requested a review from Copilot March 19, 2026 18:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for configuring embedding output dimensionality (“dimensions” / outputDimensionality) for Vertex AI embedding connectors, matching the existing Google AI connector behavior and ensuring request-time options can override constructor defaults.

Changes:

  • Thread dimensions through Vertex AI DI/kernel-builder extensions, generator/service constructors, and client request creation.
  • Add outputDimensionality to Vertex AI request parameters JSON and omit it when null.
  • Add unit tests validating metadata/attributes and request payload inclusion/omission.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
dotnet/src/Connectors/Connectors.Google/Services/VertexAITextEmbeddingGenerationService.cs Adds dimensions constructor option, attributes metadata, and an overload accepting EmbeddingGenerationOptions.
dotnet/src/Connectors/Connectors.Google/Services/VertexAIEmbeddingGenerator.cs Plumbs dimensions through generator constructors into the service.
dotnet/src/Connectors/Connectors.Google/Extensions/VertexAIServiceCollectionExtensions.cs Adds dimensions optional parameter to DI registration helpers for embedding generation.
dotnet/src/Connectors/Connectors.Google/Extensions/VertexAIServiceCollectionExtensions.DependencyInjection.cs Adds dimensions optional parameter to embedding generator registration overloads.
dotnet/src/Connectors/Connectors.Google/Extensions/VertexAIKernelBuilderExtensions.cs Adds dimensions to kernel builder extension methods and forwards into service collection registration.
dotnet/src/Connectors/Connectors.Google/Core/VertexAI/VertexAIEmbeddingRequest.cs Adds outputDimensionality under parameters in the Vertex AI request model.
dotnet/src/Connectors/Connectors.Google/Core/VertexAI/VertexAIEmbeddingClient.cs Stores default dimensions and resolves final value from options?.Dimensions ?? _dimensions.
dotnet/src/Connectors/Connectors.Google.UnitTests/Services/VertexAITextEmbeddingGenerationServiceTests.cs Adds tests for dimensions attributes and request serialization behavior.
dotnet/src/Connectors/Connectors.Google.UnitTests/Services/VertexAIEmbeddingGeneratorTests.cs Adds tests for generator metadata and request serialization behavior with dimensions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Comment on lines +83 to +84
private VertexAIEmbeddingRequest GetEmbeddingRequest(IEnumerable<string> data, EmbeddingGenerationOptions? options = null)
=> VertexAIEmbeddingRequest.FromData(data, options?.Dimensions ?? this._dimensions);
Comment on lines +135 to +136
var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent);
Assert.DoesNotContain("outputDimensionality", requestBody);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Bug: Embedding generation dimensions works in Google Gemini but fails in VertexAI

4 participants