{"record":{"id":"883f6b55bf963203","repo":"tensorflow/models","slug":"num-steps-should-be-a-tf-tensor-passing-a-pyt","errorCode":null,"errorMessage":"`num_steps` should be a `tf.Tensor`. Passing a Python value can cause unnecessary retracing when wrapped by `tf.function`.","messagePattern":"`num_steps` should be a `tf\\.Tensor`\\. Passing a Python value can cause unnecessary retracing when wrapped by `tf\\.function`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"orbit/utils/loop_fns.py","lineNumber":116,"sourceCode":"\n  Returns:\n    A loop function taking required `iterator` and `num_steps` parameters. If\n    called inside a `tf.function`, the loop will be converted by AutoGraph into\n    a `tf.while_loop` construct. See the `loop_fn` definition below for\n    additional details.\n  \"\"\"\n\n  def loop_fn(iterator, num_steps):\n    \"\"\"Makes `num_steps` calls to `step_fn(iterator)`.\n\n    Args:\n      iterator: A nested structure of `tf.data.Iterator` or\n        `DistributedIterator`.\n      num_steps: The number of steps in the loop. Should be passed as a\n        `tf.Tensor`. Iterating until iterator exhaustion is not supported.\n    \"\"\"\n    if not isinstance(num_steps, tf.Tensor):\n      raise ValueError(\n          \"`num_steps` should be a `tf.Tensor`. Passing a Python value can \"\n          \"cause unnecessary retracing when wrapped by `tf.function`.\")\n\n    for _ in tf.range(num_steps):\n      # Clear out the outer name scope so the ops created inside `tf.while_loop`\n      # don't get \"while/\" as name prefix.\n      with tf.name_scope(\"\"):  # pyrefly: ignore[bad-instantiation]\n        step_fn(iterator)\n\n  return loop_fn\n\n\ndef create_tf_while_loop_fn_with_state(step_fn):\n  \"\"\"Creates a TF while loop function with state.\n\n  This function is similar to `create_tf_while_loop_fn`, but allowing a `state`\n  to be accumulated over multiple iterations of the loop. Note that the\n  structure of the `state` cannot be changed across iterations.","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/orbit/utils/loop_fns.py#L98-L134","documentation":"Error \"`num_steps` should be a `tf.Tensor`. Passing a Python value can cause unnecessary retracing when wrapped by `tf.function`.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at orbit/utils/loop_fns.py:116 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass num_steps as a tf.Tensor (e.g. tf.constant(n)) to avoid retracing.","Keep passing a tf.Tensor when the loop is wrapped in tf.function."],"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"}