# OutOfRangeError: End of sequence [Op:IteratorGetNext]

- **ID:** `tensorflow/iterator-get-next-out-of-range`
- **Domain:** tensorflow
- **Category:** runtime_error
- **Error Code:** `EOS`
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

A tf.data iterator has been exhausted because the dataset has no more elements, but the training loop attempts to fetch again.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| tensorflow 2.15 | active | — | — |
| tensorflow 2.16 | active | — | — |
| tensorflow 2.17 | active | — | — |

## Workarounds

1. **Wrap the training loop with `for batch in dataset.take(num_batches):` instead of using a manual iterator, ensuring the loop terminates when data runs out. Example: `for batch in train_dataset.take(steps_per_epoch): model.train_on_batch(batch)`.** (95% success)
   ```
   Wrap the training loop with `for batch in dataset.take(num_batches):` instead of using a manual iterator, ensuring the loop terminates when data runs out. Example: `for batch in train_dataset.take(steps_per_epoch): model.train_on_batch(batch)`.
   ```
2. **Use `dataset.repeat()` before iterating to allow multiple epochs, but ensure `steps_per_epoch` is set correctly to avoid infinite training.** (85% success)
   ```
   Use `dataset.repeat()` before iterating to allow multiple epochs, but ensure `steps_per_epoch` is set correctly to avoid infinite training.
   ```

## Dead Ends

- **** — Increasing the dataset size via repeat() without proper cardinality handling can mask the issue but leads to infinite loops. (70% fail)
- **** — Manually catching the error with try/except in eager mode works but does not fix the root cause of the loop logic. (50% fail)
