OpenTelemetryChatClient records gen_ai.response.finish_reasons as a JSON string instead of string[]
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 75/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
从 issue 中提到的三个位置开始:OpenTelemetryChatClient.cs 第338和447行,以及 OpenTelemetryRealtimeClientSession.cs 第835行。阅读 OpenTelemetryChatClientTests.cs 中相应的断言,并检查它们的标签值是如何导出的。完成标准是:结束原因和停止序列以字符串数组的形式输出,并更新受影响的测试以验证这种格式。
由索引模型根据 Issue 内容生成。
描述
Description
OpenTelemetryChatClient records gen_ai.response.finish_reasons as a single string that contains JSON, ["stop"]. However, the GenAI semantic conventions define the attribute as string[], so over OTLP it arrives as a stringValue where an arrayValue is expected. I ran into this while checking several AI frameworks against the GenAI conventions.
The value is formatted as a string in the library itself: https://github.com/dotnet/extensions/blob/c209c80ca2acafdb8dfa417c3e1e55b902d32986/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs#L447
There are two more places with the same behavior:
gen_ai.request.stop_sequences, also string[] in the conventions: https://github.com/dotnet/extensions/blob/c209c80ca2acafdb8dfa417c3e1e55b902d32986/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs#L338
gen_ai.response.finish_reasons in the realtime session: https://github.com/dotnet/extensions/blob/c209c80ca2acafdb8dfa417c3e1e55b902d32986/src/Libraries/Microsoft.Extensions.AI/Realtime/OpenTelemetryRealtimeClientSession.cs#L835
The stop sequences are not escaped, so a sequence that contains a double quote or a newline produces a value that isn't valid JSON.
The unit tests assert the string form, so it may have been done intentionally: https://github.com/dotnet/extensions/blob/c209c80ca2acafdb8dfa417c3e1e55b902d32986/test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs#L183-L189 If there is a reason for it that I'm missing, I'd like to know.
In practice this means a query like "spans where finish_reasons contains length" doesn't work unless the backend parses the JSON first.
Reproduction Steps
Minimal repro without a model or an OTLP receiver. A stub client returns FinishReason = Stop, and an ActivityListener prints the tag types:
using System.Diagnostics;
using Microsoft.Extensions.AI;
using var listener = new ActivityListener
{
ShouldListenTo = s => s.Name == "Experimental.Microsoft.Extensions.AI",
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStopped = a =>
{
foreach (var key in new[] { "gen_ai.response.finish_reasons", "gen_ai.request.stop_sequences" })
{
var v = a.GetTagItem(key);
Console.WriteLine($"{key}: {v?.GetType().Name} {v}");
}
},
};
ActivitySource.AddActivityListener(listener);
IChatClient client = new StubChatClient().AsBuilder().UseOpenTelemetry().Build();
await client.GetResponseAsync("hi", new ChatOptions { StopSequences = ["say \"bye\""] });
sealed class StubChatClient : IChatClient
{
public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) =>
Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "hi")) { FinishReason = ChatFinishReason.Stop });
public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) =>
throw new NotSupportedException();
public object? GetService(Type serviceType, object? serviceKey = null) => null;
public void Dispose() { }
}
Output:
gen_ai.response.finish_reasons: String ["stop"]
gen_ai.request.stop_sequences: String ["say "bye""]
I see the same in a real run with OpenAI and the OTLP exporter, the exported attribute is shown under Actual behavior.
Expected behavior
The tag value is a string[], for example activity.AddTag("gen_ai.response.finish_reasons", new[] { "stop" }), and the exported attribute is an array:
{
"key": "gen_ai.response.finish_reasons",
"value": { "arrayValue": { "values": [ { "stringValue": "stop" } ] } }
}
Same for gen_ai.request.stop_sequences.
Actual behavior
{
"key": "gen_ai.response.finish_reasons",
"value": { "stringValue": "[\"stop\"]" }
}
With a stop sequence that contains a quote, the value isn't valid JSON either: ["say "bye""].
Regression?
No, it has been like this since the first Microsoft.Extensions.AI commit (01a52dd).
Known Workarounds
Parse the string on the consumer side, or rewrite the tag in a processor before export.
Configuration
Stub repro: Microsoft.Extensions.AI 10.11.1, .NET 10 (net10.0), macOS
End-to-end repro: Microsoft.Extensions.AI 10.11.0, Microsoft.Extensions.AI.OpenAI 10.11.0, OpenTelemetry 1.20.1, OpenTelemetry.Exporter.OpenTelemetryProtocol 1.20.1, .NET SDK 9.0.101, net9.0, macOS
Not specific to configuration: the string is built in the library, and the same lines are on main today.
Other information
I can send a PR that passes arrays and updates the two assertions, if you agree it should change.
- 主要语言
- C#
- 星标
- 3.3k
- 派生
- 928
- 平均合并
- 2 天 12 小时
- 30 天内合并 PR
- 23
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/extensions 的其他 Issue
-
MarkdownReader throws NotSupportedException on task lists (- [ ])可能已有人在做 @Laurianti 于 5 天前认领。 未关闭area-ai untriaged
难度 2/5 1-3 小时 新手友好度 74/100
dotnet/extensions#7982 ·
维护者通常 1 天内回复
-
FakeTimeProvider keeps a non-UTC offset in GetUtcNow()可能已有人在做 @lqkhanh295 于 5 天前认领。 未关闭area-fundamentals bug untriaged
难度 2/5 1-3 小时 新手友好度 75/100
dotnet/extensions#7979 · 1 条评论 ·
维护者通常 1 天内回复
-
DataContent rejects RFC 2397 data URIs that give only a charset (data:;charset=utf-8,...)可能已有人在做 @caigen 于 10 天前认领。 未关闭area-ai untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/extensions#7960 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
area-ai untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/extensions#7956 · 1 条评论 ·
维护者通常 1 天内回复
-
[MEAI.OpenAI] OpenAIResponsesChatClient streams reasoning and non-message items with Role=null, merging them into preceding Tool messages ```可能已有人在做 @caigen 于 10 天前认领。 未关闭area-ai bug untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/extensions#7954 ·
维护者通常 1 天内回复
查看 dotnet/extensions 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/fluentui-blazor#5688 ·
维护者通常 1 天内回复
-
chore: remove stale IL3050/IL3053 workaround from the iOS sample可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭State: Needs Discussion
难度 2/5 1-3 小时 新手友好度 76/100
getsentry/sentry-dotnet#6071 · 1 条评论 ·
维护者通常 1 天内回复
-
:watch: Not Triaged dotnet-target-version
难度 1/5 1 小时以内 新手友好度 84/100
dotnet/docs-desktop#2313 ·
维护者通常 4 天内回复
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 77/100
-
area/frontend type/bug
难度 2/5 1-3 小时 新手友好度 65/100
umbraco/Umbraco-CMS#24867 · 1 条评论 ·
维护者通常 1 天内回复