[MEAI.OpenAI] OpenAIResponsesChatClient streams reasoning and non-message items with Role=null, merging them into preceding Tool messages ```
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
Hướng nghiên cứu
Bắt đầu trong src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs, tập trung vào cách lastRole được thiết lập cho reasoning và các mục đầu ra khác trước thông điệp hoặc lệnh gọi hàm đầu tiên. So sánh với cách xử lý FunctionCallOutputResponseItem hiện có, sau đó kiểm tra ChatResponseExtensions.ProcessUpdate để hiểu cách các bản cập nhật không có role được kết hợp. Bản tái hiện đạt yêu cầu khi đặt reasoning và các mục đầu ra khác của lượt thứ hai trong một thông điệp assistant, chỉ để lại kết quả hàm trong thông điệp tool.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
In OpenAIResponsesChatClient.GetStreamingResponseAsync, lastRole and lastMessageId are initialized to null at the start of each model call and are only assigned when a MessageResponseItem or FunctionCallResponseItem is added.
Because OpenAI's /v1/responses streaming API emits reasoning items (and default output items such as compaction) before any message or function_call item, those ChatResponseUpdate instances are yielded with Role = null and MessageId = null.
When OpenAIResponsesChatClient runs inside a multi-turn tool loop (such as FunctionInvokingChatClient or Microsoft.Agents.AI agents) and updates are combined via ToChatResponse() / ToChatResponseAsync() — including inside OpenTelemetryChatClient when serializing gen_ai.output.messages — ChatResponseExtensions.ProcessUpdate treats Role == null && MessageId == null as a continuation of the previous message and appends the next model call's TextReasoningContent (and AIContent items) onto the preceding ChatRole.Tool message.
Reproduction Steps
Create a .NET 10.0 console project with Microsoft.Extensions.AI 11.0.0 and Microsoft.Extensions.AI.OpenAI 11.0.0 (no API key or network access needed) and run dotnet run:
<!-- Repro.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>OPENAI001</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" Version="11.0.0" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="11.0.0" />
</ItemGroup>
</Project>
// Program.cs
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Net;
using System.Text;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Responses;
const string Turn1Sse = """
event: response.created
data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_1","object":"response","created_at":1700000000,"status":"in_progress","model":"gpt-5","output":[]}}
event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":1,"output_index":0,"item":{"id":"fc_1","type":"function_call","status":"in_progress","call_id":"call_1","name":"get_menu","arguments":""}}
event: response.function_call_arguments.delta
data: {"type":"response.function_call_arguments.delta","sequence_number":2,"item_id":"fc_1","output_index":0,"delta":"{}"}
event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":3,"output_index":0,"item":{"id":"fc_1","type":"function_call","status":"completed","call_id":"call_1","name":"get_menu","arguments":"{}"}}
event: response.completed
data: {"type":"response.completed","sequence_number":4,"response":{"id":"resp_1","object":"response","created_at":1700000000,"status":"completed","model":"gpt-5","output":[{"id":"fc_1","type":"function_call","status":"completed","call_id":"call_1","name":"get_menu","arguments":"{}"}]}}
""";
const string Turn2Sse = """
event: response.created
data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_2","object":"response","created_at":1700000001,"status":"in_progress","model":"gpt-5","output":[]}}
event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":1,"output_index":0,"item":{"id":"rs_1","type":"reasoning","summary":[]}}
event: response.reasoning_summary_text.delta
data: {"type":"response.reasoning_summary_text.delta","sequence_number":2,"item_id":"rs_1","output_index":0,"summary_index":0,"delta":"Thinking about the menu..."}
event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":3,"output_index":0,"item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"Thinking about the menu..."}]}}
event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":4,"output_index":1,"item":{"id":"msg_1","type":"message","status":"in_progress","role":"assistant","content":[]}}
event: response.output_text.delta
data: {"type":"response.output_text.delta","sequence_number":5,"item_id":"msg_1","output_index":1,"content_index":0,"delta":"Burger is $10."}
event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":6,"output_index":1,"item":{"id":"msg_1","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"Burger is $10.","annotations":[]}]}}
event: response.completed
data: {"type":"response.completed","sequence_number":7,"response":{"id":"resp_2","object":"response","created_at":1700000001,"status":"completed","model":"gpt-5","output":[{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"Thinking about the menu..."}]},{"id":"msg_1","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"Burger is $10.","annotations":[]}]}]}}
""";
int call = 0;
using var httpClient = new HttpClient(new FakeSseHandler(() => ++call == 1 ? Turn1Sse : Turn2Sse));
var openAiOptions = new OpenAIClientOptions { Transport = new HttpClientPipelineTransport(httpClient) };
IChatClient client = new OpenAIClient(new ApiKeyCredential("fake"), openAiOptions)
.GetResponsesClient()
.AsIChatClient("gpt-5")
.AsBuilder()
.UseFunctionInvocation()
.Build();
var options = new ChatOptions { Tools = [AIFunctionFactory.Create(() => "Burger $10", "get_menu")] };
ChatResponse response = await client.GetStreamingResponseAsync("What is on the menu?", options).ToChatResponseAsync();
for (int i = 0; i < response.Messages.Count; i++)
{
var m = response.Messages[i];
Console.WriteLine($"Message[{i}] Role={m.Role}: [{string.Join(", ", m.Contents.Select(c => c.GetType().Name))}]");
}
sealed class FakeSseHandler(Func<string> nextSse) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) =>
Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(nextSse(), Encoding.UTF8, "text/event-stream")
});
}
Expected behavior
Message[1] (Role=tool) contains only FunctionResultContent, and the second turn's TextReasoningContent (and any AIContent output items) are placed in an assistant message:
Message[0] Role=assistant: [FunctionCallContent]
Message[1] Role=tool: [FunctionResultContent]
Message[2] Role=assistant: [TextReasoningContent, TextContent]
Actual behavior
TextReasoningContent (and any default AIContent items such as Responses compaction items) from the second model call are merged into Message[1] (Role=tool):
Message[0] Role=assistant: [FunctionCallContent]
Message[1] Role=tool: [FunctionResultContent, TextReasoningContent]
Message[2] Role=assistant: [TextContent]
When OpenTelemetryChatClient wraps a multi-turn tool or agent loop (for example via UseOpenTelemetry() above UseFunctionInvocation(), or Microsoft.Agents.AI's OpenTelemetryAgent), gen_ai.output.messages records model reasoning and compaction items inside role: "tool" messages.
Regression?
No — present in Microsoft.Extensions.AI.OpenAI 10.0.0 through 11.0.0.
Known Workarounds
Wrap the inner OpenAIResponsesChatClient in a DelegatingChatClient that defaults update.Role ??= ChatRole.Assistant; on each ChatResponseUpdate yielded from GetStreamingResponseAsync.
Configuration
Microsoft.Extensions.AI:11.0.0Microsoft.Extensions.AI.OpenAI:11.0.0- .NET SDK / Runtime:
.NET 10.0(OS-independent; reproduced on Linux and Windows)
Other information
Root cause in OpenAIResponsesChatClient (v11.0.0):
lastRoleandlastMessageIdstart asnullper call, andCreateUpdateconstructs every update withnew(lastRole, ...):OpenAIResponsesChatClient.cs#L371-L386(v11.0.0)lastRoleis assigned only when aMessageResponseItemorFunctionCallResponseItemis added:OpenAIResponsesChatClient.cs#L459-L471(v11.0.0)- Reasoning deltas, encrypted reasoning items, and
defaultitems (such as compaction) arrive before any message/function-call item and are yielded withRole = null: FunctionCallOutputResponseItemin the same method already setslastRole ??= ChatRole.Assistant;:OpenAIResponsesChatClient.cs#L558-L561(v11.0.0). SettinglastRole ??= ChatRole.Assistant;for reasoning and other model output items (or initializinglastRole = ChatRole.Assistant) ensuresChatResponseExtensions.ProcessUpdate(ChatResponseExtensions.cs#L538-L562(v11.0.0)) starts a newassistantmessage after atoolmessage, including inOpenTelemetryChatClient(OpenTelemetryChatClient.cs#L267(v11.0.0)).
- Ngôn ngữ chính
- C#
- Star
- 3.3k
- Fork
- 919
- Merge trung bình
- 2 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 35
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/extensions
-
area-ai untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/extensions#7956 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ServiceEndpointWatcher logs normal cancellation as an errorCó thể đã có người làm @seragAhmed đã nhận 5 ngày trước. Đang mởbug untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
dotnet/extensions#7940 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
OpenAISpeechToTextClient sends Ogg MemoryStream audio with an .mp3 filenameCó thể đã có người làm @Manish-1kumar đã nhận 16 ngày trước. Đang mởarea-ai untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dotnet/extensions#7900 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Unable to obtain the UsageContent and FinishReason of response.incompleteCó thể đã có người làm @jaeminbird đã nhận 20 ngày trước. Đang mởarea-ai bug untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
dotnet/extensions#7893 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
AI Chat Web template: scaffolded Identity pages are inaccessible because App.razor applies an interactive render mode unconditionallyCó thể đã có người làm @valkiara đã nhận 20 ngày trước. Đang mởarea-ai-templates untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
dotnet/extensions#7889 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/extensions
Issue tương tự
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area/backend type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
umbraco/Umbraco-CMS#24668 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Pri3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
MicrosoftDocs/windows-dev-docs#6205 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
filiptrivan/spiderly#474 ·
Maintainer thường phản hồi trong vòng 2 ngày