microsoft/semantic-kernel · error · ServiceInvalidRequestError

Unsupported item type in Assistant message while formatting

Error message

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

What it means

Raised by format_assistant_message when a ChatMessageContent item in an assistant-role message is not a TextContent, FunctionCallContent, or ImageContent. The Google AI assistant-message formatter serializes text, function calls (tool invocations), and images only. Any other item type (e.g. FunctionResultContent, BinaryContent) in an assistant message is unsupported.

Source

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

                    Part(
                        function_call={
                            "name": item.name,  # type: ignore[arg-type]
                            "args": json.loads(item.arguments) if isinstance(item.arguments, str) else item.arguments,
                        },
                        thought_signature=thought_signature,
                    )
                )
            else:
                parts.append(
                    Part.from_function_call(
                        name=item.name,  # type: ignore[arg-type]
                        args=json.loads(item.arguments) if isinstance(item.arguments, str) else item.arguments,  # type: ignore[arg-type]
                    )
                )
        elif isinstance(item, ImageContent):
            parts.append(_create_image_part(item))
        else:
            raise ServiceInvalidRequestError(
                "Unsupported item type in Assistant message while formatting chat history for Google AI"
                f" Inference: {type(item)}"
            )

    return parts


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

    Args:
        message: The tool message.

    Returns:
        The formatted tool message.
    """
    parts: list[Part] = []
    for item in message.items:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure assistant messages contain only TextContent, FunctionCallContent, or ImageContent.
  2. Move FunctionResultContent items into tool-role messages (add_message(role=AuthorRole.TOOL, ...)) rather than assistant messages.
  3. Pre-validate assistant-message items before sending to the Google AI service.

Example fix

# before
history.add_message(
    role=AuthorRole.ASSISTANT,
    items=[TextContent(text='calling tool'), FunctionResultContent(id='1', name='search', result='{...}')],
)

# after
history.add_message(role=AuthorRole.ASSISTANT, items=[TextContent(text='calling tool')])
history.add_message(role=AuthorRole.TOOL, items=[FunctionResultContent(id='1', name='search', result='{...}')])
Defensive patterns

Strategy: type-guard

Validate before calling

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

ALLOWED = (TextContent, ImageContent, FunctionCallContent)
def validate_assistant_items(message):
    for item in message.items:
        if not isinstance(item, ALLOWED):
            raise TypeError(f'Unsupported assistant item type: {type(item).__name__}')

Type guard

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

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

Prevention

When it happens

Trigger: An assistant-role ChatMessageContent whose items include a type outside {TextContent, FunctionCallContent, ImageContent}, then calling a chat completion method that formats chat history.

Common situations: Chat history replay where assistant messages were populated with FunctionResultContent instead of putting results in tool-role messages; cross-connector history migration; custom content subclasses in assistant items.

Related errors


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