OpenTelemetryChatClient records gen_ai.response.finish_reasons as a JSON string instead of string[]
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 75/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- csharp
調査の方向性
issueで挙げられている3か所から始めます。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時間
- マージ済み PR(30日)
- 23
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 が 6 日前に担当しました。 オープン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 件 ·
メンテナーはふだん 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対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン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 日以内に返信