{"record":{"id":"f010ed28db855131","repo":"crewAIInc/crewAI","slug":"the-number-of-iterations-must-be-a-positive-intege","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/evaluate_crew.py","lineNumber":27,"sourceCode":"def evaluate_crew(\n    n_iterations: int, model: str, trained_agents_file: str | None = None\n) -> None:\n    \"\"\"Test and Evaluate the crew by running a command in the UV environment.\n\n    Args:\n        n_iterations: The number of iterations to test the crew.\n        model: The model to test the crew with.\n        trained_agents_file: Optional trained-agents pickle path forwarded to\n            the subprocess via the ``CREWAI_TRAINED_AGENTS_FILE`` env var.\n    \"\"\"\n    command = [\"uv\", \"run\", \"test\", str(n_iterations), model]\n    env = build_env_with_all_tool_credentials()\n    if trained_agents_file:\n        env[CREWAI_TRAINED_AGENTS_FILE_ENV] = trained_agents_file\n\n    try:\n        if n_iterations <= 0:\n            raise ValueError(\"The number of iterations must be a positive integer.\")\n\n        result = subprocess.run(  # noqa: S603\n            command, capture_output=False, text=True, check=True, env=env\n        )\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 testing 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":9,"sourceCodeEnd":42,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/cli/src/crewai_cli/evaluate_crew.py#L9-L42","documentation":"Raised by evaluate_crew (the `crewai test` flow) when n_iterations is zero or negative. It is a pre-flight argument check before the `uv run test <n> <model>` subprocess is launched; the ValueError is raised inside the try block, and since the except clauses handle CalledProcessError and generic Exception separately, the click echo path depends on the generic handler — the run never starts. It exists to stop wasted evaluation runs with nonsensical iteration counts.","triggerScenarios":"Calling `crewai test -n 0` (or a negative --n_iterations value), or calling evaluate_crew(n_iterations=0, model=...) programmatically. The check is `n_iterations <= 0`, so both 0 and negatives fail.","commonSituations":"CI scripts parameterizing iteration count where a variable defaults to 0, shell expansion of an unset variable producing 0, or users misunderstanding -n as verbosity rather than iteration count.","solutions":["Pass a positive integer: `crewai test -n 3 gpt-4o-mini`","If the count comes from a script/env var, default it to a sane positive value (e.g. 2 or 3) when unset","Validate the argument in your wrapper before invoking the CLI or evaluate_crew"],"exampleFix":"# before\ncrewai test -n 0 gpt-4o-mini\n# after\ncrewai test -n 3 gpt-4o-mini","handlingStrategy":"validation","validationCode":"def valid_iterations(n: int) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n > 0","typeGuard":"def is_positive_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value > 0","tryCatchPattern":"if n_iterations <= 0:\n    raise SystemExit(\"--n_iterations must be a positive integer\")\nevaluate_crew(n_iterations=n_iterations, model=model)","preventionTips":["Default iteration counts to a positive value in scripts","Validate CLI args in wrappers before invoking crewai test","Beware shell expansion of unset variables becoming 0"],"tags":["cli","validation","testing","arguments"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}