# tf.data.experimental.Optional

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/data/experimental/Optional) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/data/ops/optional_ops.py#L36-L121) |

Wraps a value that may/may not be present at runtime.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.data.experimental.Optional`](https://www.tensorflow.org/api_docs/python/tf/experimental/Optional)

<br />

An `Optional` can represent the result of an operation that may fail as a
value, rather than raising an exception and halting execution. For example,
[`tf.data.experimental.get_next_as_optional`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/data/experimental/get_next_as_optional) returns an `Optional` that either
contains the next value from a [`tf.compat.v1.data.Iterator`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/compat/v1/data/Iterator) if one exists, or
a "none" value that indicates the end of the sequence has been reached.

`Optional` can only be used by values that are convertible to `Tensor` or
`CompositeTensor`.

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `value_structure` | The structure of the components of this optional. |

<br />

## Methods

### `from_value`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/data/ops/optional_ops.py#L87-L105)

    @staticmethod
    from_value(
        value
    )

Returns an `Optional` that wraps the given value.

<br />

<br />

<br />

| Args ||
|---|---|
| `value` | A value to wrap. The value must be convertible to `Tensor` or `CompositeTensor`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `Optional` that wraps `value`. ||

<br />

### `get_value`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/data/ops/optional_ops.py#L61-L75)

    @abc.abstractmethod
    get_value(
        name=None
    )

Returns the value wrapped by this optional.

If this optional does not have a value (i.e. `self.has_value()` evaluates
to `False`), this operation will raise [`tf.errors.InvalidArgumentError`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/errors/InvalidArgumentError)
at runtime.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | (Optional.) A name for the created operation. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The wrapped value. ||

<br />

### `has_value`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/data/ops/optional_ops.py#L49-L59)

    @abc.abstractmethod
    has_value(
        name=None
    )

Returns a tensor that evaluates to `True` if this optional has a value.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | (Optional.) A name for the created operation. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A scalar [`tf.Tensor`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/Tensor) of type [`tf.bool`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf#bool). ||

<br />

### `none_from_structure`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/data/ops/optional_ops.py#L107-L121)

    @staticmethod
    none_from_structure(
        value_structure
    )

Returns an `Optional` that has no value.

> [!NOTE]
> **Note:** This method takes an argument that defines the structure of the value that would be contained in the returned `Optional` if it had a value.

<br />

<br />

<br />

| Args ||
|---|---|
| `value_structure` | A `Structure` object representing the structure of the components of this optional. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `Optional` that has no value. ||

<br />