Skip to content

Commit cc3ab6a

Browse files
authored
Fix propagation of ChatThreadId in ToChatResponse{Async} (#5899)
1 parent 2f973c9 commit cc3ab6a

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseUpdateExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ static async Task<ChatResponse> ToChatResponseAsync(
8989
/// <param name="response">The <see cref="ChatResponse"/> object whose properties should be updated based on <paramref name="update"/>.</param>
9090
private static void ProcessUpdate(ChatResponseUpdate update, Dictionary<int, ChatMessage> messages, ChatResponse response)
9191
{
92-
response.ResponseId ??= update.ResponseId;
92+
response.ChatThreadId ??= update.ChatThreadId;
9393
response.CreatedAt ??= update.CreatedAt;
9494
response.FinishReason ??= update.FinishReason;
9595
response.ModelId ??= update.ModelId;
96+
response.ResponseId ??= update.ResponseId;
9697

9798
#if NET
9899
ChatMessage message = CollectionsMarshal.GetValueRefOrAddDefault(messages, update.ChoiceIndex, out _) ??=

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting.Azure/Storage/AzureStorageResultStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private static string GetLastSegmentFromPath(string name)
187187
=> name.Substring(name.LastIndexOf('/') + 1);
188188

189189
private static string StripExtension(string name)
190-
=> name.Substring(0, name.LastIndexOf(".", StringComparison.Ordinal));
190+
=> name.Substring(0, name.LastIndexOf('.'));
191191

192192
private static (string path, bool isDir) GetResultPath(
193193
string? executionName = null,

src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaChatClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ private IEnumerable<OllamaChatRequestMessage> ToOllamaChatRequestMessages(ChatMe
426426
currentTextMessage = new OllamaChatRequestMessage
427427
{
428428
Role = content.Role.Value,
429-
Content = textContent.Text ?? string.Empty,
429+
Content = textContent.Text,
430430
};
431431
break;
432432

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIAssistantClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ strictObj is bool strictValue ?
232232
// Store the tool mode.
233233
switch (options.ToolMode)
234234
{
235+
case NoneChatToolMode:
236+
runOptions.ToolConstraint = ToolConstraint.None;
237+
break;
238+
239+
case null:
235240
case AutoChatToolMode:
236241
runOptions.ToolConstraint = ToolConstraint.Auto;
237242
break;

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task ToChatResponse_SuccessfullyCreatesResponse(bool useAsync, bool
4242
new() { ChoiceIndex = 1, Text = "Hey", ResponseId = "12345", CreatedAt = new DateTimeOffset(1, 2, 3, 4, 5, 6, TimeSpan.Zero), ModelId = "model124" },
4343

4444
new() { ChoiceIndex = 0, Text = ", ", AuthorName = "Someone", Role = ChatRole.User, AdditionalProperties = new() { ["a"] = "b" } },
45-
new() { ChoiceIndex = 1, Text = ", ", AuthorName = "Else", Role = ChatRole.System, AdditionalProperties = new() { ["g"] = "h" } },
45+
new() { ChoiceIndex = 1, Text = ", ", AuthorName = "Else", Role = ChatRole.System, ChatThreadId = "123", AdditionalProperties = new() { ["g"] = "h" } },
4646

4747
new() { ChoiceIndex = 0, Text = "world!", CreatedAt = new DateTimeOffset(2, 2, 3, 4, 5, 6, TimeSpan.Zero), AdditionalProperties = new() { ["c"] = "d" } },
4848
new() { ChoiceIndex = 1, Text = "you!", Role = ChatRole.Tool, CreatedAt = new DateTimeOffset(3, 2, 3, 4, 5, 6, TimeSpan.Zero), AdditionalProperties = new() { ["e"] = "f", ["i"] = 42 } },
@@ -69,6 +69,8 @@ public async Task ToChatResponse_SuccessfullyCreatesResponse(bool useAsync, bool
6969
Assert.Equal(new DateTimeOffset(1, 2, 3, 4, 5, 6, TimeSpan.Zero), response.CreatedAt);
7070
Assert.Equal("model123", response.ModelId);
7171

72+
Assert.Equal("123", response.ChatThreadId);
73+
7274
Assert.Equal(3, response.Choices.Count);
7375

7476
ChatMessage message = response.Choices[0];

0 commit comments

Comments
 (0)