def _content_block_to_bedrock_format(
    block: ContentBlock, role: MessageRole
) -> Optional[Dict[str, Any]]:
    """Convert content block to AWS Bedrock Converse API required format."""
    if isinstance(block, TextBlock):
        if not block.text:
            return None
        return {
            "text": block.text,
        }
    elif isinstance(block, DocumentBlock):
        if not block.data:
            file_buffer = block.resolve_document()
            with file_buffer as f:
                data = f.read()
        else:
            data = base64.b64decode(block.data)
        title = block.title
        # NOTE: At the time of writing, "txt" format works for all file types
        # The API then infers the format from the file type based on the bytes
        return {"document": {"format": "txt", "name": title, "source": {"bytes": data}}}
    elif isinstance(block, ImageBlock):
        if role != MessageRole.USER:
            logger.warning(
                "Bedrock Converse API only supports image blocks for user messages."
            )
            return None
        # NOTE: Bedrock Converse API accepts raw image bytes directly
        # No need to convert the image to base64 or UTF-8 format
        # The image will be passed to the API in its raw binary form
        img_format = __get_img_format_from_image_mimetype(block.image_mimetype)
        raw_image_bytes = block.resolve_image(as_base64=False).read()
        return {"image": {"format": img_format, "source": {"bytes": raw_image_bytes}}}
    elif isinstance(block, AudioBlock):
        logger.warning("Audio blocks are not supported in Bedrock Converse API.")
        return None
    else:
        logger.warning(f"Unsupported block type: {type(block)}")
        return None
Convert content block to AWS Bedrock Converse API required format.
llama-index-integrations/llms/llama-index-llms-bedrock-converse/llama_index/llms/bedrock_converse/utils.py