{"record":{"id":"d69d5da977229fac","repo":"microsoft/semantic-kernel","slug":"imagecontent-without-data-uri-in-user-message-whil","errorCode":null,"errorMessage":"ImageContent without data_uri in User message while formatting chat history for Google AI","messagePattern":"ImageContent without data_uri in User message while formatting chat history for Google AI","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/google/google_ai/services/utils.py","lineNumber":201,"sourceCode":"            }\n        }\n        settings.tools = [\n            {\n                \"function_declarations\": [\n                    kernel_function_metadata_to_google_ai_function_call_format(f)\n                    for f in function_choice_configuration.available_functions\n                ]\n            }\n        ]\n\n\ndef _create_image_part(image_content: ImageContent) -> Part:\n    if image_content.data_uri:\n        return Part.from_bytes(data=image_content.data, mime_type=image_content.mime_type)  # type: ignore[arg-type]\n\n    # The Google AI API doesn't support images from arbitrary URIs:\n    # https://github.com/google-gemini/generative-ai-python/issues/357\n    raise ServiceInvalidRequestError(\n        \"ImageContent without data_uri in User message while formatting chat history for Google AI\"\n    )\n","sourceCodeStart":183,"sourceCodeEnd":204,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/google/google_ai/services/utils.py#L183-L204","documentation":"Raised by _create_image_part when an ImageContent has no data_uri. The Gemini API does not support fetching images from arbitrary URIs (see the linked google-gemini issue); it only accepts inline image bytes. When data_uri is set, the formatter reads image_content.data and mime_type to construct Part.from_bytes. Without data_uri, there is no inline data to send and the request cannot proceed.","triggerScenarios":"Adding an ImageContent to a user or assistant message with only a uri (remote URL or local path) but no data_uri / inline data, then calling a chat completion method.","commonSituations":"Constructing ImageContent(uri='https://...') expecting the API to fetch it (Gemini does not support this); passing ImageContent(data=b'...') without setting data_uri; migrating from an OpenAI connector that accepts URL-based images.","solutions":["Provide inline image data: ImageContent(data=image_bytes, mime_type='image/png', data_uri='data:image/png;base64,...').","If you have a URL, download the image bytes first and pass them as inline data with a data_uri.","Set data_uri to a truthy value whenever ImageContent.data is populated, so the formatter uses the inline-bytes path."],"exampleFix":"# before\nimg = ImageContent(uri='https://example.com/cat.png')  # no inline data\nhistory.add_user_message(items=[TextContent(text='what is this?'), img])\n\n# after\nimport httpx, base64\nresp = httpx.get('https://example.com/cat.png')\nb64 = base64.b64encode(resp.content).decode()\nimg = ImageContent(data=resp.content, mime_type='image/png', data_uri=f'data:image/png;base64,{b64}')\nhistory.add_user_message(items=[TextContent(text='what is this?'), img])","handlingStrategy":"validation","validationCode":"def validate_image_content(image_content):\n    if not image_content.data_uri:\n        raise ValueError('ImageContent requires inline data_uri for Google AI; remote URIs are not supported.')\n    if not image_content.data:\n        raise ValueError('ImageContent.data is empty; provide inline image bytes.')","typeGuard":"def is_valid_google_ai_image(image_content) -> bool:\n    return bool(getattr(image_content, 'data_uri', None)) and bool(getattr(image_content, 'data', None))","tryCatchPattern":null,"preventionTips":["Always download remote image URLs into bytes before adding to chat history for Google AI.","Set data_uri on every ImageContent you construct, even when using inline bytes.","Write a helper that builds ImageContent from a URL by fetching and inlining the bytes."],"tags":["google-ai","image","image-content","data-uri","inline-bytes","chat-history"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}