{"record":{"id":"15b1ca7f4c6223a6","repo":"microsoft/semantic-kernel","slug":"image-must-be-encoded-as-base64","errorCode":null,"errorMessage":"Image must be encoded as base64.","messagePattern":"Image must be encoded as base64\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/ollama/services/utils.py","lineNumber":71,"sourceCode":"\n    return user_message\n\n\ndef _format_assistant_message(message: ChatMessageContent) -> Message:\n    \"\"\"Format an assistant message to the expected object for the client.\n\n    Args:\n        message: The assistant message.\n\n    Returns:\n        The formatted assistant message.\n    \"\"\"\n    assistant_message = Message(role=\"assistant\", content=message.content)\n\n    image_items = [item for item in message.items if isinstance(item, ImageContent)]\n    if image_items:\n        if any(image_item.data is None for image_item in image_items):\n            raise ValueError(\"Image must be encoded as base64.\")\n        assistant_message[\"images\"] = [image_item.data for image_item in image_items]\n\n    tool_calls = [item for item in message.items if isinstance(item, FunctionCallContent)]\n    if tool_calls:\n        assistant_message[\"tool_calls\"] = [\n            {\n                \"function\": {\n                    \"name\": tool_call.function_name,\n                    \"arguments\": tool_call.arguments\n                    if isinstance(tool_call.arguments, Mapping)\n                    else json.loads(tool_call.arguments or \"{}\"),\n                }\n            }\n            for tool_call in tool_calls\n        ]\n\n    return assistant_message\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/ollama/services/utils.py#L53-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every ImageContent in assistant messages has a non-None data field","Encode the image: ImageContent(data=base64.b64encode(blob).decode())","Strip image items from assistant messages if data is not available"],"exampleFix":"// before\nChatMessageContent(role=AuthorRole.ASSISTANT, items=[ImageContent(uri='file:///img.png')])\n// after\nChatMessageContent(role=AuthorRole.ASSISTANT, items=[ImageContent(data=base64.b64encode(open('img.png','rb').read()).decode())])","handlingStrategy":"validation","validationCode":"from semantic_kernel.contents import ImageContent\n\ndef validate_assistant_image_data(message):\n    for item in message.items:\n        if isinstance(item, ImageContent) and item.data is None:\n            raise ValueError('Assistant message image is missing base64 data')","typeGuard":"from semantic_kernel.contents import ImageContent\n\ndef assistant_images_have_data(message) -> bool:\n    return all(\n        item.data is not None for item in message.items\n        if isinstance(item, ImageContent)\n    )","tryCatchPattern":"try:\n    response = await chat.get_chat_message_contents(chat_history=history, settings=settings)\nexcept ValueError as e:\n    if 'encoded as base64' in str(e):\n        logger.error('Assistant message has an image without data; enriching or removing it')\n        raise","preventionTips":["When reconstructing assistant messages from history, ensure image data is re-attached","Strip image items from assistant messages if the original data is not available","Run a pre-flight check on all messages before sending to Ollama"],"tags":["ollama","image-content","base64","message-formatting"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}