{"record":{"id":"f0af70b984d5d800","repo":"tensorflow/models","slug":"steps-should-be-an-tensor-python-object-may-cause","errorCode":null,"errorMessage":"steps should be an Tensor. Python object may cause retracing.","messagePattern":"steps should be an Tensor\\. Python object may cause retracing\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/bert/model_training_utils.py","lineNumber":369,"sourceCode":"      # For reporting, the metric takes the mean of losses.\n      train_loss_metric.update_state(raw_loss)\n      for metric in train_metrics:\n        metric.update_state(labels, model_outputs)\n\n    @tf.function\n    def train_steps(iterator, steps):\n      \"\"\"Performs distributed training steps in a loop.\n\n      Args:\n        iterator: the distributed iterator of training datasets.\n        steps: an tf.int32 integer tensor to specify number of steps to run\n          inside host training loop.\n\n      Raises:\n        ValueError: Any of the arguments or tensor shapes are invalid.\n      \"\"\"\n      if not isinstance(steps, tf.Tensor):\n        raise ValueError('steps should be an Tensor. Python object may cause '\n                         'retracing.')\n\n      for _ in tf.range(steps):\n        strategy.run(_replicated_step, args=(next(iterator),))  # pyrefly: ignore[missing-attribute]\n\n    def train_single_step(iterator):\n      \"\"\"Performs a distributed training step.\n\n      Args:\n        iterator: the distributed iterator of training datasets.\n\n      Raises:\n        ValueError: Any of the arguments or tensor shapes are invalid.\n      \"\"\"\n      strategy.run(_replicated_step, args=(next(iterator),))  # pyrefly: ignore[missing-attribute]\n\n    def test_step(iterator):\n      \"\"\"Calculates evaluation metrics on distributed devices.\"\"\"","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/bert/model_training_utils.py#L351-L387","documentation":"Error \"steps should be an Tensor. Python object may cause retracing.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/bert/model_training_utils.py:369 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}