{"record":{"id":"11b178c6698825ee","repo":"dotnet/machinelearning","slug":"only-text-content-is-supported-but-got-item-gett-11b178","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.Phi/Phi3/Phi3ChatTemplateBuilder.cs","lineNumber":72,"sourceCode":"\n        sb.Append(\"<|assistant|>\");\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        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 prompt = message.Role switch\n                {\n                    _ when message.Role == AuthorRole.System => $\"<|system|>{Newline}{textContent}<|end|>{Newline}\",\n                    _ when message.Role == AuthorRole.User => $\"<|user|>{Newline}{textContent}<|end|>{Newline}\",\n                    _ when message.Role == AuthorRole.Assistant => $\"<|assistant|>{Newline}{textContent}<|end|>{Newline}\",\n                    _ => throw new NotSupportedException($\"Unsupported role {message.Role}\")\n                };\n\n                sb.Append(prompt);\n            }\n        }\n\n        sb.Append(\"<|assistant|>\");\n\n        return sb.ToString();\n    }","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ChatTemplateBuilder.cs#L54-L90","documentation":"The ChatMessage (AutoGen/Semantic Kernel style) overload of BuildPrompt only supports TextContent items inside each message. Any other content item type (images, function-call items, etc.) triggers NotSupportedException with the concrete type name.","triggerScenarios":"Calling BuildPrompt(IEnumerable<ChatMessage>) where any message's Items collection contains a non-TextContent item — e.g. ImageContent, FunctionCallContent, or a binary content item.","commonSituations":"Multimodal chat histories (images from vision models) passed to a text-only Phi-3 template builder; tool-call records stored as message items; histories shared with a multimodal agent then reused for Phi-3.","solutions":["Strip non-text items from message.Items before calling BuildPrompt.","Convert non-text items to text (e.g. describe the image, stringify the function call).","Use a template builder / model that supports the content types you need."],"exampleFix":"// before\nvar prompt = builder.BuildPrompt(chatHistory);\n// after\nvar textOnly = chatHistory.Select(m => new ChatMessage(m.Role, string.Join(\"\\n\", m.Items.OfType<TextContent>().Select(t => t.Text))));\nvar prompt = builder.BuildPrompt(textOnly);","handlingStrategy":"validation","validationCode":"if (chatHistory.SelectMany(m => m.Items).Any(i => i is not TextContent))\n    throw new InvalidOperationException(\"Phi-3 template builder supports text-only chat history.\");","typeGuard":"bool IsTextOnly(ChatMessage m) => m.Items.All(i => i is TextContent);","tryCatchPattern":"try { var prompt = builder.BuildPrompt(chatHistory); } catch (NotSupportedException ex) when (ex.Message.StartsWith(\"Only text content is supported\")) { chatHistory = chatHistory.Where(IsTextOnly).ToList(); prompt = builder.BuildPrompt(chatHistory); }","preventionTips":["Keep Phi-3 pipelines text-only; route multimodal content to a vision-capable model.","Flatten function-call items into text before storing them in history.","Assert Items.OfType<TextContent>().Count() == Items.Count in tests."],"tags":["csharp","llm","chat-template","multimodal"],"backgroundTag":"unsupported-content-type","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"}