microsoft/semantic-kernel · error · NotSupportedException

Unsupported content type. {item.GetType().Name} is not suppo

Error message

Unsupported content type. {item.GetType().Name} is not supported by Gemini.

What it means

Thrown by GeminiRequest.GetGeminiPartFromKernelContent when a KernelContent item in the collection is not one of the four recognized types: TextContent, ImageContent, AudioContent, or BinaryContent. The switch expression exhausts these four cases and falls to the default throw for any other KernelContent subclass.

Source

Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:283

                // Apply signature to last text part
                var lastTextPart = parts.LastOrDefault(p => p.Text is not null);
                if (lastTextPart is not null)
                {
                    lastTextPart.ThoughtSignature = signature;
                }
            }
        }

        return parts;
    }

    private static GeminiPart GetGeminiPartFromKernelContent(KernelContent item) => item switch
    {
        TextContent textContent => new GeminiPart { Text = textContent.Text },
        ImageContent imageContent => CreateGeminiPartFromImage(imageContent),
        AudioContent audioContent => CreateGeminiPartFromAudio(audioContent),
        BinaryContent binaryContent => CreateGeminiPartFromBinary(binaryContent),
        _ => throw new NotSupportedException($"Unsupported content type. {item.GetType().Name} is not supported by Gemini.")
    };

    private static GeminiPart CreateGeminiPartFromImage(ImageContent imageContent)
    {
        // Binary data takes precedence over URI as per the ImageContent.ToString() implementation.
        if (imageContent.Data is { IsEmpty: false })
        {
            return new GeminiPart
            {
                InlineData = new GeminiPart.InlineDataPart
                {
                    MimeType = GetMimeTypeFromImageContent(imageContent),
                    InlineData = Convert.ToBase64String(imageContent.Data.Value.ToArray())
                }
            };
        }

        if (imageContent.Uri is not null)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Filter chat history to only include TextContent, ImageContent, AudioContent, or BinaryContent before passing to the Gemini connector.
  2. Convert custom content types to TextContent or another supported type before the call.
  3. Upgrade the Connectors.Google package if a newer version supports the content type you need.
  4. If using function calling, ensure FunctionCallContent/FunctionResultContent are handled through the tool-call pipeline, not manually added to history.

Example fix

// before — custom content type in history
history.Items.Add(new MyCustomContent());
await client.GetChatMessageContentsAsync(history);

// after — filter to supported types
var supported = history.Where(m => m.Items.All(i =>
    i is TextContent or ImageContent or AudioContent or BinaryContent)).ToList();
await client.GetChatMessageContentsAsync(supported);
Defensive patterns

Strategy: type-guard

Validate before calling

var supportedTypes = new HashSet<Type>
{
    typeof(TextContent), typeof(ImageContent), typeof(AudioContent), typeof(BinaryContent)
};
var unsupported = history.SelectMany(m => m.Items)
    .Where(item => !supportedTypes.Contains(item.GetType()))
    .ToList();
if (unsupported.Count > 0)
{
    throw new ArgumentException(
        $"Unsupported content types for Gemini: {string.Join(", ", unsupported.Select(i => i.GetType().Name))}");
}

Type guard

static bool IsSupportedGeminiContent(KernelContent content) =>
    content is TextContent or ImageContent or AudioContent or BinaryContent;

Try / catch

try { await client.GetChatMessageContentsAsync(history, settings, ct); }
catch (NotSupportedException ex) when (ex.Message.Contains("Unsupported content type"))
{
    // Filter to supported types and retry
    foreach (var msg in history)
        msg.Items = msg.Items.Where(IsSupportedGeminiContent).ToList();
    await client.GetChatMessageContentsAsync(history, settings, ct);
}

Prevention

When it happens

Trigger: Passing a chat history or content collection containing a custom KernelContent subclass, or a built-in type not yet mapped (e.g. FunctionCallContent, FunctionResultContent, or a future SK content type). The conversion happens when SK builds the Gemini request from the kernel content.

Common situations: Adding a custom KernelContent subclass for application-specific metadata. Using FunctionCallContent or FunctionResultContent directly in chat history (these are typically handled through tool-call paths, not content conversion). Newer SK versions introduce content types the connector does not yet support.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/06ee0263af6bf985. Report an issue: GitHub.