microsoft/semantic-kernel · error · ServiceInvalidRequestError

Unsupported item type in User message while formatting chat

Error message

Unsupported item type in User message while formatting chat history for Google AI Inference: {type(item)}

What it means

Raised by format_user_message when a ChatMessageContent item in a user-role message is neither a TextContent nor an ImageContent. The Google AI Gemini API user-message formatter only handles text and inline images; any other item type (e.g. FunctionResultContent, BinaryContent, custom content subclasses) is unsupported and the request cannot be serialized for the API.

Source

Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/utils.py:72


def format_user_message(message: ChatMessageContent) -> list[Part]:
    """Format a user message to the expected object for the client.

    Args:
        message: The user message.

    Returns:
        The formatted user message as a list of parts.
    """
    parts: list[Part] = []
    for item in message.items:
        if isinstance(item, TextContent):
            parts.append(Part.from_text(text=item.text))
        elif isinstance(item, ImageContent):
            parts.append(_create_image_part(item))
        else:
            raise ServiceInvalidRequestError(
                "Unsupported item type in User message while formatting chat history for Google AI"
                f" Inference: {type(item)}"
            )

    return parts


def format_assistant_message(message: ChatMessageContent) -> list[Part]:
    """Format an assistant message to the expected object for the client.

    Args:
        message: The assistant message.

    Returns:
        The formatted assistant message as a list of parts.
    """
    parts: list[Part] = []
    for item in message.items:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure every item in a user message is a TextContent or ImageContent before sending to Google AI.
  2. Convert or serialize unsupported items to text (e.g. str(result)) before adding them to user-message items.
  3. Filter chat history to strip or transform unsupported item types prior to the API call.

Example fix

# before
history.add_user_message(items=[
    TextContent(text='describe this'),
    BinaryContent(data=b'...', mime_type='audio/wav'),  # unsupported
])

# after
history.add_user_message(items=[
    TextContent(text='describe this'),
    ImageContent(data=image_bytes, mime_type='image/png', data_uri='inline'),
])
Defensive patterns

Strategy: type-guard

Validate before calling

from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.image_content import ImageContent

def validate_user_items(message):
    for item in message.items:
        if not isinstance(item, (TextContent, ImageContent)):
            raise TypeError(f'Unsupported user item type: {type(item).__name__}')

Type guard

from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.image_content import ImageContent

def is_valid_user_item(item) -> bool:
    return isinstance(item, (TextContent, ImageContent))

Prevention

When it happens

Trigger: Adding a user-role ChatMessageContent with an items list containing a type other than TextContent/ImageContent (e.g. a FunctionResultContent, a BinaryContent, or a custom content type) and then calling a chat completion method.

Common situations: Manually constructing chat history with unsupported item types; piping output from another connector that produces item types Google AI doesn't understand; adding binary/audio content as a user item.

Related errors


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