diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/AIRequestSettings.cs b/dotnet/src/SemanticKernel.Abstractions/AI/AIRequestSettings.cs index 7ec825adc127..4856b2adc563 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/AIRequestSettings.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/AIRequestSettings.cs @@ -15,6 +15,8 @@ namespace Microsoft.SemanticKernel.AI; /// public class AIRequestSettings { + private Dictionary? _extensionData; + /// /// Service identifier. /// This identifies a service and is set when the AI service is registered. @@ -33,5 +35,9 @@ public class AIRequestSettings /// Extra properties /// [JsonExtensionData] - public Dictionary ExtensionData { get; set; } = new Dictionary(); + public Dictionary ExtensionData + { + get => this._extensionData ??= new(); + set => this._extensionData = value; + } } diff --git a/dotnet/src/SemanticKernel.Abstractions/Events/FunctionInvokedEventArgs.cs b/dotnet/src/SemanticKernel.Abstractions/Events/FunctionInvokedEventArgs.cs index 78d41edc23fc..b36674e28a15 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Events/FunctionInvokedEventArgs.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Events/FunctionInvokedEventArgs.cs @@ -10,6 +10,8 @@ namespace Microsoft.SemanticKernel.Events; /// public class FunctionInvokedEventArgs : SKCancelEventArgs { + private Dictionary? _metadata; + /// /// Indicates if the function execution should repeat. /// @@ -18,7 +20,7 @@ public class FunctionInvokedEventArgs : SKCancelEventArgs /// /// Metadata for storing additional information about function execution result. /// - public Dictionary Metadata { get; private set; } = new Dictionary(); + public Dictionary Metadata => this._metadata ??= new(); /// /// Initializes a new instance of the class. @@ -27,7 +29,7 @@ public class FunctionInvokedEventArgs : SKCancelEventArgs /// Function result public FunctionInvokedEventArgs(FunctionView functionView, FunctionResult result) : base(functionView, result.Context) { - this.Metadata = result.Metadata; + this._metadata = result._metadata; } /// diff --git a/dotnet/src/SemanticKernel.Abstractions/Orchestration/FunctionResult.cs b/dotnet/src/SemanticKernel.Abstractions/Orchestration/FunctionResult.cs index f600e84ffe62..eaac4d7ae3e1 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Orchestration/FunctionResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Orchestration/FunctionResult.cs @@ -10,6 +10,8 @@ namespace Microsoft.SemanticKernel.Orchestration; /// public sealed class FunctionResult { + internal Dictionary? _metadata; + /// /// Name of executed function. /// @@ -23,7 +25,11 @@ public sealed class FunctionResult /// /// Metadata for storing additional information about function execution result. /// - public Dictionary Metadata { get; internal set; } = new Dictionary(); + public Dictionary Metadata + { + get => this._metadata ??= new(); + internal set => this._metadata = value; + } /// /// Function result object. @@ -86,7 +92,8 @@ public FunctionResult(string functionName, string pluginName, SKContext context, /// public bool TryGetMetadataValue(string key, out T value) { - if (this.Metadata.TryGetValue(key, out object? valueObject) && + if (this._metadata is { } metadata && + metadata.TryGetValue(key, out object? valueObject) && valueObject is T typedValue) { value = typedValue;