{"record":{"id":"94e41cea4ee853e2","repo":"microsoft/semantic-kernel","slug":"image-item-must-contain-data-encoded-as-base64","errorCode":null,"errorMessage":"Image item must contain data encoded as base64.","messagePattern":"Image item must contain data encoded as base64\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/ollama/services/utils.py","lineNumber":51,"sourceCode":"\ndef _format_user_message(message: ChatMessageContent) -> Message:\n    \"\"\"Format a user message to the expected object for the client.\n\n    Args:\n        message: The user message.\n\n    Returns:\n        The formatted user message.\n    \"\"\"\n    if not any(isinstance(item, (ImageContent)) for item in message.items):\n        return Message(role=\"user\", content=message.content)\n\n    user_message = Message(role=\"user\", content=message.content)\n\n    image_items = [item for item in message.items if isinstance(item, ImageContent)]\n    if image_items:\n        if any(not image_item.data for image_item in image_items):\n            raise ValueError(\"Image item must contain data encoded as base64.\")\n        user_message[\"images\"] = [image_item.data for image_item in image_items]\n\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:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/ollama/services/utils.py#L33-L69","documentation":"Raised inside _format_user_message when a user message contains an ImageContent item whose .data attribute is falsy (None or empty string). The Ollama API requires base64-encoded image data for inline images; the formatter checks all image items and rejects the request if any lack data. Note this is a plain ValueError, not a ServiceException.","triggerScenarios":"Adding an ImageContent to a user message without base64 data — e.g. ImageContent(uri='file:///img.png') with no data field, or ImageContent() with data left as None. Triggered when the message is formatted for an Ollama chat or text request.","commonSituations":"Creating ImageContent from a URI but forgetting to read and base64-encode the file; using a constructor that sets uri but not data; loading image metadata from a database without populating the data field.","solutions":["Provide base64-encoded data when creating the ImageContent","Read the image file and encode it: ImageContent(data=base64.b64encode(open('img.png','rb').read()).decode())","If using a file path, pre-load and encode the data before adding to the message"],"exampleFix":"// before\nImageContent(uri='file:///img.png')\n// after\nimport base64\nImageContent(data=base64.b64encode(open('img.png','rb').read()).decode())","handlingStrategy":"validation","validationCode":"from semantic_kernel.contents import ImageContent\n\ndef validate_image_items(message):\n    for item in message.items:\n        if isinstance(item, ImageContent) and not item.data:\n            raise ValueError(f'ImageContent at index is missing base64 data')\n\nvalidate_image_items(user_message)","typeGuard":"from semantic_kernel.contents import ImageContent\n\ndef has_valid_image_data(message) -> bool:\n    return all(\n        item.data 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 'data encoded as base64' in str(e):\n        logger.error('User message contains an image without base64 data')\n        raise","preventionTips":["Always encode image files to base64 before creating ImageContent","Add a pre-send validation pass over message items for user messages with images","Create a helper that reads a file path and returns a fully-populated ImageContent"],"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"}