Skip to content

Conversation

@dogdie233
Copy link
Contributor

@dogdie233 dogdie233 commented Dec 21, 2025

Description

This PR fixes a bug where the Dimensions property in EmbeddingGenerationOptions was ignored when generating embeddings.

Previously, setting Dimensions had no effect, and the API would return the full-length vector (e.g., 3072 for gemini-embedding-001). This change ensures the dimension parameter is correctly passed to the Gemini API.

Related Issue

Closes #95

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

I verified the fix using the code snippet that originally reproduced the issue.

Test Code:

var options = new EmbeddingGenerationOptions() { Dimensions = 1536 };
var generator = new GenerativeAIEmbeddingGenerator(key, "models/gemini-embedding-001");
var embedding = (await generator.GenerateAsync(["test"], options))[0].Vector;
Console.WriteLine(embedding.Length);

Summary by CodeRabbit

  • New Features
    • Embedding generation now supports configurable output dimensionality settings.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 21, 2025

Walkthrough

The change adds the OutputDimensionality parameter to the batch embedding request in the generative AI embedding generator. The parameter is populated from options.Dimensions, ensuring the requested dimension size is passed to the API call.

Changes

Cohort / File(s) Summary
Embedding Dimension Fix
src/GenerativeAI.Microsoft/GenerativeAIEmbeddingGenerator.cs
Added OutputDimensionality field set from options.Dimensions to the batch embed request construction, enabling the API to respect the requested embedding dimensions.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A rabbit hops through embedding space,
Where dimensions now find their place,
No more the thousand seventy-two,
Fifteen thirty-six shines bright and true!
A tiny fix, yet oh so grand,
The API listens to command! 🌟

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: support Dimensions parameter in GenerativeAI.Microsoft' directly and accurately describes the main change—adding support for the Dimensions parameter that was previously ignored in the embedding generation flow.
Linked Issues check ✅ Passed The PR successfully addresses the bug reported in issue #95 by ensuring the Dimensions parameter from EmbeddingGenerationOptions is now passed to the Gemini API via OutputDimensionality in the batch embed request.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to adding OutputDimensionality to the embedding request based on options.Dimensions, with no unrelated modifications to other functionality or files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/GenerativeAI.Microsoft/GenerativeAIEmbeddingGenerator.cs (1)

72-82: Consider adding dimension validation for better user experience.

While the API will reject invalid dimension values, adding client-side validation would provide clearer, more immediate feedback to users. This would catch common mistakes like negative values, zero, or dimensions unsupported by the model before making an API call.

💡 Example validation approach
 var requests = valuesList.Select(text => new EmbedContentRequest
 {
     Content = new Content
     {
         Role = Roles.User,
         Parts = [new Part { Text = text }]
     },
     Model = Model.Model,
     TaskType = GetTaskType(options),
-    OutputDimensionality = options?.Dimensions
+    OutputDimensionality = ValidateDimensions(options?.Dimensions)
 });

Then add a validation helper (example):

private static int? ValidateDimensions(int? dimensions)
{
    if (dimensions.HasValue)
    {
        if (dimensions.Value <= 0)
            throw new ArgumentException("Dimensions must be a positive integer.", nameof(dimensions));
        // Add model-specific range validation if needed
    }
    return dimensions;
}
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2689062 and 82bfe10.

📒 Files selected for processing (1)
  • src/GenerativeAI.Microsoft/GenerativeAIEmbeddingGenerator.cs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.cs

📄 CodeRabbit inference engine (CLAUDE.md)

C# code should use latest language features, enable nullable reference types, and include comprehensive XML documentation

Files:

  • src/GenerativeAI.Microsoft/GenerativeAIEmbeddingGenerator.cs
🔇 Additional comments (1)
src/GenerativeAI.Microsoft/GenerativeAIEmbeddingGenerator.cs (1)

80-81: The fix correctly passes the Dimensions parameter to the Gemini API, resolving the reported bug.

The null-conditional operator ensures safe handling when options or dimensions are not specified. The method's documentation is properly inherited via <inheritdoc/> per the C# coding guidelines.

@gunpal5 gunpal5 merged commit b30ef5c into gunpal5:main Dec 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Embedding Dimensions parameter is ignored (Returns 3072 instead of 1536) In

2 participants