Repository navigation
Description
Description
MicrosoftExtensionsAIChatExtensions.AsOpenAIStreamingChatCompletionUpdatesAsync sets finish_reason to "stop" on every chunk whose ChatResponseUpdate.FinishReason is null. In a stream, every chunk before the last one has no finish reason, so each content chunk is serialized as if the response had already finished.
A client that reads the stream with the OpenAI SDK, or any client that ends the turn on the first non-null finish_reason, stops after the first chunk. A response cut off by the token limit also reports "stop" on its content chunks before the terminal "length" chunk.
The cause is ToChatFinishReason, which maps null to ChatFinishReason.Stop through its default arm:
extensions/src/Libraries/Microsoft.Extensions.AI.OpenAI/MicrosoftExtensionsAIChatExtensions.cs
Lines 293 to 302 in 431c048
| /// <summary>Converts the <see cref="Microsoft.Extensions.AI.ChatFinishReason"/> to a <see cref="ChatFinishReason"/>.</summary> | |
| private static ChatFinishReason ToChatFinishReason(Microsoft.Extensions.AI.ChatFinishReason? finishReason) => | |
| finishReason?.Value switch | |
| { | |
| "length" => ChatFinishReason.Length, | |
| "content_filter" => ChatFinishReason.ContentFilter, | |
| "tool_calls" => ChatFinishReason.ToolCalls, | |
| "function_call" => ChatFinishReason.FunctionCall, | |
| _ => ChatFinishReason.Stop, | |
| }; |
Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>OPENAI001</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.11.0" />
</ItemGroup>
</Project>
using System.ClientModel.Primitives;
using Microsoft.Extensions.AI;
using OpenAI.Chat;
static async IAsyncEnumerable<ChatResponseUpdate> Updates()
{
// A content chunk: no finish reason yet.
yield return new ChatResponseUpdate(ChatRole.Assistant, "Hello") { ResponseId = "r1", ModelId = "m" };
// The terminal chunk: the model stopped at the token limit.
yield return new ChatResponseUpdate { ResponseId = "r1", ModelId = "m", FinishReason = Microsoft.Extensions.AI.ChatFinishReason.Length };
await Task.CompletedTask;
}
await foreach (var chunk in Updates().AsOpenAIStreamingChatCompletionUpdatesAsync())
{
Console.WriteLine(ModelReaderWriter.Write(chunk).ToString());
}
Expected behavior
The first chunk has no finish reason ("finish_reason": null, or the property is absent). Only the terminal chunk has "finish_reason": "length".
Actual behavior
{"id":"r1","choices":[{"delta":{"content":"Hello","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":-62135596800,"model":"m","object":"chat.completion.chunk"}
{"id":"r1","choices":[{"delta":{"content":"","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"length","index":0}],"created":-62135596800,"model":"m","object":"chat.completion.chunk"}
The content chunk reports "finish_reason":"stop".
Regression?
Not known. The default arm has this behavior on main at the commit linked above.
Known Workarounds
Write the chunks by hand instead of using the converter. Alternatively, keep the StreamingChatCompletionUpdate as RawRepresentation, which the converter passes through unchanged. That works only when the updates come from OpenAIChatClient.
Configuration
- Microsoft.Extensions.AI.OpenAI 10.11.0, OpenAI 2.x (resolved transitively)
- .NET SDK 10.0.402, macOS 27.0.1
- The behavior does not depend on the platform. It comes from the mapping code.
Other information
A possible fix: let ToChatFinishReason return ChatFinishReason? and map null to null on the streaming path. OpenAIChatModelFactory.StreamingChatCompletionUpdate already accepts a nullable finishReason; with null, the chunk is written without a finish_reason. The non-streaming AsOpenAIChatCompletion can keep its Stop default, because a complete response always has a finish reason.
A related detail on the same line: createdAt: update.CreatedAt ?? default serializes as "created":-62135596800 when CreatedAt is null.
Activity