code: def _add_ragged_fields(
      example_data, tensor_info: feature_lib.TensorInfo
  ):
      """Optionally convert the ragged data into flat/row_lengths fields.
  
      Example:
  
      ```
      example_data = [
          [1, 2, 3],
          [],
          [4, 5]
      ]
      tensor_info = TensorInfo(shape=(None, None,), sequence_rank=2, ...)
      out = _add_ragged_fields(example_data, tensor_info)
      out == {
          'ragged_flat_values': ([1, 2, 3, 4, 5], TensorInfo(shape=(), ...)),
          'ragged_row_length_0': ([3, 0, 2], TensorInfo(shape=(None,), ...))
      }
      ```
  
      If `example_data` isn't ragged, `example_data` and `tensor_info` are
      forwarded as-is.
  
      Args:
        example_data: Data to optionally convert to ragged data.
        tensor_info: TensorInfo associated with the given data.
  
      Returns:
        A tuple(example_data, tensor_info) if the tensor isn't ragged, or a dict of
          tuple(example_data, tensor_info) if the tensor is ragged.
      """
      # Step 1: Extract the ragged tensor info
      if tensor_info.sequence_rank:
          # If the input is ragged, extract the nested values.
          # 1-level sequences are converted as numpy and stacked.
          # If the sequence is empty, a np.empty(shape=(0, ...)) array is returned.
          example_data, nested_row_lengths = _extract_ragged_attributes(
              example_data, tensor_info
          )
  
      # Step 2: Format the ragged tensor data as dict
      # No sequence or 1-level sequence, forward the data.
      # Could eventually handle multi-level sequences with static lengths
      # in a smarter way.
      if tensor_info.sequence_rank < 2:
          return (example_data, tensor_info)
      # Multiple level sequence:
      else:
          tensor_info_length = feature_lib.TensorInfo(
              shape=(None,), dtype=np.int64
          )
          ragged_attr_dict = {
              "ragged_row_lengths_{}".format(i): (
                  length,
                  tensor_info_length,
              )
              for i, length in enumerate(nested_row_lengths)
          }
          tensor_info_flat = feature_lib.TensorInfo(
              shape=(None,)
              + tensor_info.shape[tensor_info.sequence_rank :],
              dtype=tensor_info.dtype,
          )
          ragged_attr_dict["ragged_flat_values"] = (
              example_data,
              tensor_info_flat,
          )
          return ragged_attr_dict
  
imports: from __future__ import annotations
  import abc
  import collections
  import dataclasses
  from typing import Any, Mapping, Union
  import numpy
  from tensorflow_datasets.core import utils
  from tensorflow_datasets.core.features import feature
  from tensorflow_datasets.core.utils import dtype_utils
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
  from tensorflow_datasets.proto import tf_example_pb2
  from tensorflow_datasets.proto import tf_feature_pb2
label_desc: Use image node_id 6 for calling a global function with example usage: _add_ragged_fields(example_data, tensor_info) and returns: example_data,  tensor_info, ragged_attr_dict