33using System ;
44using System . Collections . Generic ;
55using System . Diagnostics ;
6+ using System . Diagnostics . CodeAnalysis ;
67using System . Linq ;
78using System . Text . Json ;
89using Microsoft . SemanticKernel . ChatCompletion ;
@@ -38,6 +39,8 @@ internal static class ModelDiagnostics
3839 /// Start a text completion activity for a given model.
3940 /// The activity will be tagged with the a set of attributes specified by the semantic conventions.
4041 /// </summary>
42+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
43+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
4144 internal static Activity ? StartCompletionActivity < TPromptExecutionSettings > (
4245 Uri ? endpoint ,
4346 string modelName ,
@@ -88,6 +91,8 @@ internal static class ModelDiagnostics
8891 /// Start a chat completion activity for a given model.
8992 /// The activity will be tagged with the a set of attributes specified by the semantic conventions.
9093 /// </summary>
94+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
95+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
9196 internal static Activity ? StartCompletionActivity < TPromptExecutionSettings > (
9297 Uri ? endpoint ,
9398 string modelName ,
@@ -141,6 +146,8 @@ internal static class ModelDiagnostics
141146 /// <summary>
142147 /// Start an agent invocation activity and return the activity.
143148 /// </summary>
149+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
150+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
144151 internal static Activity ? StartAgentInvocationActivity (
145152 string agentId ,
146153 string agentName ,
@@ -189,6 +196,8 @@ internal static class ModelDiagnostics
189196 /// <summary>
190197 /// Set the agent response for a given activity.
191198 /// </summary>
199+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
200+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
192201 internal static void SetAgentResponse ( this Activity activity , IEnumerable < ChatMessageContent > ? responses )
193202 {
194203 if ( ! IsModelDiagnosticsEnabled ( ) || responses is null )
@@ -207,6 +216,8 @@ internal static void SetAgentResponse(this Activity activity, IEnumerable<ChatMe
207216 /// <summary>
208217 /// End the agent streaming response for a given activity.
209218 /// </summary>
219+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
220+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
210221 internal static void EndAgentStreamingResponse (
211222 this Activity activity ,
212223 IEnumerable < StreamingChatMessageContent > ? contents )
@@ -244,19 +255,25 @@ internal static void EndAgentStreamingResponse(
244255 /// Set the text completion response for a given activity.
245256 /// The activity will be enriched with the response attributes specified by the semantic conventions.
246257 /// </summary>
258+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
259+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
247260 internal static void SetCompletionResponse ( this Activity activity , IEnumerable < TextContent > completions , int ? promptTokens = null , int ? completionTokens = null )
248261 => SetCompletionResponse ( activity , completions , promptTokens , completionTokens , ToGenAIConventionsChoiceFormat ) ;
249262
250263 /// <summary>
251264 /// Set the chat completion response for a given activity.
252265 /// The activity will be enriched with the response attributes specified by the semantic conventions.
253266 /// </summary>
267+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
268+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
254269 internal static void SetCompletionResponse ( this Activity activity , IEnumerable < ChatMessageContent > completions , int ? promptTokens = null , int ? completionTokens = null )
255270 => SetCompletionResponse ( activity , completions , promptTokens , completionTokens , ToGenAIConventionsChoiceFormat ) ;
256271
257272 /// <summary>
258273 /// Notify the end of streaming for a given activity.
259274 /// </summary>
275+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
276+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
260277 internal static void EndStreaming (
261278 this Activity activity ,
262279 IEnumerable < StreamingKernelContent > ? contents ,
@@ -313,6 +330,8 @@ internal static bool IsModelDiagnosticsEnabled()
313330 internal static bool HasListeners ( ) => s_activitySource . HasListeners ( ) ;
314331
315332 #region Private
333+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
334+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
316335 private static void AddOptionalTags < TPromptExecutionSettings > ( Activity ? activity , TPromptExecutionSettings ? executionSettings )
317336 where TPromptExecutionSettings : PromptExecutionSettings
318337 {
@@ -409,6 +428,8 @@ private static object ToGenAIConventionsFormat(KernelFunctionMetadata metadata)
409428 /// <summary>
410429 /// Convert a chat model response to a JSON string based on the OTel GenAI Semantic Conventions format
411430 /// </summary>
431+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
432+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
412433 private static string ToGenAIConventionsChoiceFormat ( ChatMessageContent chatMessage , int index )
413434 {
414435 var jsonObject = new
@@ -425,6 +446,8 @@ private static string ToGenAIConventionsChoiceFormat(ChatMessageContent chatMess
425446 /// <summary>
426447 /// Convert a text model response to a JSON string based on the OTel GenAI Semantic Conventions format
427448 /// </summary>
449+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
450+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
428451 private static string ToGenAIConventionsChoiceFormat ( TextContent textContent , int index )
429452 {
430453 var jsonObject = new
@@ -494,6 +517,8 @@ private static void SetCompletionResponse<T>(
494517 /// <summary>
495518 /// Set the streaming completion response for a given activity.
496519 /// </summary>
520+ [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
521+ [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)" ) ]
497522 private static void SetCompletionResponse (
498523 Activity activity ,
499524 Dictionary < int , List < StreamingKernelContent > > choices ,
0 commit comments