Skip to content

Amazon Bedrock: Image data URLs not properly handled - incorrect mediaType and data format #722

@amirglatt

Description

@amirglatt

Description

When using @openai/agents-extensions with Amazon Bedrock models and providing images as base64 data URLs, the conversion in itemsToLanguageV2Messages causes errors because:

  1. Incorrect data format: The entire data URL string (including data:image/png;base64, prefix) is passed to the data field, but Bedrock expects only the raw base64 data
  2. Hardcoded mediaType: The mediaType is hardcoded to 'image/*', which Bedrock rejects with the error: Unsupported image mime type: image/*, expected one of: image/jpeg, image/png, image/gif, image/webp

Steps to Reproduce

  1. Configure an agent to use an Amazon Bedrock model (e.g., Claude via @ai-sdk/amazon-bedrock)
  2. Send a message with an image as a base64 data URL: data:image/png;base64,iVBORw0K...
  3. Agent fails with: AI_UnsupportedFunctionalityError: Unsupported image mime type: image/*

Expected Behavior

Images provided as base64 data URLs should work with Amazon Bedrock models.

Current Code (aiSdk.ts ~line 53)

const url = new URL(imageSource);
return {
    type: 'file',
    data: url.toString(),        // ❌ Passes entire data URL string
    mediaType: 'image/*',        // ❌ Hardcoded, Bedrock rejects this
    providerOptions: { ... },
};

Proposed Fix

const url = new URL(imageSource);
// For data URLs, extract MIME type and raw base64 data
let mediaType = 'image/*';
let imageData = url.toString();
if (url.protocol === 'data:') {
    const match = imageSource.match(/^data:([^;,]+)/);
    if (match && match[1]) {
        mediaType = match[1];
    }
    // Extract raw base64 data (after the comma)
    const commaIndex = imageSource.indexOf(',');
    if (commaIndex !== -1) {
        imageData = imageSource.substring(commaIndex + 1);
    }
}
return {
    type: 'file',
    data: imageData,             // ✅ Only raw base64 for data URLs
    mediaType: mediaType,        // ✅ Actual MIME type from data URL
    providerOptions: { ... },
};

Environment

  • @openai/agents-extensions: 0.3.3
  • @ai-sdk/amazon-bedrock: latest
  • Node.js: 22.x

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions