How can I assign a unique identifier to each cart line item?

I am looking for a workaround to come up with a unique and static identifier for the cart line item.
Using variantId is not an option because there might be several line items with the same variantId.

Using lineitem key would be an option but it gets updated when lineitem properties are changed, Also keys of all line items are getting updated when quantity for a single lineitem is changed for a large amount, for example from 1 to 50.

The problem with “line” i.e. index of line items is that in my cart user can add and remove warranties for each line item, and each warranty is a separate product i.e. line item so while adding or removing warranties the line property also gets changed.

If anyone has any possible solution in mind please share it

Add custom line item id:

  1. UUID (Universally Unique Identifier): Consider using a universally unique identifier (UUID) for each line item. A UUID is a 128-bit number that guarantees uniqueness across space and time. Many programming languages and platforms have libraries or built-in functions for generating UUIDs.

  2. Concatenation of Values: If you want more control over the format of your line item IDs, you could concatenate multiple values to create a unique identifier. For example, you could combine the variant ID, customer ID (if applicable), and a timestamp to create a composite identifier.

  3. Hashing: You could hash a combination of the variant ID, timestamp, and other relevant data to create a unique identifier. This approach ensures uniqueness and can be generated in a consistent manner.