microsoft/semantic-kernel · error · ServiceInvalidRequestError

ImageContent without data_uri in User message while formatti

Error message

ImageContent without data_uri in User message while formatting chat history for Google AI

What it means

Raised by _create_image_part() when an ImageContent has no data_uri. The Google AI/Vertex AI generative API cannot fetch images from arbitrary URLs (see the linked upstream issue), so Semantic Kernel requires an inline base64 data_uri to build the Part via Part.from_data. A bare URL or empty image content is rejected before the request is built.

Source

Thrown at python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py:189

            ),
        )
        settings.tools = [
            Tool(
                function_declarations=[
                    kernel_function_metadata_to_vertex_ai_function_call_format(f)
                    for f in function_choice_configuration.available_functions
                ]
            )
        ]


def _create_image_part(image_content: ImageContent) -> Part:
    if image_content.data_uri:
        return Part.from_data(image_content.data, image_content.mime_type)  # type: ignore[arg-type]

    # The Google AI API doesn't support images from arbitrary URIs:
    # https://github.com/google-gemini/generative-ai-python/issues/357
    raise ServiceInvalidRequestError(
        "ImageContent without data_uri in User message while formatting chat history for Google AI"
    )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Build ImageContent with an inline data_uri: encode the image bytes as a base64 data URI (data:<mime>;base64,...) and pass it so data_uri is truthy.
  2. Load the remote image bytes yourself, then construct ImageContent(data_uri='data:image/png;base64,...', data=bytes, mime_type='image/png').
  3. Avoid setting only the uri field; populate data_uri with the embedded base64 payload.
  4. Validate image_content.data_uri is non-empty before adding the item to the user message.

Example fix

// before
ImageContent(uri='https://cdn/img.png')
# after
import base64
b = base64.b64encode(raw_bytes).decode()
ImageContent(data_uri=f'data:image/png;base64,{b}', data=raw_bytes, mime_type='image/png')
Defensive patterns

Strategy: validation

Validate before calling

for item in user_msg.items:
    if isinstance(item, ImageContent):
        assert item.data_uri, 'ImageContent needs an inline base64 data_uri for Vertex AI'

Type guard

def has_inline_data(image_content) -> bool:
    return bool(getattr(image_content, 'data_uri', None))

Prevention

When it happens

Trigger: Constructing ImageContent with only a uri pointing at a public/remote URL (or no data at all) and passing it in a Vertex AI chat request. Loading image references from a database or CDN URL instead of embedding the bytes.

Common situations: Assuming Vertex AI accepts HTTPS image URLs like OpenAI vision endpoints do. Forgetting to base64-encode a local file. Passing an ImageContent built for another provider whose uri field is populated but data_uri is not.

Related errors


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