microsoft/semantic-kernel · error · ValueError

Image must be encoded as base64.

Error message

Image must be encoded as base64.

What it means

Raised inside _format_assistant_message when an assistant message contains an ImageContent item whose .data attribute is None. This is functionally the same check as error 1125 but checks data is None specifically (vs falsy in the user path). The Ollama API needs base64 image data in assistant messages too. Raised as a plain ValueError.

Source

Thrown at python/semantic_kernel/connectors/ai/ollama/services/utils.py:71

    return user_message


def _format_assistant_message(message: ChatMessageContent) -> Message:
    """Format an assistant message to the expected object for the client.

    Args:
        message: The assistant message.

    Returns:
        The formatted assistant message.
    """
    assistant_message = Message(role="assistant", content=message.content)

    image_items = [item for item in message.items if isinstance(item, ImageContent)]
    if image_items:
        if any(image_item.data is None for image_item in image_items):
            raise ValueError("Image must be encoded as base64.")
        assistant_message["images"] = [image_item.data for image_item in image_items]

    tool_calls = [item for item in message.items if isinstance(item, FunctionCallContent)]
    if tool_calls:
        assistant_message["tool_calls"] = [
            {
                "function": {
                    "name": tool_call.function_name,
                    "arguments": tool_call.arguments
                    if isinstance(tool_call.arguments, Mapping)
                    else json.loads(tool_call.arguments or "{}"),
                }
            }
            for tool_call in tool_calls
        ]

    return assistant_message

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure every ImageContent in assistant messages has a non-None data field
  2. Encode the image: ImageContent(data=base64.b64encode(blob).decode())
  3. Strip image items from assistant messages if data is not available

Example fix

// before
ChatMessageContent(role=AuthorRole.ASSISTANT, items=[ImageContent(uri='file:///img.png')])
// after
ChatMessageContent(role=AuthorRole.ASSISTANT, items=[ImageContent(data=base64.b64encode(open('img.png','rb').read()).decode())])
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.contents import ImageContent

def validate_assistant_image_data(message):
    for item in message.items:
        if isinstance(item, ImageContent) and item.data is None:
            raise ValueError('Assistant message image is missing base64 data')

Type guard

from semantic_kernel.contents import ImageContent

def assistant_images_have_data(message) -> bool:
    return all(
        item.data is not None for item in message.items
        if isinstance(item, ImageContent)
    )

Try / catch

try:
    response = await chat.get_chat_message_contents(chat_history=history, settings=settings)
except ValueError as e:
    if 'encoded as base64' in str(e):
        logger.error('Assistant message has an image without data; enriching or removing it')
        raise

Prevention

When it happens

Trigger: Adding an ImageContent with data=None to an assistant-role ChatMessageContent that is being sent to Ollama. This happens when replaying or constructing assistant messages with image references but no encoded data.

Common situations: Reconstructing assistant messages from serialized history where only the URI was persisted; building test fixtures with ImageContent(uri=...) and no data; a multimodal round-trip where the data field was dropped during serialization.

Related errors


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