Just to extend the official example 8 in the documentation
import tensorflow as tf
def gen():
  ragged_tensor = tf.ragged.constant([[1, 2], [3]])
  yield 42, ragged_tensor

dataset = tf.data.Dataset.from_generator(
     gen,
     output_signature=(
         tf.TensorSpec(shape=(), dtype=tf.int32),
         tf.RaggedTensorSpec(shape=(2, None), dtype=tf.int32)))
print(dataset.cardinality())

You will see that cardinality is -2 that is tf.data.experimental.UNKNOWN_CARDINALITY.
So it can’t quickly estimate the cardinality on datasets using from_generator
You can use tf.data.experimental.assert_cardinality 28 to manually set the cardinality.