Skip to content

Commit b07f859

Browse files
Copilotstephentoub
authored andcommitted
Add Name property to OtelMessage to store ChatMessage.AuthorName per OpenTelemetry semantic conventions (dotnet#6953)
* Initial plan * Add Name property to OtelMessage and populate from ChatMessage.AuthorName Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Refactor: integrate AuthorName test into existing ExpectedInformationLogged_Async test Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent c46a478 commit b07f859

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ internal static string SerializeChatMessages(
239239
message.Role == ChatRole.Tool ? "tool" :
240240
message.Role == ChatRole.System || message.Role == new ChatRole("developer") ? "system" :
241241
"user",
242+
Name = message.AuthorName,
242243
};
243244

244245
foreach (AIContent content in message.Contents)
@@ -595,6 +596,7 @@ private void AddOutputMessagesTags(ChatResponse response, Activity? activity)
595596
private sealed class OtelMessage
596597
{
597598
public string? Role { get; set; }
599+
public string? Name { get; set; }
598600
public List<object> Parts { get; set; } = [];
599601
public string? FinishReason { get; set; }
600602
}

test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
105105
List<ChatMessage> messages =
106106
[
107107
new(ChatRole.System, "You are a close friend."),
108-
new(ChatRole.User, "Hey!"),
108+
new(ChatRole.User, "Hey!") { AuthorName = "Alice" },
109109
new(ChatRole.Assistant, [new FunctionCallContent("12345", "GetPersonName")]),
110110
new(ChatRole.Tool, [new FunctionResultContent("12345", "John")]),
111-
new(ChatRole.Assistant, "Hey John, what's up?"),
111+
new(ChatRole.Assistant, "Hey John, what's up?") { AuthorName = "BotAssistant" },
112112
new(ChatRole.User, "What's the biggest animal?")
113113
];
114114

@@ -201,6 +201,7 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
201201
},
202202
{
203203
"role": "user",
204+
"name": "Alice",
204205
"parts": [
205206
{
206207
"type": "text",
@@ -230,6 +231,7 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
230231
},
231232
{
232233
"role": "assistant",
234+
"name": "BotAssistant",
233235
"parts": [
234236
{
235237
"type": "text",

0 commit comments

Comments
 (0)