def messages_to_converse_messages(
    messages: Sequence[ChatMessage],
) -> Tuple[Sequence[Dict[str, Any]], str]:
    """
    Converts a list of generic ChatMessages to AWS Bedrock Converse messages.

    Args:
        messages: List of ChatMessages

    Returns:
        Tuple of:
        - List of AWS Bedrock Converse messages
        - System prompt

    """
    converse_messages = []
    system_prompt = ""
    for message in messages:
        if message.role == MessageRole.SYSTEM and message.content:
            system_prompt += (message.content) + "\n"
        elif message.role in [MessageRole.FUNCTION, MessageRole.TOOL]:
            # convert tool output to the AWS Bedrock Converse format
            content = {
                "toolResult": {
                    "toolUseId": message.additional_kwargs["tool_call_id"],
                    "content": [{"text": message.content}] if message.content else [],
                }
            }
            if status := message.additional_kwargs.get("status"):
                content["toolResult"]["status"] = status
            converse_messages.append(
                {
                    "role": MessageRole.USER.value,  # bedrock converse api requires the role to be user
                    "content": [content],
                }
            )
        else:
            content = []
            for block in message.blocks:
                bedrock_format_block = _content_block_to_bedrock_format(
                    block=block,
                    role=message.role,
                )
                if bedrock_format_block:
                    content.append(bedrock_format_block)

            if content:
                converse_messages.append(
                    {
                        "role": message.role.value,
                        "content": content,
                    }
                )

        # convert tool calls to the AWS Bedrock Converse format
        # NOTE tool calls might show up within any message,
        # e.g. within assistant message or in consecutive tool calls,
        # thus this tool call check is done for all messages
        tool_calls = message.additional_kwargs.get("tool_calls", [])
        content = []
        for tool_call in tool_calls:
            assert "toolUseId" in tool_call, f"`toolUseId` not found in {tool_call}"
            assert "input" in tool_call, f"`input` not found in {tool_call}"
            assert "name" in tool_call, f"`name` not found in {tool_call}"
            tool_input = tool_call["input"] if tool_call["input"] else {}
            if isinstance(tool_input, str):
                tool_input = json.loads(tool_input)
            content.append(
                {
                    "toolUse": {
                        "input": tool_input,
                        "toolUseId": tool_call["toolUseId"],
                        "name": tool_call["name"],
                    }
                }
            )
        if len(content) > 0:
            converse_messages.append(
                {
                    "role": "assistant",  # tool calls are always from the assistant
                    "content": content,
                }
            )

    return __merge_common_role_msgs(converse_messages), system_prompt.strip()

    Converts a list of generic ChatMessages to AWS Bedrock Converse messages.

    Args:
        messages: List of ChatMessages

    Returns:
        Tuple of:
        - List of AWS Bedrock Converse messages
        - System prompt

    
llama-index-integrations/llms/llama-index-llms-bedrock-converse/llama_index/llms/bedrock_converse/utils.py