Why am I getting inconsistent results with checkout.line_items in Liquid script?

I am trying to collect data from the order status page via Liquid script but I am getting inconsistent results when accessing checkout.line_items.

I added the code below to the order status page additional scripts section, and for some orders I get the correct amount of items, but for some I get 0. I can’t find a pattern, but it’s consistent between orders (once 0 always 0, once correct always correct). For some orders this prop exists, for some it doesn’t.

Obviously all orders have items.

Can anyone shed some light please?

To complement, the whole checkout object seems to not be present. This line of code prints nothing:

console.log(‘checkout.id’, {{checkout.id}});