-
Notifications
You must be signed in to change notification settings - Fork 4.6k
.Net: Fix OpenAiResponseAgent chat message images removed by SK #13019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markwallace-microsoft
merged 1 commit into
microsoft:main
from
markwallace-microsoft:users/markwallace/issue_12888
Aug 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using OpenAI.Responses; | ||
|
|
||
| namespace Microsoft.SemanticKernel.Agents.OpenAI; | ||
|
|
||
| /// <summary> | ||
| /// Extensons methods for <see cref="KernelContent"/>. | ||
| /// </summary> | ||
| internal static class KernelContentExtensions | ||
| { | ||
| internal static ResponseContentPart ToResponseContentPart(this KernelContent content) | ||
| { | ||
| return content switch | ||
| { | ||
| TextContent textContent => textContent.ToResponseContentPart(), | ||
| ImageContent imageContent => imageContent.ToResponseContentPart(), | ||
| BinaryContent binaryContent => binaryContent.ToResponseContentPart(), | ||
| FileReferenceContent fileReferenceContent => fileReferenceContent.ToResponseContentPart(), | ||
| _ => throw new NotSupportedException($"Unsupported content type {content.GetType().Name}. Cannot convert to {nameof(ResponseContentPart)}.") | ||
| }; | ||
| } | ||
|
|
||
| internal static ResponseContentPart ToResponseContentPart(this TextContent content) | ||
| { | ||
| return ResponseContentPart.CreateInputTextPart(content.Text); | ||
| } | ||
|
|
||
| internal static ResponseContentPart ToResponseContentPart(this ImageContent content) | ||
| { | ||
| return content.Uri is not null | ||
| ? ResponseContentPart.CreateInputImagePart(content.Uri) | ||
| : content.Data is not null | ||
| ? ResponseContentPart.CreateInputImagePart(new BinaryData(content.Data), content.MimeType) | ||
| : throw new NotSupportedException("ImageContent cannot be converted to ResponseContentPart. Only ImageContent with a uri or binary data is supported."); | ||
| } | ||
|
|
||
| internal static ResponseContentPart ToResponseContentPart(this BinaryContent content) | ||
| { | ||
| return content.Data is not null | ||
| ? ResponseContentPart.CreateInputFilePart(new BinaryData(content.Data), content.MimeType, Guid.NewGuid().ToString()) | ||
| : throw new NotSupportedException("AudioContent cannot be converted to ResponseContentPart. Only AudioContent with binary data is supported."); | ||
| } | ||
|
|
||
| internal static ResponseContentPart ToResponseContentPart(this FileReferenceContent content) | ||
| { | ||
| return content.FileId is not null | ||
| ? ResponseContentPart.CreateInputFilePart(content.FileId) | ||
| : throw new NotSupportedException("FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported."); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
dotnet/src/Agents/UnitTests/OpenAI/Extensions/ChatContentMessageExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.Agents.OpenAI; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using OpenAI.Responses; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Agents.UnitTests.OpenAI.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for ChatContentMessageExtensions | ||
| /// </summary> | ||
| public class ChatContentMessageExtensionsTests | ||
| { | ||
| [Theory] | ||
| [InlineData("User")] | ||
| [InlineData("Assistant")] | ||
| [InlineData("System")] | ||
| public void VerifyToResponseItemWithUserChatMessageContent(string roleLabel) | ||
| { | ||
| // Arrange | ||
| var role = new AuthorRole(roleLabel); | ||
| var content = new ChatMessageContent( | ||
| role, | ||
| items: [ | ||
| new TextContent("What is in this image?"), | ||
| new ImageContent(new Uri("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg")), | ||
| new BinaryContent(new ReadOnlyMemory<byte>([0x52, 0x49, 0x46, 0x46, 0x24, 0x08, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45]), "audio/wav"), | ||
| new FileReferenceContent("file-abc123") | ||
| ] | ||
| ); | ||
|
|
||
| // Act | ||
| var responseItem = content.ToResponseItem(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(responseItem); | ||
| Assert.IsType<MessageResponseItem>(responseItem, exactMatch: false); | ||
| var messageResponseItem = responseItem as MessageResponseItem; | ||
| Assert.NotNull(messageResponseItem); | ||
| Assert.Equal(role.Label.ToUpperInvariant(), messageResponseItem.Role.ToString().ToUpperInvariant()); | ||
| Assert.Equal(4, messageResponseItem.Content.Count); | ||
|
|
||
| // Validate TextContent conversion - should create InputText part | ||
| var textContent = messageResponseItem.Content.FirstOrDefault(p => p.Kind == ResponseContentPartKind.InputText); | ||
| Assert.NotNull(textContent); | ||
| //Assert.IsType<>(textContent); | ||
| Assert.Equal("What is in this image?", textContent.Text); | ||
|
|
||
| // Validate ImageContent conversion - should create InputImage part | ||
| var imageContent = messageResponseItem.Content.FirstOrDefault(p => p.Kind == ResponseContentPartKind.InputImage); | ||
| Assert.NotNull(imageContent); | ||
|
|
||
| // Validate BinaryContent conversion - should create InputFile part | ||
| var binaryContent = messageResponseItem.Content.FirstOrDefault(p => p.Kind == ResponseContentPartKind.InputFile && p.InputFileBytes is not null); | ||
| Assert.NotNull(binaryContent); | ||
| Assert.Equal("audio/wav", binaryContent.InputFileBytesMediaType); | ||
|
|
||
| // Validate FileReferenceContent conversion - should create InputImage part | ||
| var fileContent = messageResponseItem.Content.FirstOrDefault(p => p.Kind == ResponseContentPartKind.InputFile && p.InputFileId is not null); | ||
| Assert.NotNull(fileContent); | ||
| Assert.Equal("file-abc123", fileContent.InputFileId); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.