{"record":{"id":"6f3e118d85d37ed0","repo":"microsoft/semantic-kernel","slug":"imagecontent-must-have-either-a-data-uri-or-uri-se","errorCode":null,"errorMessage":"ImageContent must have either a data_uri or uri set to be used in the request.","messagePattern":"ImageContent must have either a data_uri or uri set to be used in the request\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/open_ai/responses_agent_thread_actions.py","lineNumber":819,"sourceCode":"                match content:\n                    case TextContent() | StreamingTextContent():\n                        final_text = content.text\n                        if not isinstance(final_text, str):\n                            if isinstance(final_text, (list, tuple)):\n                                final_text = \" \".join(map(str, final_text))\n                            else:\n                                final_text = str(final_text)\n                        text_type = \"input_text\" if original_role == AuthorRole.USER else \"output_text\"\n                        contents.append({\"type\": text_type, \"text\": final_text})\n                    case ImageContent():\n                        image_url = \"\"\n                        if content.data_uri:\n                            image_url = content.data_uri\n                        elif content.uri:\n                            image_url = str(content.uri)\n\n                        if not image_url:\n                            raise ValueError(\n                                \"ImageContent must have either a data_uri or uri set to be used in the request.\"\n                            )\n\n                        contents.append({\"type\": \"input_image\", \"image_url\": image_url})\n                    case FunctionCallContent():\n                        if not store_enabled:\n                            fc_dict = {\n                                \"type\": \"function_call\",\n                                \"call_id\": content.call_id,\n                                \"name\": content.name,\n                                \"arguments\": content.arguments,\n                            }\n                            response_inputs.append(fc_dict)\n                    case FunctionResultContent():\n                        rfrc_dict = {\n                            \"type\": \"function_call_output\",\n                            \"output\": str(content.result),\n                            \"call_id\": content.call_id,","sourceCodeStart":801,"sourceCodeEnd":837,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/responses_agent_thread_actions.py#L801-L837","documentation":"Raised as a ValueError when serializing chat history for a Responses request, an ImageContent item is encountered that has neither a data_uri nor a uri. The serializer (_prepare_chat_history_for_request) needs a URL or data URI to build the input_image payload; with neither, it cannot construct a valid request, so it fails fast.","triggerScenarios":"A ChatMessageContent containing an ImageContent item with both data_uri and uri empty/None is passed into the agent's chat history. Occurs when constructing images manually (e.g. ImageContent(data_uri=None, uri=None)) or when an image was created but its source was never set.","commonSituations":"Building ImageContent from a local file but forgetting to call to_uri()/encode it to a data URI; placeholders/empty image content objects left in the history; copying an ImageContent and losing the uri; loading images from a source that returned None.","solutions":["Ensure every ImageContent has a data_uri (base64) or a resolvable uri before adding it to chat history.","For local files, use the helper to build a data URI: ImageContent.from_image_file(path) or encode bytes as a data URI.","Validate image content before sending: assert content.data_uri or content.uri.","Filter out empty/placeholder image items before invoking the agent."],"exampleFix":"# before\nfrom semantic_kernel.contents import ImageContent\nmsg = ChatMessageContent(role=AuthorRole.USER, items=[\n    ImageContent(),  # no data_uri or uri -> error\n])\n# after - provide a data URI\nmsg = ChatMessageContent(role=AuthorRole.USER, items=[\n    ImageContent(data_uri=\"data:image/png;base64,iVBORw0KG...\"),\n])","handlingStrategy":"validation","validationCode":"# Validate image content before adding to chat history:\ndef assert_image_usable(content):\n    if not (content.data_uri or content.uri):\n        raise ValueError(\"ImageContent needs a data_uri or uri\")\nfor msg in chat_history:\n    for item in msg.items:\n        if type(item).__name__ == \"ImageContent\":\n            assert_image_usable(item)","typeGuard":"from semantic_kernel.contents import ImageContent\ndef has_image_source(content: ImageContent) -> bool:\n    return bool(getattr(content, \"data_uri\", None) or getattr(content, \"uri\", None))","tryCatchPattern":"try:\n    async for _, m in agent.invoke(thread=thread):\n        ...\nexcept ValueError as ex:\n    if \"ImageContent\" in str(ex):\n        # remove/fix the offending image item and retry\n        ...","preventionTips":["Always set data_uri or uri when constructing ImageContent.","For local files, convert to a data URI before adding to history.","Filter history for empty image items before invoking."],"tags":["openai-responses","image-content","validation","serialization"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}