{"record":{"id":"b364559ab78815a0","repo":"dotnet/machinelearning","slug":"only-text-content-is-supported-but-got-item-gett","errorCode":null,"errorMessage":"Only text content is supported, but got {item.GetType().Name}","messagePattern":"Only text content is supported, but got (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.GenAI.LLaMA/Llama3_1ChatTemplateBuilder.cs","lineNumber":69,"sourceCode":"        sb.Append($\"<|start_header_id|>assistant<|end_header_id|>{Newline}\");\n        var input = sb.ToString();\n\n        return input;\n    }\n\n    public string BuildPrompt(ChatHistory chatHistory)\n    {\n        // build prompt from chat history\n        var sb = new StringBuilder();\n\n        sb.Append(\"<|begin_of_text|>\");\n        foreach (var message in chatHistory)\n        {\n            foreach (var item in message.Items)\n            {\n                if (item is not TextContent textContent)\n                {\n                    throw new NotSupportedException($\"Only text content is supported, but got {item.GetType().Name}\");\n                }\n\n                var text = textContent.Text?.Trim() ?? string.Empty;\n\n                var prompt = message.Role switch\n                {\n                    _ when message.Role == AuthorRole.System => $\"<|start_header_id|>system<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}\",\n                    _ when message.Role == AuthorRole.User => $\"<|start_header_id|>user<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}\",\n                    _ when message.Role == AuthorRole.Assistant => $\"<|start_header_id|>assistant<|end_header_id|>{Newline}{text}<|eot_id|>{Newline}\",\n                    _ => throw new NotSupportedException($\"Unsupported role {message.Role}\")\n                };\n\n                sb.Append(prompt);\n            }\n        }\n\n        sb.Append($\"<|start_header_id|>assistant<|end_header_id|>{Newline}\");\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.GenAI.LLaMA/Llama3_1ChatTemplateBuilder.cs#L51-L87","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar prompt = builder.BuildPrompt(chatHistory); // history has FunctionResultContent items\n// after\nvar textOnly = chatHistory.Select(m => new ChatMessage(m.Role,\n    string.Join(\"\\n\", m.Items.OfType<TextContent>().Select(t => t.Text))));\nvar prompt = builder.BuildPrompt(textOnly);","handlingStrategy":"validation","validationCode":"if (chatHistory.Any(m => m.Items.Any(i => i is not TextContent)))\n    throw new InvalidOperationException(\"History contains non-text items; flatten before BuildPrompt.\");","typeGuard":"static bool IsTextOnly(ChatMessage m) => m.Items.All(i => i is TextContent);\nvar safe = chatHistory.Where(IsTextOnly);","tryCatchPattern":"try { prompt = builder.BuildPrompt(chatHistory); }\ncatch (NotSupportedException ex) when (ex.Message.StartsWith(\"Only text content\"))\n{ prompt = builder.BuildPrompt(FlattenToText(chatHistory)); }","preventionTips":["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."],"tags":["llama","semantic-kernel","unsupported-content","multimodal"],"backgroundTag":"unsupported-operation","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}