Skip to content

.Net: Fix GeminiPromptExecutionSettings.Clone dropping settable properties - #14194

Open
dfedoryshchev wants to merge 2 commits into
microsoft:mainfrom
dfedoryshchev:fix/gemini-clone-labels-cachedcontent
Open

.Net: Fix GeminiPromptExecutionSettings.Clone dropping settable properties#14194
dfedoryshchev wants to merge 2 commits into
microsoft:mainfrom
dfedoryshchev:fix/gemini-clone-labels-cachedcontent

Conversation

@dfedoryshchev

@dfedoryshchev dfedoryshchev commented Jul 24, 2026

Copy link
Copy Markdown

Motivation and Context

GeminiPromptExecutionSettings.Clone() rebuilds the settings through an object initializer, and four settable, serialized properties are missing from it. The clone therefore silently loses function calling, the service identifier, the request labels and the explicit-cache reference.

Function calling is the one that bites hardest. Kernel.CreateFunctionFromPrompt(...) stores a frozen Clone() of the settings it is given (dotnet/src/SemanticKernel.Abstractions/Functions/KernelFunction.cs:176). At request time GeminiPromptExecutionSettings.FromExecutionSettings derives ToolCallBehavior from FunctionChoiceBehavior, and GeminiChatCompletionClient configures the request from ToolCallBehavior. If FunctionChoiceBehavior did not survive the clone, no tools are sent and the model just answers in prose, with no error to explain it.

The four dropped properties:

  • ServiceId - [JsonPropertyName("service_id")], dotnet/src/SemanticKernel.Abstractions/AI/PromptExecutionSettings.cs:39
  • FunctionChoiceBehavior - [JsonPropertyName("function_choice_behavior")], dotnet/src/SemanticKernel.Abstractions/AI/PromptExecutionSettings.cs:91
  • Labels - [JsonPropertyName("labels")], dotnet/src/Connectors/Connectors.Google/GeminiPromptExecutionSettings.cs:153
  • CachedContent - [JsonPropertyName("cached_content")], dotnet/src/Connectors/Connectors.Google/GeminiPromptExecutionSettings.cs:272

The first two are base-class properties that PromptExecutionSettings.Clone() itself copies (PromptExecutionSettings.cs:153-154), and that OllamaPromptExecutionSettings.Clone() copies as well (OllamaPromptExecutionSettings.cs:180 and :187), so the Gemini override loses them rather than deliberately omitting them.

Description

  • Copy ServiceId and FunctionChoiceBehavior in Clone(), matching the base implementation.
  • Copy CachedContent, and Labels as a new dictionary (matching how ExtensionData and StopSequences are cloned so the clone does not share the caller's instance).
  • Extend PromptExecutionSettingsCloneWorksAsExpected so the source settings populate all four properties and the test asserts each one survives the clone.

Every other settable serialized property on GeminiPromptExecutionSettings and its base is now copied.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines
  • The existing tests pass
  • New tests added

Copilot AI review requested due to automatic review settings July 24, 2026 22:50
@dfedoryshchev
dfedoryshchev requested a review from a team as a code owner July 24, 2026 22:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a correctness issue in the .NET Google Gemini connector where GeminiPromptExecutionSettings.Clone() previously dropped two serialized, user-settable properties (Labels and CachedContent), causing cloned settings to lose request metadata and explicit caching references.

Changes:

  • Update GeminiPromptExecutionSettings.Clone() to clone Labels (as a new dictionary) and copy CachedContent.
  • Extend PromptExecutionSettingsCloneWorksAsExpected unit test to set and assert both labels and cached_content survive cloning.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
dotnet/src/Connectors/Connectors.Google/GeminiPromptExecutionSettings.cs Ensures Clone() preserves Labels and CachedContent (previously omitted).
dotnet/src/Connectors/Connectors.Google.UnitTests/GeminiPromptExecutionSettingsTests.cs Adds assertions verifying the new clone behavior for Labels and CachedContent.

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

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 5 | Confidence: 95% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by dfedoryshchev's agents

@dfedoryshchev

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

…ionSettings.Clone

Cloning also dropped the base ServiceId and FunctionChoiceBehavior properties. Losing FunctionChoiceBehavior disables function calling on the clone, since FromExecutionSettings derives ToolCallBehavior from it.
@dfedoryshchev dfedoryshchev changed the title .Net: Fix GeminiPromptExecutionSettings.Clone dropping Labels and CachedContent .Net: Fix GeminiPromptExecutionSettings.Clone dropping settable properties Jul 31, 2026
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.

2 participants