Skip to content
Merged
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
53 changes: 53 additions & 0 deletions src/Custom/Chat/ChatMessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace OpenAI.Chat;

Expand Down Expand Up @@ -36,4 +37,56 @@ internal bool IsInnerCollectionDefined()
{
return !(Items is ChangeTrackingList<ChatMessageContentPart> changeTrackingList && changeTrackingList.IsUndefined);
}

/// <summary>
/// Returns a string representation of the chat message content by iterating through all content parts.
/// </summary>
/// <returns>A formatted string representation of all content parts.</returns>
public override string ToString()
{
StringBuilder builder = new();

if (Count == 0)
{
return "<empty content>";
}

for (int i = 0; i < Count; i++)
{
if (i > 0) {
builder.AppendLine();
}

var part = this[i];
switch (part.Kind)
{
case ChatMessageContentPartKind.Text:
builder.Append(part.Text);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can potentially return an empty string, which is against the ToString guidelines.

break;

case ChatMessageContentPartKind.Image:
builder.Append("<image>");
break;

case ChatMessageContentPartKind.InputAudio:
builder.Append("<audio>");
break;

case ChatMessageContentPartKind.File:
builder.Append($"<file: {part.Filename}>");
break;

case ChatMessageContentPartKind.Refusal:
var refusal = part.Refusal;
builder.Append($"<refusal: {refusal}>");
break;

default:
builder.Append("<unknown content kind>");
Copy link
Collaborator

@joseharriaga joseharriaga Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This should say "<unknown content part kind>" or simply "<unknown>"

break;
}
}

return builder.ToString();
}
}