{"record":{"id":"53f1bfa224ae232b","repo":"crewAIInc/crewAI","slug":"the-number-of-iterations-must-be-a-positive-intege-53f1bf","errorCode":null,"errorMessage":"The number of iterations must be a positive integer.","messagePattern":"The number of iterations must be a positive integer\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/cli/src/crewai_cli/train_crew.py","lineNumber":17,"sourceCode":"import subprocess\n\nimport click\n\n\ndef train_crew(n_iterations: int, filename: str) -> None:\n    \"\"\"\n    Train the crew by running a command in the UV environment.\n\n    Args:\n        n_iterations (int): The number of iterations to train the crew.\n    \"\"\"\n    command = [\"uv\", \"run\", \"train\", str(n_iterations), filename]\n\n    try:\n        if n_iterations <= 0:\n            raise ValueError(\"The number of iterations must be a positive integer.\")\n\n        if not filename.endswith(\".pkl\"):\n            raise ValueError(\"The filename must not end with .pkl\")\n\n        result = subprocess.run(command, capture_output=False, text=True, check=True)  # noqa: S603\n\n        if result.stderr:\n            click.echo(result.stderr, err=True)\n\n    except subprocess.CalledProcessError as e:\n        click.echo(f\"An error occurred while training the crew: {e}\", err=True)\n        click.echo(e.output, err=True)\n\n    except Exception as e:\n        click.echo(f\"An unexpected error occurred: {e}\", err=True)\n","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/cli/src/crewai_cli/train_crew.py#L1-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"n = int(sys.argv[1])\nif n <= 0:\n    raise SystemExit(\"n_iterations must be a positive integer\")","typeGuard":"def is_positive_int(value) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value > 0","tryCatchPattern":null,"preventionTips":["Validate iteration count >= 1 before invoking train","Default loop variables to a positive number, never 0"],"tags":["crewai","cli","training","validation"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}