ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more


Represents a value that may or may not be present.

A tf.experimental.Optional can represent the result of an operation that may fail as a value, rather than raising an exception and halting execution. For example, returns a tf.experimental.Optional that either contains the next element of an iterator if one exists, or an "empty" value that indicates the end of the sequence has been reached.

tf.experimental.Optional can only be used with values that are convertible to tf.Tensor or tf.CompositeTensor.

One can create a tf.experimental.Optional from a value using the from_value() method:

optional = tf.experimental.Optional.from_value(42)
tf.Tensor(True, shape=(), dtype=bool)
tf.Tensor(42, shape=(), dtype=int32)

or without a value using the empty() method:

optional = tf.experimental.Optional.empty(
  tf.TensorSpec(shape=(), dtype=tf.int32, name=None))
tf.Tensor(False, shape=(), dtype=bool)

element_spec The type specification of an element of this optional.

optional = tf.experimental.Optional.from_value(42)
tf.TensorSpec(shape=(), dtype=tf.int32, name=None)