dotnet/machinelearning · error · NotSupportedException
Only text content is supported, but got {item.GetType().Name
Error message
Only text content is supported, but got {item.GetType().Name} What it means
This BuildPrompt overload works on ChatMessage (Semantic Kernel style) and only renders TextContent items into the LLaMA prompt. It throws NotSupportedException when a message item is any other content type (function call/result, image, binary), since the plain-text LLaMA template cannot represent them.
Source
Thrown at src/Microsoft.ML.GenAI.LLaMA/Llama3_1ChatTemplateBuilder.cs:69
sb.Append($"<|start_header_id|>assistant<|end_header_id|>{Newline}");
var input = sb.ToString();
return input;
}
public string BuildPrompt(ChatHistory chatHistory)
{
// build prompt from chat history
var sb = new StringBuilder();
sb.Append("<|begin_of_text|>");
foreach (var message in chatHistory)
{
foreach (var item in message.Items)
{
if (item is not TextContent textContent)
{
throw new NotSupportedException($"Only text content is supported, but got {item.GetType().Name}");
}
var text = textContent.Text?.Trim() ?? string.Empty;
var prompt = message.Role switch
{
_ when message.Role == AuthorRole.System => $"<|start_header_id|>system<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}",
_ when message.Role == AuthorRole.User => $"<|start_header_id|>user<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}",
_ when message.Role == AuthorRole.Assistant => $"<|start_header_id|>assistant<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}",
_ => throw new NotSupportedException($"Unsupported role {message.Role}")
};
sb.Append(prompt);
}
}
sb.Append($"<|start_header_id|>assistant<|end_header_id|>{Newline}");
View on GitHub (pinned to 7b76e69cf9)
Solutions
- Flatten function-call/result items to text messages before calling BuildPrompt.
- Disable automatic function invocation (FunctionChoiceBehavior.None) or remove tool items from history.
- Filter messages whose Items contain non-TextContent entries.
- Serialize non-text items to a textual form (e.g. JSON of arguments/results) if they must be included.
Example fix
// before
var prompt = builder.BuildPrompt(chatHistory); // history has FunctionResultContent items
// after
var textOnly = chatHistory.Select(m => new ChatMessage(m.Role,
string.Join("\n", m.Items.OfType<TextContent>().Select(t => t.Text))));
var prompt = builder.BuildPrompt(textOnly); Defensive patterns
Strategy: validation
Validate before calling
if (chatHistory.Any(m => m.Items.Any(i => i is not TextContent)))
throw new InvalidOperationException("History contains non-text items; flatten before BuildPrompt."); Type guard
static bool IsTextOnly(ChatMessage m) => m.Items.All(i => i is TextContent); var safe = chatHistory.Where(IsTextOnly);
Try / catch
try { prompt = builder.BuildPrompt(chatHistory); }
catch (NotSupportedException ex) when (ex.Message.StartsWith("Only text content"))
{ prompt = builder.BuildPrompt(FlattenToText(chatHistory)); } Prevention
- Disable automatic function invocation or strip FunctionCall/Result content before prompt building.
- Flatten multimodal histories to text for text-only models.
- Add a history-normalization step between the chat pipeline and the template builder.
When it happens
Trigger: Calling BuildPrompt(IEnumerable<ChatMessage>, ChatOptions?, bool) when any message's Items collection contains non-text content — e.g. FunctionCallContent, FunctionResultContent, or image content added by Semantic Kernel's automatic function calling.
Common situations: Enabling auto function invocation so tool-call/result items appear in chat history; multimodal messages fed to a text-only LLaMA pipeline; chat histories persisted with rich content items.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Unsupported role {message.Role}
- Please provide a message with content.
- Please provide a message with a valid role. The valid roles
- Invalid role.
- Failed to generate a reply.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/b364559ab78815a0.
Report an issue: GitHub.