@@ -217,7 +217,7 @@ public override async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseA
217217 }
218218 }
219219
220- private static string SerializeChatMessages ( IEnumerable < ChatMessage > messages , ChatFinishReason ? chatFinishReason = null )
220+ internal static string SerializeChatMessages ( IEnumerable < ChatMessage > messages , ChatFinishReason ? chatFinishReason = null )
221221 {
222222 List < object > output = [ ] ;
223223
@@ -244,6 +244,12 @@ private static string SerializeChatMessages(IEnumerable<ChatMessage> messages, C
244244 {
245245 switch ( content )
246246 {
247+ // These are all specified in the convention:
248+
249+ case TextContent tc when ! string . IsNullOrWhiteSpace ( tc . Text ) :
250+ m . Parts . Add ( new OtelGenericPart { Content = tc . Text } ) ;
251+ break ;
252+
247253 case FunctionCallContent fcc :
248254 m . Parts . Add ( new OtelToolCallRequestPart
249255 {
@@ -261,8 +267,30 @@ private static string SerializeChatMessages(IEnumerable<ChatMessage> messages, C
261267 } ) ;
262268 break ;
263269
264- case TextContent tc :
265- m . Parts . Add ( new OtelGenericPart { Content = tc . Text } ) ;
270+ // These are non-standard and are using the "generic" non-text part that provides an extensibility mechanism:
271+
272+ case TextReasoningContent trc when ! string . IsNullOrWhiteSpace ( trc . Text ) :
273+ m . Parts . Add ( new OtelGenericPart { Type = "reasoning" , Content = trc . Text } ) ;
274+ break ;
275+
276+ case UriContent uc :
277+ m . Parts . Add ( new OtelGenericPart { Type = "image" , Content = uc . Uri . ToString ( ) } ) ;
278+ break ;
279+
280+ case DataContent dc :
281+ m . Parts . Add ( new OtelGenericPart { Type = "image" , Content = dc . Uri } ) ;
282+ break ;
283+
284+ case HostedFileContent fc :
285+ m . Parts . Add ( new OtelGenericPart { Type = "file" , Content = fc . FileId } ) ;
286+ break ;
287+
288+ case HostedVectorStoreContent vsc :
289+ m . Parts . Add ( new OtelGenericPart { Type = "vector_store" , Content = vsc . VectorStoreId } ) ;
290+ break ;
291+
292+ case ErrorContent ec :
293+ m . Parts . Add ( new OtelGenericPart { Type = "error" , Content = ec . Message } ) ;
266294 break ;
267295
268296 default :
@@ -293,7 +321,7 @@ private static string SerializeChatMessages(IEnumerable<ChatMessage> messages, C
293321 string . IsNullOrWhiteSpace ( modelId ) ? OpenTelemetryConsts . GenAI . Chat : $ "{ OpenTelemetryConsts . GenAI . Chat } { modelId } ",
294322 ActivityKind . Client ) ;
295323
296- if ( activity is not null )
324+ if ( activity is { IsAllDataRequested : true } )
297325 {
298326 _ = activity
299327 . AddTag ( OpenTelemetryConsts . GenAI . Operation . Name , OpenTelemetryConsts . GenAI . Chat )
@@ -411,15 +439,15 @@ private void TraceResponse(
411439 TagList tags = default ;
412440 tags . Add ( OpenTelemetryConsts . GenAI . Token . Type , OpenTelemetryConsts . TokenTypeInput ) ;
413441 AddMetricTags ( ref tags , requestModelId , response ) ;
414- _tokenUsageHistogram . Record ( ( int ) inputTokens ) ;
442+ _tokenUsageHistogram . Record ( ( int ) inputTokens , tags ) ;
415443 }
416444
417445 if ( usage . OutputTokenCount is long outputTokens )
418446 {
419447 TagList tags = default ;
420448 tags . Add ( OpenTelemetryConsts . GenAI . Token . Type , OpenTelemetryConsts . TokenTypeOutput ) ;
421449 AddMetricTags ( ref tags , requestModelId , response ) ;
422- _tokenUsageHistogram . Record ( ( int ) outputTokens ) ;
450+ _tokenUsageHistogram . Record ( ( int ) outputTokens , tags ) ;
423451 }
424452 }
425453
0 commit comments