#8737·tfjs

Feature Request: Add padSequences utility function for sequence preprocessing

Author: bruce619Created Jul 24, 2026Updated Sep 3, 2026
Labelstype:featureMerge : Status Pending
  • TensorFlow.js version (4.8.0): @tensorflow/tfjs-node ^4.8.0 (Request applies to all current/future versions)
  • Are you willing to contribute it (Yes/No): Yes

Current State: TensorFlow.js currently lacks a high-level padSequences utility equivalent to Python's tf.keras.utils.pad_sequences.

While tf.pad() exists in TF.js, it operates strictly on existing tensors and requires manual dimension mapping. When working with variable-length sequence data (such as tokenized text strings or time-series arrays in JavaScript), developers are forced to write custom array-manipulation boilerplate to handle padding, truncating, and tensor conversion before passing inputs into a model.

Proposed Feature: Introduce a high-level padSequences utility function (exposed under @tensorflow/tfjs-layers / tf.util or top-level utilities) that accepts nested sequence arrays and pads/truncates them to a uniform length, returning a formatted Tensor2D.

Key capabilities to support:

  • Custom maxlen (defaulting to the longest sequence in the input).
  • Pre- and post-padding (padding: 'pre' | 'post').
  • Pre- and post-truncating (truncating: 'pre' | 'post').
  • Custom pad value (defaulting to 0).
  • Configurable output dtype (defaulting to 'int32').

Will this change the current api? How?

No, this is purely an additive feature and will not break any existing APIs.

It introduces a new utility function exposed alongside existing layer utilities in @tensorflow/tfjs-layers or tf.util:

typescript
tf.util.padSequences(sequences, options?)
// or
tf.layers.padSequences(sequences, options?)