crewAIInc/crewAI · error · ValueError
The number of iterations must be a positive integer.
Error message
The number of iterations must be a positive integer.
What it means
ValueError raised by train_crew() in crewai_cli/train_crew.py when n_iterations <= 0, guarding the `uv run train <n> <file>` subprocess invocation. Training must run at least one iteration, so zero or negative counts are rejected before any subprocess starts. Note the exception is caught by the local generic handler and echoed to stderr.
Source
Thrown at lib/cli/src/crewai_cli/train_crew.py:17
import subprocess
import click
def train_crew(n_iterations: int, filename: str) -> None:
"""
Train the crew by running a command in the UV environment.
Args:
n_iterations (int): The number of iterations to train the crew.
"""
command = ["uv", "run", "train", str(n_iterations), filename]
try:
if n_iterations <= 0:
raise ValueError("The number of iterations must be a positive integer.")
if not filename.endswith(".pkl"):
raise ValueError("The filename must not end with .pkl")
result = subprocess.run(command, capture_output=False, text=True, check=True) # noqa: S603
if result.stderr:
click.echo(result.stderr, err=True)
except subprocess.CalledProcessError as e:
click.echo(f"An error occurred while training the crew: {e}", err=True)
click.echo(e.output, err=True)
except Exception as e:
click.echo(f"An unexpected error occurred: {e}", err=True)
View on GitHub (pinned to 754d7323be)
Solutions
- Pass a positive integer: `crewai train 5 memory.pkl`.
- If invoking programmatically, validate n_iterations >= 1 before calling train_crew.
- Check shell argument parsing — a flag-like value (e.g. -1) may need `--` separation in your CLI parser.
Defensive patterns
Strategy: validation
Validate before calling
n = int(sys.argv[1])
if n <= 0:
raise SystemExit("n_iterations must be a positive integer") Type guard
def is_positive_int(value) -> bool:
return isinstance(value, int) and not isinstance(value, bool) and value > 0 Prevention
- Validate iteration count >= 1 before invoking train
- Default loop variables to a positive number, never 0
When it happens
Trigger: Calling train_crew(0, 'memory.pkl') or with a negative count — commonly from the CLI layer when the user runs `crewai train 0 memory.pkl` or passes an argument that evaluates to <= 0.
Common situations: Scripts computing iteration counts from a variable that defaults to 0; typos like `crewai train -1 file.pkl`; misunderstanding that 0 means 'no training' rather than being accepted.
Related errors
- The filename must not end with .pkl
- An error occurred while training the crew: {e}
- Invalid JSON payload provided as argument
- Invalid JSON payload provided as argument
- Failed to publish tool. Local changes need to be resolved be
AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15).
Data as JSON: /api/errors/53f1bfa224ae232b.
Report an issue: GitHub.