Skip to content
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

Address lingering deviation in null content serialization for deprecated FunctionChatMessage #350

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .dotnet/src/Custom/Chat/Messages/FunctionChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public FunctionChatMessage(string functionName, string content)
Argument.AssertNotNull(functionName, nameof(functionName));

FunctionName = functionName;

// FunctionChatMessage treats content as *required* but explicitly *nullable*. If null content was provided,
// enforce manifestation of the (nullable) content collection via .Clear().
if (!Content.IsInnerCollectionDefined())
{
Content.Clear();
}
}

// CUSTOM: Renamed.
Expand Down
19 changes: 19 additions & 0 deletions .dotnet/tests/Chat/ChatSmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,25 @@
}
}

[Test]
public void AssistantAndFunctionMessagesHandleNoContentCorrectly()
{
// AssistantChatMessage and FunctionChatMessage can both exist without content, but follow different rules:
// - AssistantChatMessage treats content as optional, as valid assistant message variants (e.g. for tool calls)
// - FunctionChatMessage meanwhile treats content as required and nullable.
// This test validates that no-content assistant messages just don't serialize content, while no-content
// function messages serialize content with an explicit null value.

ChatToolCall fakeToolCall = ChatToolCall.CreateFunctionToolCall("call_abcd1234", "function_name", functionArguments: BinaryData.FromString("{}"));
AssistantChatMessage assistantChatMessage = new([fakeToolCall]);
string serializedAssistantChatMessage = ModelReaderWriter.Write(assistantChatMessage).ToString();
Assert.That(serializedAssistantChatMessage, Does.Not.Contain("content"));

FunctionChatMessage functionChatMessage = new("function_name", null);

Check warning on line 846 in .dotnet/tests/Chat/ChatSmokeTests.cs

View workflow job for this annotation

GitHub Actions / build

'FunctionChatMessage' is obsolete: 'This class is obsolete. Please use ToolChatMessage instead.'

Check warning on line 846 in .dotnet/tests/Chat/ChatSmokeTests.cs

View workflow job for this annotation

GitHub Actions / build

'FunctionChatMessage' is obsolete: 'This class is obsolete. Please use ToolChatMessage instead.'
string serializedFunctionChatMessage = ModelReaderWriter.Write(functionChatMessage).ToString();
Assert.That(serializedFunctionChatMessage, Does.Contain(@"""content"":null"));
}

#pragma warning disable CS0618
[Test]
public void SerializeMessagesWithNullProperties()
Expand Down