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 Vertex AI Inference: {type(item)}

What it means

Raised by format_user_message() in the Vertex AI connector when it iterates a user message's items and encounters a content item that is neither TextContent nor ImageContent. The Vertex AI Inference API only accepts text and inline-image parts in a user role, so any other content type is rejected before the request is sent. The message embeds the offending item's Python type so you can see exactly what was unsupported.

Source

Thrown at python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py:70


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(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 Vertex 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. Inspect the {type(item)} in the error and ensure user-role messages only contain TextContent (and optionally ImageContent) items.
  2. Move FunctionCallContent into an assistant-role message and FunctionResultContent into a tool-role message before calling the Vertex AI service.
  3. Strip or convert unsupported items (e.g. serialize BinaryContent to text) before adding the message to ChatHistory.
  4. If you need a content type Vertex AI doesn't support, build the message via a role the Vertex AI formatter handles (tool/assistant) or pre-flatten the item to TextContent.

Example fix

// before
history.add_user_message(items=[FunctionResultContent(id=..., result=...), TextContent(text='hi')])
# after
history.add_message(role=AuthorRole.TOOL, items=[FunctionResultContent(id=..., result=...)])
history.add_user_message(text='hi')
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.contents import TextContent, ImageContent
SUPPORTED = (TextContent, ImageContent)
bad = [type(i) for i in user_msg.items if not isinstance(i, SUPPORTED)]
assert not bad, f'Unsupported user-role items: {bad}'

Type guard

def is_valid_vertex_user_message(msg) -> bool:
    return all(isinstance(i, (TextContent, ImageContent)) for i in msg.items)

Prevention

When it happens

Trigger: Calling VertexAIChatCompletion with a ChatHistory whose user message contains an item other than TextContent/ImageContent (e.g. FunctionCallContent, FunctionResultContent, BinaryContent, AudioContent, or a custom ChatMessageContent item). Happens when reusing a chat history built for another connector, or when appending a tool result into a user-role message.

Common situations: Porting a chat history authored for OpenAI/Azure (which tolerates mixed content) over to Vertex AI. Mixing tool-call round-trips into user messages instead of assistant/tool roles. Constructing ChatMessageContent with a generic item list rather than typed TextContent.

Related errors


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