# ResourceExhaustedError: The function 'train_step' has been retraced 1000 times. The tracing limit has been reached. This may be caused by passing Python literals or tensors with changing shapes.

- **ID:** `tensorflow/tf-function-recompilation-cache-limit`
- **Domain:** tensorflow
- **Category:** resource_error
- **Error Code:** `ERTL`
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

A tf.function-decorated function (e.g., train_step) is being re-traced excessively because it receives arguments with varying shapes or Python values that are not cached as part of the function's input signature, exhausting the tracing cache.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| TensorFlow 2.6.0 | active | — | — |
| TensorFlow 2.10.0 | active | — | — |

## Workarounds

1. **Ensure that all tensor arguments to the tf.function have consistent shapes. Pad or resize inputs to a fixed shape before passing them. For Python arguments, convert them to tensors or use tf.constant() to make them part of the graph signature.** (90% success)
   ```
   Ensure that all tensor arguments to the tf.function have consistent shapes. Pad or resize inputs to a fixed shape before passing them. For Python arguments, convert them to tensors or use tf.constant() to make them part of the graph signature.
   ```
2. **Define the input signature explicitly using tf.TensorSpec to prevent retracing due to shape or dtype variations. This tells TensorFlow to use a single graph for all calls matching the signature.** (85% success)
   ```
   Define the input signature explicitly using tf.TensorSpec to prevent retracing due to shape or dtype variations. This tells TensorFlow to use a single graph for all calls matching the signature.
   ```
3. **If the function uses Python integer or boolean arguments that change, convert them to tensors or move them outside the tf.function by using tf.cond() or tf.switch_case() for control flow.** (80% success)
   ```
   If the function uses Python integer or boolean arguments that change, convert them to tensors or move them outside the tf.function by using tf.cond() or tf.switch_case() for control flow.
   ```

## Dead Ends

- **** — Running eagerly defeats the purpose of tf.function (performance), and increasing the limit only delays the error without fixing the root cause of shape variability. (90% fail)
- **** — While this can reduce retracing for shape changes, it does not address retracing caused by changing Python literal values (e.g., integer arguments), and may still hit the limit. (70% fail)
- **** — This eliminates retracing but also removes the performance benefits of graph compilation, potentially slowing training significantly. (60% fail)
