microsoft/semantic-kernel · error · ServiceInvalidRequestError

Image content is not supported in an assistant message.

Error message

Image content is not supported in an assistant message.

What it means

Raised while formatting an assistant-role message for Bedrock when an item is an ImageContent. Bedrock's assistant role accepts only TextContent and FunctionCallContent; an image in an assistant turn is explicitly rejected as unsupported.

Source

Thrown at python/semantic_kernel/connectors/ai/bedrock/services/model_provider/utils.py:96

        "content": contents,
    }


def _format_assistant_message(message: ChatMessageContent) -> dict[str, Any]:
    """Format an assistant message to the expected object for the client.

    Note that Guardrails and documents are currently not supported.

    Args:
        message: The assistant message.

    Returns:
        The formatted assistant message.
    """
    contents: list[Any] = []
    for item in message.items:
        if isinstance(item, ImageContent):
            raise ServiceInvalidRequestError("Image content is not supported in an assistant message.")

        if isinstance(item, TextContent):
            contents.append({"text": item.text})
        elif isinstance(item, FunctionCallContent):
            contents.append({
                "toolUse": {
                    "toolUseId": item.id,
                    "name": item.name,
                    "input": item.arguments
                    if isinstance(item.arguments, Mapping)
                    else json.loads(item.arguments or "{}"),
                }
            })
        else:
            raise ServiceInvalidRequestError(f"Unsupported content type in an assistant message: {type(item)}")

    return {
        "role": "assistant",

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Remove ImageContent items from assistant messages before sending the history to Bedrock.
  2. If the image is user-supplied, place it in a user-role message instead.
  3. If you need multimodal assistant turns, use a connector that supports them rather than Bedrock's assistant converter.

Example fix

// before
history.add_message(ChatMessageContent(role=AuthorRole.ASSISTANT, items=[ImageContent(...)]))
// after
history.add_message(ChatMessageContent(role=AuthorRole.USER, items=[ImageContent(...)]))
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.contents.image_content import ImageContent

def assistant_message_has_no_image(items) -> bool:
    return not any(isinstance(i, ImageContent) for i in items)

Type guard

from semantic_kernel.contents.image_content import ImageContent

def is_bedrock_safe_assistant_item(item: object) -> bool:
    return not isinstance(item, ImageContent)

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInvalidRequestError

try:
    await service.get_chat_message_contents(history, settings)
except ServiceInvalidRequestError as e:
    if "Image content is not supported in an assistant message" in str(e):
        strip_images_from_assistant_messages(history)
    raise

Prevention

When it happens

Trigger: A ChatHistory assistant message that contains an ImageContent item is sent to a Bedrock chat completion service, hitting the isinstance(item, ImageContent) guard at the top of _format_assistant_message.

Common situations: Streaming a multimodal model's assistant output back into Bedrock; deserializing a stored assistant message that included an image; incorrectly tagging image content with the assistant role.

Related errors


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